GUI: Updated code viewer
This commit is contained in:
parent
21bb973702
commit
feef8f3ebe
|
@ -49,15 +49,31 @@ Highlighter::Highlighter(QTextDocument *parent)
|
|||
rule.format = singleLineCommentFormat;
|
||||
highlightingRules.append(rule);
|
||||
|
||||
highlightingRulesWithSymbols = highlightingRules;
|
||||
|
||||
multiLineCommentFormat.setForeground(Qt::gray);
|
||||
|
||||
symbolFormat.setForeground(Qt::red);
|
||||
symbolFormat.setBackground(QColor(220,220,255));
|
||||
|
||||
commentStartExpression = QRegularExpression("/\\*");
|
||||
commentEndExpression = QRegularExpression("\\*/");
|
||||
}
|
||||
|
||||
void Highlighter::setSymbols(const QStringList &symbols)
|
||||
{
|
||||
highlightingRulesWithSymbols = highlightingRules;
|
||||
foreach (const QString &sym, symbols) {
|
||||
HighlightingRule rule;
|
||||
rule.pattern = QRegularExpression("\\b" + sym + "\\b");
|
||||
rule.format = symbolFormat;
|
||||
highlightingRulesWithSymbols.append(rule);
|
||||
}
|
||||
}
|
||||
|
||||
void Highlighter::highlightBlock(const QString &text)
|
||||
{
|
||||
foreach (const HighlightingRule &rule, highlightingRules) {
|
||||
foreach (const HighlightingRule &rule, highlightingRulesWithSymbols) {
|
||||
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
|
||||
while (matchIterator.hasNext()) {
|
||||
QRegularExpressionMatch match = matchIterator.next();
|
||||
|
@ -94,6 +110,8 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
|
|||
highlighter = new Highlighter(this->document());
|
||||
mErrorPosition = -1;
|
||||
|
||||
setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
|
||||
|
||||
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
|
||||
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
|
||||
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
|
||||
|
@ -103,19 +121,25 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
|
|||
|
||||
static int getPos(const QString &fileData, int lineNumber)
|
||||
{
|
||||
if (lineNumber <= 1)
|
||||
return 0;
|
||||
for (int pos = 0, line = 1; pos < fileData.size(); ++pos) {
|
||||
if (fileData[pos] != '\n')
|
||||
continue;
|
||||
++line;
|
||||
if (line == lineNumber)
|
||||
if (line >= lineNumber)
|
||||
return pos + 1;
|
||||
}
|
||||
return fileData.size();
|
||||
}
|
||||
|
||||
void CodeEditor::setErrorLine(int errorLine)
|
||||
void CodeEditor::setError(const QString &code, int errorLine, const QStringList &symbols)
|
||||
{
|
||||
mErrorPosition = getPos(toPlainText(), errorLine);
|
||||
highlighter->setSymbols(symbols);
|
||||
|
||||
setPlainText(code);
|
||||
|
||||
mErrorPosition = getPos(code, errorLine);
|
||||
QTextCursor tc = textCursor();
|
||||
tc.setPosition(mErrorPosition);
|
||||
setTextCursor(tc);
|
||||
|
|
|
@ -20,6 +20,8 @@ class Highlighter : public QSyntaxHighlighter {
|
|||
public:
|
||||
explicit Highlighter(QTextDocument *parent);
|
||||
|
||||
void setSymbols(const QStringList &symbols);
|
||||
|
||||
protected:
|
||||
void highlightBlock(const QString &text) override;
|
||||
|
||||
|
@ -29,6 +31,7 @@ private:
|
|||
QTextCharFormat format;
|
||||
};
|
||||
QVector<HighlightingRule> highlightingRules;
|
||||
QVector<HighlightingRule> highlightingRulesWithSymbols;
|
||||
|
||||
QRegularExpression commentStartExpression;
|
||||
QRegularExpression commentEndExpression;
|
||||
|
@ -39,6 +42,7 @@ private:
|
|||
QTextCharFormat multiLineCommentFormat;
|
||||
QTextCharFormat quotationFormat;
|
||||
QTextCharFormat functionFormat;
|
||||
QTextCharFormat symbolFormat;
|
||||
};
|
||||
|
||||
class CodeEditor : public QPlainTextEdit {
|
||||
|
@ -50,7 +54,7 @@ public:
|
|||
void lineNumberAreaPaintEvent(QPaintEvent *event);
|
||||
int lineNumberAreaWidth();
|
||||
|
||||
void setErrorLine(int errorLine);
|
||||
void setError(const QString &code, int errorLine, const QStringList &symbols);
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
|
|
|
@ -406,9 +406,16 @@ void ResultsView::updateDetails(const QModelIndex &index)
|
|||
|
||||
QFile file(filepath);
|
||||
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
QStringList symbols;
|
||||
QRegularExpression re(".*: ([A-Za-z_][A-Za-z0-9_]*)$");
|
||||
const QString errorMessage = data["message"].toString();
|
||||
QRegularExpressionMatch match = re.match(errorMessage);
|
||||
if (match.hasMatch()) {
|
||||
symbols << match.captured(1);
|
||||
}
|
||||
|
||||
QTextStream in(&file);
|
||||
mUI.mCode->setPlainText(in.readAll());
|
||||
mUI.mCode->setErrorLine(lineNumber);
|
||||
mUI.mCode->setError(in.readAll(), lineNumber, symbols);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue