diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index b72ed38..1dcd850 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -3,27 +3,6 @@ https://code.visualstudio.com/blogs/2018/03/23/text-buffer-reimplementation */ #define BUFFER_DEBUG 0 -void InitBuffer(Allocator allocator, Buffer *buffer, String name, Int size = 4096) { - buffer->id = AllocBufferID(); - buffer->name = name; - buffer->cap = size; - buffer->data = AllocArray(allocator, U16, buffer->cap); - buffer->line_starts.allocator = allocator; - Add(&buffer->line_starts, (Int)0); -} - -Buffer *CreateBuffer(Allocator allocator, String name, Int size = 4096) { - Buffer *result = Alloc(&Buffers); - InitBuffer(allocator, result, name, size); - return result; -} - -Buffer *CreateTempBuffer(Allocator allocator, Int size = 4096) { - Buffer *result = AllocType(allocator, Buffer); - InitBuffer(allocator, result, "*temp*", size); - return result; -} - void Grow(Buffer *buffer, Int change_size) { Int new_size = buffer->len + change_size; if (new_size > buffer->cap) { diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 17e5090..c0408c9 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -436,9 +436,9 @@ void HandleActiveWindowBindings(Window *window, bool *update_scroll) { } if (AltPress(KEY_ENTER)) { Command_SelectAll(seek_view, needle); + SetActiveWindow(seek_window->id); } else if (Press(KEY_ENTER)) { Caret caret = FindInBuffer(seek_buffer, needle, seek_view->carets[0], true); - SetActiveWindow(seek_window->id); if (Ctrl()) { Insert(&seek_view->carets, caret, 0); MergeCarets(seek_view); @@ -512,8 +512,6 @@ void HandleActiveWindowBindings(Window *window, bool *update_scroll) { Insert(&view.carets, MakeCaret(p, p), 0); view.selection_anchor = view.carets[0].range; } - - MergeCarets(&view, &view.selection_anchor); } else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { window->mouse_selecting = true; } @@ -521,6 +519,7 @@ void HandleActiveWindowBindings(Window *window, bool *update_scroll) { if (window->mouse_selecting) { if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) window->mouse_selecting = false; + MergeCarets(&view, &view.selection_anchor); Caret &caret = view.carets[0]; if (view.selection_anchor.min > p) { caret = MakeCaret(p, view.selection_anchor.max); @@ -558,6 +557,7 @@ void HandleActiveWindowBindings(Window *window, bool *update_scroll) { } } } + MergeCarets(&view); } void HandleWindowBindings(Window *window) { diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 4fe5ddb..530c9e6 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -1,3 +1,28 @@ +WindowID WindowIDs = {0}; +BufferID BufferIDs = {0}; +ViewID ViewIDs = {0}; + +Array Buffers = {}; +Array Views = {}; +Array Windows = {}; + +WindowID NullWindowID = {0}; +BufferID NullBufferID = {0}; +ViewID NullViewID = {0}; +WindowID CommandWindowID = {0}; +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 +Int CaretChangeID; +Int LastFrameIDWhenScrolled = -1; +Int LastFrameIDWhenSwitchedActiveWindow; + +inline ViewID AllocViewID() { return {ViewIDs.id++}; } +inline WindowID AllocWindowID() { return {WindowIDs.id++}; } +inline BufferID AllocBufferID() { return {BufferIDs.id++}; } + inline Window *GetWindow(WindowID id) { For(Windows) if (it.id.id == id.id) return ⁢ return &Windows[0]; @@ -27,6 +52,27 @@ inline View *GetActiveView(Window *window) { else return GetView(window->active_view); } +void InitBuffer(Allocator allocator, Buffer *buffer, String name, Int size = 4096) { + buffer->id = AllocBufferID(); + buffer->name = name; + buffer->cap = size; + buffer->data = AllocArray(allocator, U16, buffer->cap); + buffer->line_starts.allocator = allocator; + Add(&buffer->line_starts, (Int)0); +} + +Buffer *CreateBuffer(Allocator allocator, String name, Int size = 4096) { + Buffer *result = Alloc(&Buffers); + InitBuffer(allocator, result, name, size); + return result; +} + +Buffer *CreateTempBuffer(Allocator allocator, Int size = 4096) { + Buffer *result = AllocType(allocator, Buffer); + InitBuffer(allocator, result, "*temp*", size); + return result; +} + Window *CreateWindow() { Window *w = Alloc(&Windows); w->visible = true; @@ -59,11 +105,12 @@ WindowID GetLastActiveWindow() { void SetActiveWindow(WindowID window) { if (window.id != ActiveWindow.id && LastFrameIDWhenSwitchedActiveWindow != FrameID) { - ActiveWindow = window; LastFrameIDWhenSwitchedActiveWindow = FrameID; - - Window *w = GetWindow(window); - if (!w->dont_save_in_active_window_history) Add(&WindowSwitchHistory, window); + ActiveWindow = window; + Window *w = GetWindow(window); + if (!w->dont_save_in_active_window_history) { + Add(&WindowSwitchHistory, window); + } } } diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 66d74f1..0d30bfd 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -86,21 +86,6 @@ struct Scroller { Int line_count; }; -WindowID WindowIDs = {0}; -BufferID BufferIDs = {0}; -ViewID ViewIDs = {0}; - -WindowID NullWindowID = {0}; -BufferID NullBufferID = {0}; -ViewID NullViewID = {0}; -WindowID CommandWindowID = {0}; -WindowID InfoBarWindowID = {0}; -WindowID SearchWindowID = {0}; - -Array Buffers = {}; -Array Views = {}; -Array Windows = {}; - float MenuFontSize; Font MenuFont; @@ -111,21 +96,13 @@ Int FontLineSpacing; Int FontCharSpacing; Int FrameID; -Int LastFrameIDWhenScrolled = -1; -Int CaretChangeID; -String WorkingDir; -Int LastFrameIDWhenSwitchedActiveWindow; -Array WindowSwitchHistory; // @todo: probably better as a circular buffer -WindowID ActiveWindow; // @todo: maybe NextActiveWindow and change at end of frame +String WorkingDir; -String16 EvalString(Allocator allocator, String16 string16); -Rect2I GetVisibleCells(Window &window); -void AfterEdit(View *view, Array edits); -Scroller ComputeScrollerRect(Window &window); -void Command_EvalLua(View *view, String16 string); -void MergeCarets(View *view, Range *mouse_selection_anchor = NULL); - -inline ViewID AllocViewID() { return {ViewIDs.id++}; } -inline WindowID AllocWindowID() { return {WindowIDs.id++}; } -inline BufferID AllocBufferID() { return {BufferIDs.id++}; } \ No newline at end of file +String16 EvalString(Allocator allocator, String16 string16); +Rect2I GetVisibleCells(Window &window); +void AfterEdit(View *view, Array edits); +Scroller ComputeScrollerRect(Window &window); +void Command_EvalLua(View *view, String16 string); +void MergeCarets(View *view, Range *mouse_selection_anchor = NULL); +inline BufferID AllocBufferID(); \ No newline at end of file