@@ -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+
5874void 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
104122static 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);
0 commit comments