Skip to content

Commit feef8f3

Browse files
committed
GUI: Updated code viewer
1 parent 21bb973 commit feef8f3

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

gui/codeeditor.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,31 @@ Highlighter::Highlighter(QTextDocument *parent)
4949
rule.format = singleLineCommentFormat;
5050
highlightingRules.append(rule);
5151

52+
highlightingRulesWithSymbols = highlightingRules;
53+
5254
multiLineCommentFormat.setForeground(Qt::gray);
5355

56+
symbolFormat.setForeground(Qt::red);
57+
symbolFormat.setBackground(QColor(220,220,255));
58+
5459
commentStartExpression = QRegularExpression("/\\*");
5560
commentEndExpression = QRegularExpression("\\*/");
5661
}
5762

63+
void Highlighter::setSymbols(const QStringList &symbols)
64+
{
65+
highlightingRulesWithSymbols = highlightingRules;
66+
foreach (const QString &sym, symbols) {
67+
HighlightingRule rule;
68+
rule.pattern = QRegularExpression("\\b" + sym + "\\b");
69+
rule.format = symbolFormat;
70+
highlightingRulesWithSymbols.append(rule);
71+
}
72+
}
73+
5874
void Highlighter::highlightBlock(const QString &text)
5975
{
60-
foreach (const HighlightingRule &rule, highlightingRules) {
76+
foreach (const HighlightingRule &rule, highlightingRulesWithSymbols) {
6177
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
6278
while (matchIterator.hasNext()) {
6379
QRegularExpressionMatch match = matchIterator.next();
@@ -94,6 +110,8 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
94110
highlighter = new Highlighter(this->document());
95111
mErrorPosition = -1;
96112

113+
setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
114+
97115
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
98116
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
99117
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
@@ -103,19 +121,25 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
103121

104122
static int getPos(const QString &fileData, int lineNumber)
105123
{
124+
if (lineNumber <= 1)
125+
return 0;
106126
for (int pos = 0, line = 1; pos < fileData.size(); ++pos) {
107127
if (fileData[pos] != '\n')
108128
continue;
109129
++line;
110-
if (line == lineNumber)
130+
if (line >= lineNumber)
111131
return pos + 1;
112132
}
113133
return fileData.size();
114134
}
115135

116-
void CodeEditor::setErrorLine(int errorLine)
136+
void CodeEditor::setError(const QString &code, int errorLine, const QStringList &symbols)
117137
{
118-
mErrorPosition = getPos(toPlainText(), errorLine);
138+
highlighter->setSymbols(symbols);
139+
140+
setPlainText(code);
141+
142+
mErrorPosition = getPos(code, errorLine);
119143
QTextCursor tc = textCursor();
120144
tc.setPosition(mErrorPosition);
121145
setTextCursor(tc);

gui/codeeditor.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Highlighter : public QSyntaxHighlighter {
2020
public:
2121
explicit Highlighter(QTextDocument *parent);
2222

23+
void setSymbols(const QStringList &symbols);
24+
2325
protected:
2426
void highlightBlock(const QString &text) override;
2527

@@ -29,6 +31,7 @@ class Highlighter : public QSyntaxHighlighter {
2931
QTextCharFormat format;
3032
};
3133
QVector<HighlightingRule> highlightingRules;
34+
QVector<HighlightingRule> highlightingRulesWithSymbols;
3235

3336
QRegularExpression commentStartExpression;
3437
QRegularExpression commentEndExpression;
@@ -39,6 +42,7 @@ class Highlighter : public QSyntaxHighlighter {
3942
QTextCharFormat multiLineCommentFormat;
4043
QTextCharFormat quotationFormat;
4144
QTextCharFormat functionFormat;
45+
QTextCharFormat symbolFormat;
4246
};
4347

4448
class CodeEditor : public QPlainTextEdit {
@@ -50,7 +54,7 @@ class CodeEditor : public QPlainTextEdit {
5054
void lineNumberAreaPaintEvent(QPaintEvent *event);
5155
int lineNumberAreaWidth();
5256

53-
void setErrorLine(int errorLine);
57+
void setError(const QString &code, int errorLine, const QStringList &symbols);
5458

5559
protected:
5660
void resizeEvent(QResizeEvent *event) override;

gui/resultsview.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,16 @@ void ResultsView::updateDetails(const QModelIndex &index)
406406

407407
QFile file(filepath);
408408
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
409+
QStringList symbols;
410+
QRegularExpression re(".*: ([A-Za-z_][A-Za-z0-9_]*)$");
411+
const QString errorMessage = data["message"].toString();
412+
QRegularExpressionMatch match = re.match(errorMessage);
413+
if (match.hasMatch()) {
414+
symbols << match.captured(1);
415+
}
416+
409417
QTextStream in(&file);
410-
mUI.mCode->setPlainText(in.readAll());
411-
mUI.mCode->setErrorLine(lineNumber);
418+
mUI.mCode->setError(in.readAll(), lineNumber, symbols);
412419
}
413420
}
414421

0 commit comments

Comments
 (0)