Skip to content

Commit

Permalink
add basic syntax highlighting;
Browse files Browse the repository at this point in the history
  • Loading branch information
NateSeymour committed Nov 27, 2024
1 parent 0e42c2a commit 38a4167
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/calculator/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <QMainWindow>
#include <QPushButton>
#include <QSplitter>
#include <QTextCursor>
#include <QTextEdit>
#include <QVBoxLayout>
#include <format>
Expand All @@ -32,6 +33,12 @@ namespace ui

std::shared_ptr<unlogic::Scene> scene_;

std::map<unlogic::SyntaxHighlightingGroup, QColor> colors_ = {
{unlogic::SyntaxKeyword, QColor{223, 139, 86}},
};

std::map<unlogic::SyntaxHighlightingGroup, QTextCharFormat> formats_;

public Q_SLOTS:
void editorTextChanged()
{
Expand Down Expand Up @@ -62,8 +69,16 @@ namespace ui

void tokenizationComplete(std::vector<bf::Token<unlogic::ParserGrammarType>> tokens)
{
QTextCursor cursor(this->editor_->document());
for (auto const &token: tokens)
{
cursor.setPosition(token.location.begin);
cursor.setPosition(token.location.end, QTextCursor::MoveMode::KeepAnchor);

if (this->formats_.contains(token.terminal->user_data))
{
cursor.setCharFormat(this->formats_[token.terminal->user_data]);
}
}
}

Expand Down Expand Up @@ -117,6 +132,13 @@ namespace ui
QObject::connect(&this->compiler_controller_, &CompilerController::compilationError, this, &Window::compilationError);
QObject::connect(&this->compiler_controller_, &CompilerController::statusUpdate, this, &Window::statusUpdate);

// Syntax Highlighting
for (auto const &[group, color]: this->colors_)
{
QBrush brush(color);
this->formats_[group].setForeground(brush);
}

// Layout Composition
splitter->addWidget(this->editor_);
splitter->addWidget(this->renderer_widget_);
Expand Down

0 comments on commit 38a4167

Please sign in to comment.