@@ -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+
5898void 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
104145static 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
125174int 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
0 commit comments