From f419ffd1ca3a14ed8195042244b4a3d11b24abac Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 6 Aug 2024 07:35:48 +0200 Subject: [PATCH] Add dirs to special buffers --- src/basic/win32.cpp | 4 +--- src/text_editor/buffer_multi_cursor.cpp | 2 +- src/text_editor/commands.cpp | 2 +- src/text_editor/commands_window.cpp | 8 ++++---- src/text_editor/management.cpp | 4 ++++ src/text_editor/title_bar.cpp | 5 ++++- src/text_editor/window.cpp | 22 +++++++++++++++------- 7 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/basic/win32.cpp b/src/basic/win32.cpp index 926e076..98f6b8a 100644 --- a/src/basic/win32.cpp +++ b/src/basic/win32.cpp @@ -304,9 +304,7 @@ String GetWorkingDir(Allocator arena) { wchar_t wbuffer[1024]; DWORD wsize = GetCurrentDirectoryW(Lengthof(wbuffer), wbuffer); Assert(wsize != 0); - Assert(wsize < 1022); - wbuffer[wsize++] = '/'; - wbuffer[wsize] = 0; + wbuffer[wsize] = 0; String path = ToString(arena, wbuffer, wsize); NormalizePathInPlace(path); diff --git a/src/text_editor/buffer_multi_cursor.cpp b/src/text_editor/buffer_multi_cursor.cpp index 1d3d97c..0c39642 100644 --- a/src/text_editor/buffer_multi_cursor.cpp +++ b/src/text_editor/buffer_multi_cursor.cpp @@ -137,7 +137,7 @@ void _ApplyEdits(Buffer *buffer, Array edits) { { Scratch scratch((Arena *)buffer->line_starts.allocator.object); Array edits_copy = TightCopy(scratch, edits); - MergeSort(edits.len, edits_copy.data, edits.data); + if (edits.len > 1) MergeSort(edits.len, edits_copy.data, edits.data); edits = edits_copy; } diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index a3e3845..b6a5320 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -459,7 +459,7 @@ View *FindView(BufferID buffer_id) { } void AppendToConsole(String16 string) { - Buffer *buffer = GetBuffer("*console*"); + Buffer *buffer = GetBuffer(ConsoleBufferID); // @todo: ? View *view = FindView(buffer->id); diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 85f9b88..817a548 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -265,7 +265,7 @@ Array GetSelectedLinesSorted(Allocator allocator, View *view) { Buffer *buffer = GetBuffer(view->active_buffer); Array caret_copy = TightCopy(scratch, view->carets); Array temp = TightCopy(scratch, view->carets); - MergeSort(view->carets.len, caret_copy.data, temp.data); + if (view->carets.len > 1) MergeSort(view->carets.len, caret_copy.data, temp.data); Array result = {allocator}; For(caret_copy) { @@ -487,7 +487,7 @@ void MergeCarets(View *view, Range *mouse_selection_anchor) { Scratch scratch; Array c1 = TightCopy(scratch, view->carets); - MergeSort(view->carets.len, c1.data, view->carets.data); + if (view->carets.len > 1) MergeSort(view->carets.len, c1.data, view->carets.data); view->carets.len = 0; Int first_caret_index = 0; @@ -737,13 +737,13 @@ void WindowCommand(Event event, Window *window, View *view) { } if (Ctrl(SDLK_F3)) { - Buffer *search_buffer = GetBuffer("*search*"); + Buffer *search_buffer = GetBuffer(SearchBufferID); String16 search_string = GetString(*search_buffer); Caret caret = FindInBuffer(buffer, search_string, view->carets[0], true); Insert(&view->carets, caret, 0); MergeCarets(view); } else if (Press(SDLK_F3)) { - Buffer *search_buffer = GetBuffer("*search*"); + Buffer *search_buffer = GetBuffer(SearchBufferID); String16 search_string = GetString(*search_buffer); Caret caret = FindInBuffer(buffer, search_string, view->carets[0], true); view->carets.len = 1; diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 7675664..a898e50 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -16,6 +16,10 @@ WindowID PopupWindowID; WindowID DebugWindowID; WindowID ConsoleWindowID; +BufferID ConsoleBufferID; +BufferID DebugBufferID; +BufferID SearchBufferID; + // @note: // Remember that WindowCommand works on window handed it down from HandleEvent // just because we don't have NextActiveWindow doesn't mean that we work on new diff --git a/src/text_editor/title_bar.cpp b/src/text_editor/title_bar.cpp index aa3943c..006fb8b 100644 --- a/src/text_editor/title_bar.cpp +++ b/src/text_editor/title_bar.cpp @@ -24,7 +24,7 @@ String DebugWindowList(Allocator allocator) { } void ReplaceDebugData() { - Buffer *buffer = GetBuffer("*debug*"); + Buffer *buffer = GetBuffer(DebugBufferID); Window *window = GetActiveWindow(); View *view = GetActiveView(window); @@ -44,6 +44,9 @@ void ReplaceDebugData() { Appendf(buffer, "mouse: [%f, %f]\n", xmouse, ymouse); Appendf(buffer, "window count: %d view count: %d buffer count: %d\n", (int)Windows.len, (int)Views.len, (int)Buffers.len); Appendf(buffer, "C:/Work/text_editor/src/text_editor/text_editor.cpp\n"); + Appendf(buffer, "working dir: %.*s\n", FmtString(WorkingDir)); + Appendf(buffer, "exe dir: %.*s\n", FmtString(ExeDir)); + Appendf(buffer, "config dir: %.*s\n", FmtString(ConfigDir)); // String view_list = DebugViewList(scratch); // Append(buffer, ToString16(scratch, view_list)); diff --git a/src/text_editor/window.cpp b/src/text_editor/window.cpp index 0291387..240ab7b 100644 --- a/src/text_editor/window.cpp +++ b/src/text_editor/window.cpp @@ -109,9 +109,14 @@ bool ToggleVisibility(Window *window) { return visible; } +String BuffCWD(String string) { + String result = Format(Perm, "%.*s/%.*s", FmtString(WorkingDir), FmtString(string)); + return result; +} + void InitScratchBuffer() { Allocator sys_allocator = GetSystemAllocator(); - Buffer *null_buffer = CreateBuffer(sys_allocator, "*scratch*"); + Buffer *null_buffer = CreateBuffer(sys_allocator, BuffCWD("*scratch*")); View *null_view = CreateView(null_buffer->id); } @@ -122,7 +127,7 @@ void InitWindows() { Window *window = CreateWindow(); window->is_column = true; // window->draw_line_numbers = false; - Buffer *buffer = CreateBuffer(sys_allocator, "*load_text_a*"); + Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("*load_text_a*")); View *view = CreateView(buffer->id); // LoadTextA(buffer); LoadUnicode(buffer); @@ -136,7 +141,7 @@ void InitWindows() { window->absolute_position = true; window->dont_save_in_active_window_history = true; - Buffer *buffer = CreateBuffer(sys_allocator, "*console*"); + Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("*console*")); // buffer->no_history = true; View *view = CreateView(buffer->id); window->active_view = view->id; @@ -145,12 +150,13 @@ void InitWindows() { SetVisibility(window, false); ConsoleWindowID = window->id; + ConsoleBufferID = buffer->id; } { Window *window = CreateWindow(); window->draw_line_numbers = false; - Buffer *buffer = CreateBuffer(sys_allocator, "*debug*"); + Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("*debug*")); window->absolute_position = true; window->draw_line_numbers = false; window->draw_scrollbar = false; @@ -164,6 +170,7 @@ void InitWindows() { titlebar->z = 2; SetVisibility(window, false); DebugWindowID = window->id; + DebugBufferID = buffer->id; } { @@ -175,7 +182,7 @@ void InitWindows() { w->absolute_position = true; w->dont_save_in_active_window_history = true; w->deactivate_on_escape = true; - Buffer *b = CreateBuffer(sys_allocator, "*commands*"); + Buffer *b = CreateBuffer(sys_allocator, BuffCWD("*commands*")); View *v = CreateView(b->id); v->fuzzy_search = true; @@ -198,13 +205,14 @@ void InitWindows() { w->dont_save_in_active_window_history = true; w->invisible_when_inactive = true; w->deactivate_on_escape = true; - Buffer *b = CreateBuffer(sys_allocator, "*search*"); + Buffer *b = CreateBuffer(sys_allocator, BuffCWD("*search*")); View *v = CreateView(b->id); w->active_view = v->id; CreateTitlebar(w); SetVisibility(w, false); + SearchBufferID = b->id; SearchWindowID = w->id; } @@ -218,7 +226,7 @@ void InitWindows() { w->absolute_position = true; w->deactivate_on_escape = true; w->z = 2; - Buffer *b = CreateBuffer(sys_allocator, "*popup*"); + Buffer *b = CreateBuffer(sys_allocator, BuffCWD("*popup*")); b->no_history = true; View *v = CreateView(b->id); w->active_view = v->id;