From feef8f3ebe75072c8e985b90dee243aafec28181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 18 Feb 2018 12:06:54 +0100 Subject: [PATCH] GUI: Updated code viewer --- gui/codeeditor.cpp | 32 ++++++++++++++++++++++++++++---- gui/codeeditor.h | 6 +++++- gui/resultsview.cpp | 11 +++++++++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/gui/codeeditor.cpp b/gui/codeeditor.cpp index 51eaeb7cb..bf0959529 100644 --- a/gui/codeeditor.cpp +++ b/gui/codeeditor.cpp @@ -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); diff --git a/gui/codeeditor.h b/gui/codeeditor.h index 41d555016..352bb4b58 100644 --- a/gui/codeeditor.h +++ b/gui/codeeditor.h @@ -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 highlightingRules; + QVector 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; diff --git a/gui/resultsview.cpp b/gui/resultsview.cpp index 00ad6177f..8dd13b64d 100644 --- a/gui/resultsview.cpp +++ b/gui/resultsview.cpp @@ -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); } }