From 21df381d12f67eeb767f17ac5c7b159c1e4897a0 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 4 Jul 2024 07:35:29 +0200 Subject: [PATCH] Coloring strings --- src/text_editor/buffer.cpp | 5 +++++ src/text_editor/main.cpp | 26 +++++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index 698e2b1..6f78f85 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -171,6 +171,11 @@ bool AreEqual(Cursor a, Cursor b) { return result; } +bool InRange(int64_t a, Range b) { + bool result = a >= b.min && a < b.max; + return result; +} + void MergeSort(int64_t Count, Edit *First, Edit *Temp) { // SortKey = range.min if (Count == 1) { diff --git a/src/text_editor/main.cpp b/src/text_editor/main.cpp index eb72790..cc2ab6c 100644 --- a/src/text_editor/main.cpp +++ b/src/text_editor/main.cpp @@ -87,7 +87,6 @@ int main() { window.font = font; window.font_size = font_size; window.font_spacing; - window.colored_strings.allocator = FrameArena; InitArena(&window.layout_arena); InitBuffer(GetSystemAllocator(), &window.buffer); @@ -115,6 +114,7 @@ int main() { For(windows) { Assert(it.cursors.len); it.main_cursor_begin_frame = it.cursors[0]; + it.colored_strings = {FrameArena}; } frame += 1; @@ -428,11 +428,15 @@ int main() { } { - int64_t index = 0; - String buffer_string = GetString(focused_window->buffer); - if (Seek(buffer_string, "number", &index)) { - Range range = {index, index + 6}; + String seek = "number"; + int64_t index = 0; + int64_t base_index = 0; + String s = GetString(focused_window->buffer); + while (Seek(s, seek, &index)) { + Range range = {base_index + index, base_index + index + seek.len}; focused_window->colored_strings.add({range, RED}); + base_index += index + seek.len; + s = s.skip(index + seek.len); } } @@ -452,7 +456,6 @@ int main() { // Draw and layout window overlay Vec2 mouse = GetMousePosition(); { - // @todo: why are we drawing this here?? DrawRectangleRec(ToRectangle(window.rect), WHITE); DrawRectangleRec(ToRectangle(line_number_rect), WHITE); @@ -645,12 +648,21 @@ int main() { ForItem(col, visible_columns) { Rect2 rect = {col.rect.min + window.rect.min - window.scroll, col.rect.max + window.rect.min - window.scroll}; + + Color color = BLACK; + For(window.colored_strings) { + if (InRange(col.pos, it.range)) { + color = it.color; + break; + } + } + if (col.codepoint == '\n') { DrawTextEx(font, "\\n", rect.min, font_size, font_spacing, GRAY); } else if (col.codepoint == '\0') { DrawTextEx(font, "\\0", rect.min, font_size, font_spacing, {255, 0, 0, 150}); } else if ((col.codepoint != ' ') && (col.codepoint != '\t')) { - DrawTextCodepoint(font, col.codepoint, rect.min, font_size, BLACK); + DrawTextCodepoint(font, col.codepoint, rect.min, font_size, color); } }