Skip to content

Commit 47ba7ab

Browse files
committed
triage: updated the codeeditor
1 parent f191a3e commit 47ba7ab

File tree

3 files changed

+81
-33
lines changed

3 files changed

+81
-33
lines changed

tools/triage/codeeditor.cpp

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,48 @@ Highlighter::Highlighter(QTextDocument *parent)
1111
keywordFormat.setForeground(Qt::darkBlue);
1212
keywordFormat.setFontWeight(QFont::Bold);
1313
QStringList keywordPatterns;
14-
keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
15-
<< "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
16-
<< "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
17-
<< "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
18-
<< "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
19-
<< "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
20-
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
21-
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
22-
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
23-
<< "\\bvoid\\b" << "\\bvolatile\\b" << "\\bbool\\b";
14+
keywordPatterns << "bool"
15+
<< "break"
16+
<< "case"
17+
<< "char"
18+
<< "class"
19+
<< "const"
20+
<< "continue"
21+
<< "default"
22+
<< "do"
23+
<< "double"
24+
<< "else"
25+
<< "enum"
26+
<< "explicit"
27+
<< "for"
28+
<< "friend"
29+
<< "if"
30+
<< "inline"
31+
<< "int"
32+
<< "long"
33+
<< "namespace"
34+
<< "operator"
35+
<< "private"
36+
<< "protected"
37+
<< "public"
38+
<< "return"
39+
<< "short"
40+
<< "signed"
41+
<< "static"
42+
<< "struct"
43+
<< "switch"
44+
<< "template"
45+
<< "throw"
46+
<< "typedef"
47+
<< "typename"
48+
<< "union"
49+
<< "unsigned"
50+
<< "virtual"
51+
<< "void"
52+
<< "volatile"
53+
<< "while";
2454
foreach (const QString &pattern, keywordPatterns) {
25-
rule.pattern = QRegularExpression(pattern);
55+
rule.pattern = QRegularExpression("\\b" + pattern + "\\b");
2656
rule.format = keywordFormat;
2757
highlightingRules.append(rule);
2858
}
@@ -38,26 +68,36 @@ Highlighter::Highlighter(QTextDocument *parent)
3868
rule.format = quotationFormat;
3969
highlightingRules.append(rule);
4070

41-
functionFormat.setFontItalic(true);
42-
functionFormat.setForeground(Qt::blue);
43-
rule.pattern = QRegularExpression("\\b[A-Za-z0-9_]+(?=\\()");
44-
rule.format = functionFormat;
45-
highlightingRules.append(rule);
46-
4771
singleLineCommentFormat.setForeground(Qt::gray);
4872
rule.pattern = QRegularExpression("//[^\n]*");
4973
rule.format = singleLineCommentFormat;
5074
highlightingRules.append(rule);
5175

76+
highlightingRulesWithSymbols = highlightingRules;
77+
5278
multiLineCommentFormat.setForeground(Qt::gray);
5379

80+
symbolFormat.setForeground(Qt::red);
81+
symbolFormat.setBackground(QColor(220,220,255));
82+
5483
commentStartExpression = QRegularExpression("/\\*");
5584
commentEndExpression = QRegularExpression("\\*/");
5685
}
5786

87+
void Highlighter::setSymbols(const QStringList &symbols)
88+
{
89+
highlightingRulesWithSymbols = highlightingRules;
90+
foreach (const QString &sym, symbols) {
91+
HighlightingRule rule;
92+
rule.pattern = QRegularExpression("\\b" + sym + "\\b");
93+
rule.format = symbolFormat;
94+
highlightingRulesWithSymbols.append(rule);
95+
}
96+
}
97+
5898
void Highlighter::highlightBlock(const QString &text)
5999
{
60-
foreach (const HighlightingRule &rule, highlightingRules) {
100+
foreach (const HighlightingRule &rule, highlightingRulesWithSymbols) {
61101
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
62102
while (matchIterator.hasNext()) {
63103
QRegularExpressionMatch match = matchIterator.next();
@@ -94,32 +134,41 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
94134
highlighter = new Highlighter(this->document());
95135
mErrorPosition = -1;
96136

137+
setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
138+
97139
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
98140
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
99-
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
100141

101142
updateLineNumberAreaWidth(0);
102143
}
103144

104145
static int getPos(const QString &fileData, int lineNumber)
105146
{
147+
if (lineNumber <= 1)
148+
return 0;
106149
for (int pos = 0, line = 1; pos < fileData.size(); ++pos) {
107150
if (fileData[pos] != '\n')
108151
continue;
109152
++line;
110-
if (line == lineNumber)
153+
if (line >= lineNumber)
111154
return pos + 1;
112155
}
113156
return fileData.size();
114157
}
115158

116-
void CodeEditor::setErrorLine(int errorLine)
159+
void CodeEditor::setError(const QString &code, int errorLine, const QStringList &symbols)
117160
{
118-
mErrorPosition = getPos(toPlainText(), errorLine);
161+
highlighter->setSymbols(symbols);
162+
163+
setPlainText(code);
164+
165+
mErrorPosition = getPos(code, errorLine);
119166
QTextCursor tc = textCursor();
120167
tc.setPosition(mErrorPosition);
121168
setTextCursor(tc);
122169
centerCursor();
170+
171+
highlightErrorLine();
123172
}
124173

125174
int CodeEditor::lineNumberAreaWidth()
@@ -158,12 +207,8 @@ void CodeEditor::resizeEvent(QResizeEvent *event)
158207
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
159208
}
160209

161-
void CodeEditor::highlightCurrentLine()
210+
void CodeEditor::highlightErrorLine()
162211
{
163-
QTextCursor tc = textCursor();
164-
tc.setPosition(mErrorPosition);
165-
setTextCursor(tc);
166-
167212
QList<QTextEdit::ExtraSelection> extraSelections;
168213

169214
QTextEdit::ExtraSelection selection;
@@ -172,7 +217,8 @@ void CodeEditor::highlightCurrentLine()
172217

173218
selection.format.setBackground(lineColor);
174219
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
175-
selection.cursor = textCursor();
220+
selection.cursor = QTextCursor(document());
221+
selection.cursor.setPosition(mErrorPosition);
176222
selection.cursor.clearSelection();
177223
extraSelections.append(selection);
178224

tools/triage/codeeditor.h

Lines changed: 6 additions & 4 deletions
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;
@@ -38,7 +41,7 @@ class Highlighter : public QSyntaxHighlighter {
3841
QTextCharFormat singleLineCommentFormat;
3942
QTextCharFormat multiLineCommentFormat;
4043
QTextCharFormat quotationFormat;
41-
QTextCharFormat functionFormat;
44+
QTextCharFormat symbolFormat;
4245
};
4346

4447
class CodeEditor : public QPlainTextEdit {
@@ -47,20 +50,19 @@ class CodeEditor : public QPlainTextEdit {
4750
public:
4851
explicit CodeEditor(QWidget *parent);
4952
CodeEditor(const CodeEditor &) = delete;
50-
~CodeEditor() {}
5153
CodeEditor &operator=(const CodeEditor &) = delete;
5254

5355
void lineNumberAreaPaintEvent(QPaintEvent *event);
5456
int lineNumberAreaWidth();
5557

56-
void setErrorLine(int errorLine);
58+
void setError(const QString &code, int errorLine, const QStringList &symbols);
5759

5860
protected:
5961
void resizeEvent(QResizeEvent *event) override;
6062

6163
private slots:
6264
void updateLineNumberAreaWidth(int newBlockCount);
63-
void highlightCurrentLine();
65+
void highlightErrorLine();
6466
void updateLineNumberArea(const QRect &, int);
6567

6668
private:

tools/triage/mainwindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void MainWindow::loadFile()
4646
allErrors << (url + "\n" + errorMessage);
4747
url = line;
4848
errorMessage.clear();
49-
} else if (!url.isEmpty() && QRegExp(".*: (error|warning|note):.*").exactMatch(line)) {
49+
} else if (!url.isEmpty() && QRegExp(".*: (error|warning|style|note):.*").exactMatch(line)) {
5050
if (!errorMessage.isEmpty())
5151
errorMessage += '\n';
5252
errorMessage += line;

0 commit comments

Comments
 (0)