[Fix] Only apply tokenization to editor.

This commit is contained in:
Ritchie Cunningham 2025-10-24 17:52:39 +01:00
parent 420b570902
commit 6a5515e276
4 changed files with 25 additions and 18 deletions

View File

@ -15,7 +15,7 @@ Terminal::Terminal(GameState* game_state)
: _game_state(game_state), _should_close(false), _command_history_index(0), : _game_state(game_state), _should_close(false), _command_history_index(0),
_scroll_offset(0), _prompt(""), _pending_action({ActionType::NONE}), _scroll_offset(0), _prompt(""), _pending_action({ActionType::NONE}),
_session_id(0) { _session_id(0) {
_input_view = std::make_unique<TextView>(&_input_buffer, false, false); _input_view = std::make_unique<TextView>(&_input_buffer, false, false, false);
} }
Terminal::~Terminal(void) {} Terminal::~Terminal(void) {}

View File

@ -8,7 +8,7 @@
Editor::Editor(void) Editor::Editor(void)
: _should_close(false), _pending_action({ActionType::NONE}), : _should_close(false), _pending_action({ActionType::NONE}),
_filename("untitled.txt") { _filename("untitled.txt") {
_view = std::make_unique<TextView>(&_buffer, true, true); _view = std::make_unique<TextView>(&_buffer, true, true, true);
_menu_bar = std::make_unique<MenuBar>(25); _menu_bar = std::make_unique<MenuBar>(25);
_menu_bar->add_menu("File"); _menu_bar->add_menu("File");
_menu_bar->add_menu_item("File", "Save", [this]() { _menu_bar->add_menu_item("File", "Save", [this]() {
@ -19,7 +19,7 @@ Editor::Editor(void)
Editor::Editor(const std::string& filename) Editor::Editor(const std::string& filename)
: _should_close(false), _pending_action({ActionType::NONE}), : _should_close(false), _pending_action({ActionType::NONE}),
_filename(filename) { _filename(filename) {
_view = std::make_unique<TextView>(&_buffer, true, true); _view = std::make_unique<TextView>(&_buffer, true, true, true);
_menu_bar = std::make_unique<MenuBar>(25); _menu_bar = std::make_unique<MenuBar>(25);
_menu_bar->add_menu("File"); _menu_bar->add_menu("File");
_menu_bar->add_menu_item("File", "Save", [this]() { _menu_bar->add_menu_item("File", "Save", [this]() {

View File

@ -7,11 +7,13 @@
#include "ui/text_buffer.h" #include "ui/text_buffer.h"
#include "ui/ui_renderer.h" #include "ui/ui_renderer.h"
TextView::TextView(TextBuffer* buffer, bool handle_ret, bool show_line_numbers) : TextView::TextView(TextBuffer* buffer, bool handle_ret, bool show_line_numbers,
bool syntax_highlighting) :
_buffer(buffer), _buffer(buffer),
_scroll_offset(0), _scroll_offset(0),
_handle_ret(handle_ret), _handle_ret(handle_ret),
_show_line_numbers(show_line_numbers) { _show_line_numbers(show_line_numbers),
_syntax_highlighting(syntax_highlighting) {
_lua_keywords = { _lua_keywords = {
"and", "break", "do", "else", "elseif", "end", "false", "for", "and", "break", "do", "else", "elseif", "end", "false", "for",
@ -180,6 +182,7 @@ void TextView::render_text_content(UIRenderer* ui_renderer, const SyntaxTheme& t
} }
float current_x = x+padding + gutter_width; float current_x = x+padding + gutter_width;
if(_syntax_highlighting) {
std::vector<Token> tokens = _tokenize_line(_buffer->get_line(i)); std::vector<Token> tokens = _tokenize_line(_buffer->get_line(i));
for(const auto& token : tokens) { for(const auto& token : tokens) {
Color color = theme.normal; Color color = theme.normal;
@ -193,6 +196,9 @@ void TextView::render_text_content(UIRenderer* ui_renderer, const SyntaxTheme& t
ui_renderer->render_text(token.text.c_str(), current_x, current_y+18, color); ui_renderer->render_text(token.text.c_str(), current_x, current_y+18, color);
current_x += ui_renderer->get_text_renderer()->get_text_width(token.text.c_str(), 1.0f); current_x += ui_renderer->get_text_renderer()->get_text_width(token.text.c_str(), 1.0f);
} }
} else {
ui_renderer->render_text(_buffer->get_line(i).c_str(), current_x, current_y+18, theme.normal);
}
current_y += line_height; current_y += line_height;
} }
} }

View File

@ -26,7 +26,7 @@ struct Token {
class TextView { class TextView {
public: public:
TextView(TextBuffer* buffer, bool handle_ret, bool show_line_numbers); TextView(TextBuffer* buffer, bool handle_ret, bool show_line_numbers, bool syntax_highlighting);
~TextView(void); ~TextView(void);
bool handle_event(SDL_Event* event); bool handle_event(SDL_Event* event);
@ -43,5 +43,6 @@ private:
int _scroll_offset; int _scroll_offset;
bool _handle_ret; bool _handle_ret;
bool _show_line_numbers; bool _show_line_numbers;
bool _syntax_highlighting;
std::unordered_set<std::string> _lua_keywords; std::unordered_set<std::string> _lua_keywords;
}; };