Skip to content

Commit 9c7e9a5

Browse files
committed
Working on syntax highlighting
1 parent 08b89a4 commit 9c7e9a5

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

src/Interface/Modules/Python/InterfaceWithPythonDialog.cc

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
139139

140140
updateLineNumberAreaWidth(0);
141141
highlightCurrentLine();
142+
143+
highlighter_ = new Highlighter(document());
142144
}
143145

144146
int 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+
}

src/Interface/Modules/Python/InterfaceWithPythonDialog.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ private Q_SLOTS:
7474

7575
private:
7676
QWidget* lineNumberArea_;
77+
class Highlighter* highlighter_;
7778
};
7879

7980
class LineNumberArea : public QWidget
@@ -96,6 +97,35 @@ class LineNumberArea : public QWidget
9697
CodeEditor *codeEditor;
9798
};
9899

100+
class Highlighter : public QSyntaxHighlighter
101+
{
102+
Q_OBJECT
103+
104+
public:
105+
Highlighter(QTextDocument* parent = nullptr);
106+
107+
protected:
108+
void highlightBlock(const QString &text) override;
109+
110+
private:
111+
struct HighlightingRule
112+
{
113+
QRegExp pattern;
114+
QTextCharFormat format;
115+
};
116+
QVector<HighlightingRule> highlightingRules;
117+
118+
QRegExp commentStartExpression;
119+
QRegExp commentEndExpression;
120+
121+
QTextCharFormat keywordFormat;
122+
QTextCharFormat classFormat;
123+
QTextCharFormat singleLineCommentFormat;
124+
QTextCharFormat multiLineCommentFormat;
125+
QTextCharFormat quotationFormat;
126+
QTextCharFormat functionFormat;
127+
};
128+
99129
}
100130
}
101131

0 commit comments

Comments
 (0)