diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index 1dcd850..4b517be 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -168,6 +168,7 @@ void ReplaceText(Buffer *buffer, Range range, String16 string) { Assert(range.max >= range.min); Assert(range.max >= 0 && range.max <= buffer->len); Assert(range.min >= 0 && range.min <= buffer->len); + buffer->dirty = true; Int size_to_remove = range.max - range.min; Int size_to_add = string.len; diff --git a/src/text_editor/colors.cpp b/src/text_editor/colors.cpp index af986ff..3555b2d 100644 --- a/src/text_editor/colors.cpp +++ b/src/text_editor/colors.cpp @@ -44,7 +44,7 @@ Color ColorScrollbarScroller = GruvboxLight1; Color ColorScrollbarScrollerSelected = GruvboxLight0Hard; Color ColorLineHighlight = GruvboxLight1; -Color ColorMainCaret = GruvboxBrightRed; +Color ColorMainCaret = GruvboxDark0Hard; Color ColorSubCaret = GruvboxGray245; Color ColorSelection = GruvboxLight1; Color ColorWhitespaceDuringSelection = GruvboxLight2; \ No newline at end of file diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 6450973..04778ae 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -236,6 +236,7 @@ bool GlobalCommand(Event event) { bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect); bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, window->scrollbar_rect); bool mouse_in_total = CheckCollisionPointRec(mouse, window->total_rect); + window->mouse_in_scrollbar = mouse_in_scrollbar; static SDL_Cursor *SDL_MouseCursor; if (SDL_MouseCursor) SDL_DestroyCursor(SDL_MouseCursor); diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 8b90b52..bc3f6be 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -216,6 +216,23 @@ void MergeCarets(View *view, Range *mouse_selection_anchor) { Swap(&view->carets[first_caret_index], &view->carets[0]); } +void ReplaceDebugbarData() { + Buffer *buffer = GetBuffer("*debug*"); + + Window *window = GetActiveWindow(); + View *view = GetActiveView(window); + if (view->active_buffer.id == buffer->id.id) return; + + Window *last_window = GetWindow(GetLastActiveWindow()); + View *last_view = GetActiveView(last_window); + Buffer *last_buffer = GetBuffer(last_view->active_buffer); + + Scratch scratch; + String s = Format(scratch, "wid: %d\nvid: %d\nbid: %d\nframe: %lld", (int)last_window->id.id, (int)last_view->id.id, (int)last_buffer->id.id, (long long)FrameID); + String16 string = ToString16(scratch, s); + ReplaceText(buffer, GetRange(*buffer), string); +} + void ReplaceInfobarData() { Window *window = GetWindow(InfoBarWindowID); if (IsActive(window)) return; @@ -234,10 +251,13 @@ void ReplaceInfobarData() { String16 buffer_string = GetString(*buffer); Range replace_range = {0, buffer->len}; if (!Seek(buffer_string, L"|", &replace_range.max)) { - ReplaceText(buffer, GetEndAsRange(*buffer), L"|"); + // ReplaceText(buffer, GetEndAsRange(*buffer), L"|"); } - String s = Format(scratch, "line: %5lld col: %5lld name: %.*s wid: %d vid: %d bid: %d frame%lld", (long long)xy.line + 1ll, (long long)xy.col + 1ll, FmtString(name), (int)last_window->id.id, (int)last_view->id.id, (int)last_buffer->id.id, (long long)FrameID); + const char *dirty = ""; + if (last_buffer->dirty) dirty = "!"; + + String s = Format(scratch, "line: %5lld col: %5lld name: %.*s%s", (long long)xy.line + 1ll, (long long)xy.col + 1ll, FmtString(name), dirty); String16 string = ToString16(scratch, s); ReplaceText(buffer, replace_range, string); Command_SelectRangeOneCursor(view, {}); @@ -512,6 +532,18 @@ void WindowCommand(Event event, Window *window, View *view) { } } + if (Ctrl(SDLK_S) && buffer->dirty) { + String16 string16 = GetString(*buffer); + Scratch scratch; + String string = ToString(scratch, string16); + bool success = WriteFile(buffer->name, string); + if (success) { + } else { + String msg = Format(scratch, "Failed to save file with name: %.*s", FmtString(buffer->name)); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Failed to save!", msg.data, NULL); + } + } + if (!Mouse(NONE)) { ProfileScope(mouse); diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 0cdb471..cb9fe2f 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -14,7 +14,7 @@ WindowID InfoBarWindowID = {0}; WindowID SearchWindowID = {0}; Array WindowSwitchHistory; // @todo: probably better as a circular buffer -WindowID ActiveWindow; // @todo: maybe NextActiveWindow and change at end of frame +WindowID ActiveWindow; Int CaretChangeID; Int LastFrameIDWhenSwitchedActiveWindow; diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 49480f2..6d75eb7 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -178,6 +178,7 @@ int main() View *view = GetActiveView(window); view->main_caret_on_begin_frame = view->carets[0]; view->update_scroll = true; + window->mouse_in_scrollbar = false; } SDL_Event event; @@ -201,6 +202,7 @@ int main() } ReplaceInfobarData(); + ReplaceDebugbarData(); For(IterateInReverse(&order)) { Window *window = &Windows[it]; diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 54df980..8e8cb5f 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -36,6 +36,7 @@ struct Buffer { Array redo_stack; int debug_edit_phase; bool no_history; + bool dirty; }; struct View { @@ -66,6 +67,8 @@ struct Window { struct { bool mouse_selecting_scrollbar : 1; bool mouse_selecting : 1; + bool mouse_in_scrollbar : 1; + bool draw_scrollbar : 1; bool draw_line_numbers : 1; bool visible : 1; diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index df97f79..a87ec1e 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,12 +1,17 @@ - Save file (utf16->utf8) +- Some kind of plumbing, linking - resize windows +- laying out windows, more choice +- window borders - file dock on left side - We can actually combine this with command window and lua, it's just going to be a buffer of - open "asd/asd/asd/asd" -- Ctrl + F -- Rework window views to contain a history of past views - word completion - Colored strings -- font cache and on demand unicode loads \ No newline at end of file +- font cache and on demand unicode loads + +- Search all buffers +- Search and replace +- Search result buffer diff --git a/src/text_editor/window.cpp b/src/text_editor/window.cpp index b69c8d5..c36c1af 100644 --- a/src/text_editor/window.cpp +++ b/src/text_editor/window.cpp @@ -10,6 +10,7 @@ void InitWindows() { Allocator sys_allocator = GetSystemAllocator(); { Buffer *buffer = CreateBuffer(sys_allocator, "*scratch*"); + View *view = CreateView(buffer->id); } { @@ -27,12 +28,18 @@ void InitWindows() { LoadUnicode(b); AddView(w, v->id); } + { - Window *w = CreateWindow(); - Buffer *b = GetBuffer({0}); - View *v = CreateView(b->id); - AddView(w, v->id); + Window *window = CreateWindow(); + Buffer *buffer = CreateBuffer(sys_allocator, "*debug*"); + window->draw_line_numbers = false; + window->draw_scrollbar = false; + window->dont_save_in_active_window_history = true; + buffer->no_history = true; + View *view = CreateView(buffer->id); + AddView(window, view->id); } + { Window *w = CreateWindow(); w->draw_scrollbar = false; @@ -84,6 +91,7 @@ void LayoutWindows() { Rect2I screen_rect = GetScreenRectI(); Rect2I infobar_rect = CutBottom(&screen_rect, (Int)FontLineSpacing); float line_numbers_size = GetStringSize(&MainFont, L"1234567891").x; + double sizex = (double)GetSize(screen_rect).x; { int i = 5; Rect2I sr = screen_rect; @@ -94,21 +102,21 @@ void LayoutWindows() { } { int i = 0; - Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.33)); + Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)sizex * 0.46)); Windows[i].document_rect = Windows[i].total_rect; if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); } { int i = 1; - Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 0.5)); + Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)sizex * 0.46)); Windows[i].document_rect = Windows[i].total_rect; if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); } { int i = 2; - Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)GetSize(screen_rect).x * 1.0)); + Windows[i].total_rect = CutLeft(&screen_rect, (Int)((double)sizex)); Windows[i].document_rect = Windows[i].total_rect; if (Windows[i].draw_scrollbar) Windows[i].scrollbar_rect = CutRight(&Windows[i].document_rect, 10); if (Windows[i].draw_line_numbers) Windows[i].line_numbers_rect = CutLeft(&Windows[i].document_rect, (Int)line_numbers_size); diff --git a/src/text_editor/window_draw.cpp b/src/text_editor/window_draw.cpp index 1fa160e..eccb2f3 100644 --- a/src/text_editor/window_draw.cpp +++ b/src/text_editor/window_draw.cpp @@ -187,16 +187,13 @@ void DrawWindow(Window *window) { // Draw scrollbar if (window->draw_scrollbar) { SetScissor(window->scrollbar_rect); - // Vec2 mouse = GetMousePosition(); - // bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, ToRectangle(window->scrollbar_rect)); - DrawRect(window->scrollbar_rect, ColorScrollbarBackground); Scroller scroller = ComputeScrollerRect(window); Rect2 rect = Shrink(scroller.rect, 2); Color color = ColorScrollbarScroller; - // if (!window->mouse_selecting && (window->mouse_selecting_scrollbar || mouse_in_scrollbar)) { - // if (is_active) color = ColorScrollbarScrollerSelected; - // } + if (!window->mouse_selecting && (window->mouse_selecting_scrollbar || window->mouse_in_scrollbar)) { + if (is_active) color = ColorScrollbarScrollerSelected; + } DrawRect(rect, color); }