Skip to content

Commit da95e0f

Browse files
authored
Merge pull request #12 from sandbox-science/syntax
Syntax Highlighter Foundation
2 parents 60e346e + 50b645b commit da95e0f

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ add_executable(${TARGET_NAME}
5454
src/main.cpp
5555
src/MainWindow.cpp
5656
src/CodeEditor.cpp
57+
src/Syntax.cpp
5758
include/MainWindow.h
5859
include/CodeEditor.h
60+
include/Syntax.h
5961
)
6062

6163
qt_add_resources(APP_RESOURCES resources.qrc)

include/MainWindow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define MAINWINDOW_H
33

44
#include "CodeEditor.h"
5+
#include "Syntax.h"
56
#include <QMainWindow>
67
#include <QMenu>
78
#include <QAction>
@@ -34,6 +35,7 @@ private slots:
3435
void (MainWindow::*slot)());
3536
CodeEditor *editor;
3637
QString currentFileName;
38+
Syntax *syntax;
3739
};
3840

3941
#endif // MAINWINDOW_H

include/Syntax.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef SYNTAX_H
2+
#define SYNTAX_H
3+
4+
#include <QTextCharFormat>
5+
#include <QRegularExpression>
6+
#include <QSyntaxHighlighter>
7+
#include <QTextDocument>
8+
9+
class Syntax : public QSyntaxHighlighter
10+
{
11+
Q_OBJECT
12+
13+
public:
14+
Syntax(QTextDocument *parent = nullptr);
15+
16+
protected:
17+
void highlightBlock(const QString &text) override;
18+
19+
private:
20+
struct SyntaxRule
21+
{
22+
QRegularExpression pattern;
23+
QTextCharFormat format;
24+
};
25+
QList<SyntaxRule> syntaxRules;
26+
27+
QTextCharFormat keywordFormat;
28+
QTextCharFormat singleLineCommentFormat;
29+
QTextCharFormat quotationMark;
30+
QTextCharFormat functionFormat;
31+
QTextCharFormat parenthesisFormat;
32+
QTextCharFormat charFormat;
33+
34+
void addPattern(const QString &pattern, const QTextCharFormat &format);
35+
};
36+
37+
#endif // SYNTAX_H

src/MainWindow.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "MainWindow.h"
2+
#include "Syntax.h"
23

34
#include <QMenuBar>
45
#include <QFileDialog>
@@ -15,8 +16,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
1516
resize(800, 600);
1617

1718
editor = new CodeEditor(this);
18-
setCentralWidget(editor);
19+
syntax = new Syntax(editor->document());
1920

21+
setCentralWidget(editor);
2022
createMenuBar();
2123
}
2224

src/Syntax.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "Syntax.h"
2+
3+
Syntax::Syntax(QTextDocument *parent) : QSyntaxHighlighter(parent)
4+
{
5+
keywordFormat.setForeground(Qt::blue);
6+
keywordFormat.setFontWeight(QFont::Bold);
7+
QStringList keywordPatterns;
8+
keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
9+
<< "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
10+
<< "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
11+
<< "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
12+
<< "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
13+
<< "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
14+
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
15+
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
16+
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
17+
<< "\\bvoid\\b" << "\\bvolatile\\b" << "\\bforeach\\b";
18+
foreach (const QString &pattern, keywordPatterns)
19+
{
20+
addPattern(pattern, keywordFormat);
21+
}
22+
23+
// Single line comment format expression
24+
singleLineCommentFormat.setForeground(Qt::darkGray);
25+
addPattern("//[^\n]*", singleLineCommentFormat);
26+
27+
// Double quotation mark for string
28+
quotationMark.setForeground(Qt::darkGreen);
29+
addPattern("\".*\"", quotationMark);
30+
31+
// Function format expression
32+
functionFormat.setFontItalic(true);
33+
functionFormat.setForeground(Qt::darkYellow);
34+
addPattern("\\b[a-zA-Z_][a-zA-Z0-9_]*(?=\\s*\\()", functionFormat);
35+
36+
// Color pattern for parenthesis
37+
QColor parenthesisColor("#6495ED");
38+
parenthesisFormat.setForeground(parenthesisColor);
39+
addPattern("[()]", parenthesisFormat);
40+
41+
// Regex for single character format 'a', '\n', etc
42+
charFormat.setForeground(Qt::darkCyan);
43+
addPattern("'(\\\\.|[^'])'", charFormat);
44+
}
45+
46+
// Add syntax highlighting patterns
47+
void Syntax::addPattern(const QString &pattern, const QTextCharFormat &format)
48+
{
49+
SyntaxRule rule;
50+
rule.pattern = QRegularExpression(pattern);
51+
rule.format = format;
52+
syntaxRules.append(rule);
53+
}
54+
55+
void Syntax::highlightBlock(const QString &text)
56+
{
57+
foreach (const SyntaxRule &rule, syntaxRules)
58+
{
59+
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
60+
while (matchIterator.hasNext())
61+
{
62+
QRegularExpressionMatch match = matchIterator.next();
63+
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)