Skip to content

Syntax Highlighter Foundation #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ add_executable(${TARGET_NAME}
src/main.cpp
src/MainWindow.cpp
src/CodeEditor.cpp
src/Syntax.cpp
include/MainWindow.h
include/CodeEditor.h
include/Syntax.h
)

qt_add_resources(APP_RESOURCES resources.qrc)
Expand Down
2 changes: 2 additions & 0 deletions include/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define MAINWINDOW_H

#include "CodeEditor.h"
#include "Syntax.h"
#include <QMainWindow>
#include <QMenu>
#include <QAction>
Expand Down Expand Up @@ -34,6 +35,7 @@ private slots:
void (MainWindow::*slot)());
CodeEditor *editor;
QString currentFileName;
Syntax *syntax;
};

#endif // MAINWINDOW_H
37 changes: 37 additions & 0 deletions include/Syntax.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef SYNTAX_H
#define SYNTAX_H

#include <QTextCharFormat>
#include <QRegularExpression>
#include <QSyntaxHighlighter>
#include <QTextDocument>

class Syntax : public QSyntaxHighlighter
{
Q_OBJECT

public:
Syntax(QTextDocument *parent = nullptr);

protected:
void highlightBlock(const QString &text) override;

private:
struct SyntaxRule
{
QRegularExpression pattern;
QTextCharFormat format;
};
QList<SyntaxRule> syntaxRules;

QTextCharFormat keywordFormat;
QTextCharFormat singleLineCommentFormat;
QTextCharFormat quotationMark;
QTextCharFormat functionFormat;
QTextCharFormat parenthesisFormat;
QTextCharFormat charFormat;

void addPattern(const QString &pattern, const QTextCharFormat &format);
};

#endif // SYNTAX_H
4 changes: 3 additions & 1 deletion src/MainWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "MainWindow.h"
#include "Syntax.h"

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

editor = new CodeEditor(this);
setCentralWidget(editor);
syntax = new Syntax(editor->document());

setCentralWidget(editor);
createMenuBar();
}

Expand Down
66 changes: 66 additions & 0 deletions src/Syntax.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "Syntax.h"

Syntax::Syntax(QTextDocument *parent) : QSyntaxHighlighter(parent)
{
keywordFormat.setForeground(Qt::blue);
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns;
keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
<< "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
<< "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
<< "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
<< "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
<< "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
<< "\\bvoid\\b" << "\\bvolatile\\b" << "\\bforeach\\b";
foreach (const QString &pattern, keywordPatterns)
{
addPattern(pattern, keywordFormat);
}

// Single line comment format expression
singleLineCommentFormat.setForeground(Qt::darkGray);
addPattern("//[^\n]*", singleLineCommentFormat);

// Double quotation mark for string
quotationMark.setForeground(Qt::darkGreen);
addPattern("\".*\"", quotationMark);

// Function format expression
functionFormat.setFontItalic(true);
functionFormat.setForeground(Qt::darkYellow);
addPattern("\\b[a-zA-Z_][a-zA-Z0-9_]*(?=\\s*\\()", functionFormat);

// Color pattern for parenthesis
QColor parenthesisColor("#6495ED");
parenthesisFormat.setForeground(parenthesisColor);
addPattern("[()]", parenthesisFormat);

// Regex for single character format 'a', '\n', etc
charFormat.setForeground(Qt::darkCyan);
addPattern("'(\\\\.|[^'])'", charFormat);
}

// Add syntax highlighting patterns
void Syntax::addPattern(const QString &pattern, const QTextCharFormat &format)
{
SyntaxRule rule;
rule.pattern = QRegularExpression(pattern);
rule.format = format;
syntaxRules.append(rule);
}

void Syntax::highlightBlock(const QString &text)
{
foreach (const SyntaxRule &rule, syntaxRules)
{
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext())
{
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
}