From 38a4167078201c524c68ede5562bfdd97b69819f Mon Sep 17 00:00:00 2001 From: Nathan Seymour Date: Wed, 27 Nov 2024 14:43:03 -0600 Subject: [PATCH] add basic syntax highlighting; --- src/calculator/Window.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/calculator/Window.h b/src/calculator/Window.h index 020425c..05f5917 100644 --- a/src/calculator/Window.h +++ b/src/calculator/Window.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -32,6 +33,12 @@ namespace ui std::shared_ptr scene_; + std::map colors_ = { + {unlogic::SyntaxKeyword, QColor{223, 139, 86}}, + }; + + std::map formats_; + public Q_SLOTS: void editorTextChanged() { @@ -62,8 +69,16 @@ namespace ui void tokenizationComplete(std::vector> 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]); + } } } @@ -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_);