@@ -139,6 +139,8 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
139139
140140 updateLineNumberAreaWidth (0 );
141141 highlightCurrentLine ();
142+
143+ highlighter_ = new Highlighter (document ());
142144}
143145
144146int CodeEditor::lineNumberAreaWidth ()
@@ -220,3 +222,88 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
220222 ++blockNumber;
221223}
222224}
225+
226+ Highlighter::Highlighter (QTextDocument *parent)
227+ : QSyntaxHighlighter(parent)
228+ {
229+ HighlightingRule rule;
230+
231+ keywordFormat.setForeground (Qt::green);
232+ keywordFormat.setFontWeight (QFont::Bold);
233+ QStringList keywordPatterns;
234+ keywordPatterns << " \\ bchar\\ b" << " \\ bclass\\ b" << " \\ bconst\\ b"
235+ << " \\ bdouble\\ b" << " \\ benum\\ b" << " \\ bexplicit\\ b"
236+ << " \\ bfriend\\ b" << " \\ binline\\ b" << " \\ bint\\ b"
237+ << " \\ blong\\ b" << " \\ bnamespace\\ b" << " \\ boperator\\ b"
238+ << " \\ bprivate\\ b" << " \\ bprotected\\ b" << " \\ bpublic\\ b"
239+ << " \\ bshort\\ b" << " \\ bsignals\\ b" << " \\ bsigned\\ b"
240+ << " \\ bslots\\ b" << " \\ bstatic\\ b" << " \\ bstruct\\ b"
241+ << " \\ btemplate\\ b" << " \\ btypedef\\ b" << " \\ btypename\\ b"
242+ << " \\ bunion\\ b" << " \\ bunsigned\\ b" << " \\ bvirtual\\ b"
243+ << " \\ bvoid\\ b" << " \\ bvolatile\\ b" << " \\ bfor\\ b" << " \\ bin\\ b"
244+ << " \\ bTrue\\ b" << " \\ bFalse\\ b" ;
245+ for (const auto & pattern : keywordPatterns)
246+ {
247+ rule.pattern = QRegExp (pattern);
248+ rule.format = keywordFormat;
249+ highlightingRules.append (rule);
250+ }
251+ classFormat.setFontWeight (QFont::Bold);
252+ classFormat.setForeground (Qt::darkMagenta);
253+ rule.pattern = QRegExp (" \\ bQ[A-Za-z]+\\ b" );
254+ rule.format = classFormat;
255+ highlightingRules.append (rule);
256+
257+ quotationFormat.setForeground (Qt::darkGreen);
258+ rule.pattern = QRegExp (" \" .*\" " );
259+ rule.format = quotationFormat;
260+ highlightingRules.append (rule);
261+
262+ functionFormat.setFontItalic (true );
263+ functionFormat.setForeground (Qt::cyan);
264+ rule.pattern = QRegExp (" \\ b[A-Za-z0-9_]+(?=\\ ()" );
265+ rule.format = functionFormat;
266+ highlightingRules.append (rule);
267+
268+ singleLineCommentFormat.setForeground (Qt::yellow);
269+ rule.pattern = QRegExp (" #[^\n ]*" );
270+ rule.format = singleLineCommentFormat;
271+ highlightingRules.append (rule);
272+
273+ multiLineCommentFormat.setForeground (Qt::yellow);
274+
275+ commentStartExpression = QRegExp (" /\\ *" );
276+ commentEndExpression = QRegExp (" \\ */" );
277+ }
278+
279+ void Highlighter::highlightBlock (const QString &text)
280+ {
281+ for (const auto & rule : highlightingRules)
282+ {
283+ QRegExp expression (rule.pattern );
284+ int index = expression.indexIn (text);
285+ while (index >= 0 ) {
286+ int length = expression.matchedLength ();
287+ setFormat (index, length, rule.format );
288+ index = expression.indexIn (text, index + length);
289+ }
290+ }
291+ setCurrentBlockState (0 );
292+ int startIndex = 0 ;
293+ if (previousBlockState () != 1 )
294+ startIndex = commentStartExpression.indexIn (text);
295+
296+ while (startIndex >= 0 ) {
297+ int endIndex = commentEndExpression.indexIn (text, startIndex);
298+ int commentLength;
299+ if (endIndex == -1 ) {
300+ setCurrentBlockState (1 );
301+ commentLength = text.length () - startIndex;
302+ } else {
303+ commentLength = endIndex - startIndex
304+ + commentEndExpression.matchedLength ();
305+ }
306+ setFormat (startIndex, commentLength, multiLineCommentFormat);
307+ startIndex = commentStartExpression.indexIn (text, startIndex + commentLength);
308+ }
309+ }
0 commit comments