diff --git a/src/text_editor/buffer_helpers.cpp b/src/text_editor/buffer_helpers.cpp index b280cf4..31a942b 100644 --- a/src/text_editor/buffer_helpers.cpp +++ b/src/text_editor/buffer_helpers.cpp @@ -476,7 +476,29 @@ Int FindScopeEnd(Buffer *buffer, Int seek, Int max_seek, wchar_t open, wchar_t c return seek; } +Range EncloseScope(Buffer *buffer, Int pos, wchar_t open, wchar_t close) { + Range result = {pos, pos}; + for (Int i = pos; i >= 0; i -= 1) { + if (buffer->str[i] == open) { + result.min = i; + break; + } + } + for (Int i = pos; i < buffer->len; i += 1) { + if (buffer->str[i] == close) { + result.max = i + 1; + break; + } + } + return result; +} Range EncloseExecWord(Buffer *buffer, Int pos) { + for (Int i = pos; i >= 0 && i >= pos - 1024; i -= 1) { + if (GetChar(buffer, i) == L'#') { + pos = i + 1; + break; + } + } Range result = {GetWordStart(buffer, pos), GetWordEnd(buffer, pos)}; Int seek = SkipSpaces(buffer, result.max); @@ -539,20 +561,3 @@ Range GetIndentRangeAtPos(Buffer *buffer, Int pos) { Range range = GetLineRangeWithoutNL(*buffer, line); return {range.min, range.min + indent}; } - -Range EncloseScope(Buffer *buffer, Int pos, wchar_t open, wchar_t close) { - Range result = {pos, pos}; - for (Int i = pos; i >= 0; i -= 1) { - if (buffer->str[i] == open) { - result.min = i; - break; - } - } - for (Int i = pos; i < buffer->len; i += 1) { - if (buffer->str[i] == close) { - result.max = i + 1; - break; - } - } - return result; -} \ No newline at end of file diff --git a/src/text_editor/buffer_history.cpp b/src/text_editor/buffer_history.cpp index a40f9b9..fe32d7d 100644 --- a/src/text_editor/buffer_history.cpp +++ b/src/text_editor/buffer_history.cpp @@ -113,6 +113,12 @@ void PreBeginEdit_SaveCaretHistory(Buffer *buffer, Array &carets) { BeginEdit({}, buffer, carets); } +void AssertRanges(Array carets) { + For(carets) { + Assert(it.range.max >= it.range.min); + } +} + void AdjustCarets(Array edits, Array *carets, bool kill_selection = false) { Scratch scratch; Array new_carets = TightCopy(scratch, *carets); @@ -146,11 +152,7 @@ void AdjustCarets(Array edits, Array *carets, bool kill_selection = } } -#if DEBUG_BUILD - For(*carets) { - Assert(it.range.max >= it.range.min); - } -#endif + IF_DEBUG(AssertRanges(*carets)); } bool KILL_SELECTION = true; diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 702fbd0..492784d 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -584,9 +584,12 @@ void Command_Find(View *seek_view, String16 needle, bool forward = true) { if (!forward) caret = FindPrev(seek_buffer, needle, caret); seek_view->carets.len = 1; seek_view->carets[0] = caret; + + IF_DEBUG(AssertRanges(seek_view->carets)); } void Command_GotoNextInList(Window *window, Int line_offset = 1) { + Assert(line_offset == 1 || line_offset == -1); ViewID active_view = window->active_view; CheckpointBeforeGoto(window->id); @@ -596,14 +599,24 @@ void Command_GotoNextInList(Window *window, Int line_offset = 1) { Buffer *buffer_goto = GetBuffer(view_goto->active_buffer); int64_t pos = GetFront(view_goto->carets[0]); Int line = PosToLine(*buffer_goto, pos); - if (line + line_offset < buffer_goto->line_starts.len) { + + bool opened = false; + for (Int i = line + line_offset; line + line_offset < buffer_goto->line_starts.len && line + line_offset >= 0; i += line_offset) { Range line_range = GetLineRangeWithoutNL(*buffer_goto, line + line_offset); String16 line = GetString(*buffer_goto, line_range); view_goto->carets[0] = MakeCaret(line_range.min); + line = Trim(line); + + MergeCarets(view_goto); + IF_DEBUG(AssertRanges(view_goto->carets)); + if (line.len == 0) continue; + Open(line); - } else { - window->active_view = active_view; + opened = true; + break; } + + if (!opened) window->active_view = active_view; } void WindowCommand(Event event, Window *window, View *view) { @@ -787,12 +800,17 @@ void WindowCommand(Event event, Window *window, View *view) { Insert(&view->carets, caret, 0); MergeCarets(view); } else if (Press(SDLK_F3)) { - // Scratch scratch; - // String16 search_string = ToString16(scratch, window->search_string); - // Caret caret = FindNext(buffer, search_string, view->carets[0]); - // view->carets.len = 1; - // view->carets[0] = caret; - Command_GotoNextInList(window); + Scratch scratch; + String16 search_string = ToString16(scratch, window->search_string); + Caret caret = FindNext(buffer, search_string, view->carets[0]); + view->carets.len = 1; + view->carets[0] = caret; + } + + if (Shift(SDLK_F4)) { + Command_GotoNextInList(window, -1); + } else if (Press(SDLK_F4)) { + Command_GotoNextInList(window, 1); } if (view->fuzzy_search && search) { @@ -863,9 +881,17 @@ void WindowCommand(Event event, Window *window, View *view) { } } + if (Ctrl(SDLK_PERIOD)) { + Window *window = GetWindow(GetLastActiveWindow()); + View *view = GetView(window->active_view); + Buffer *buffer = GetBuffer(view->active_buffer); + + String name = ChopLastSlash(buffer->name); + Open(name); + } + if (CtrlShift(SDLK_G)) { - Window *titlebar = window; - if (!window->is_title_bar) titlebar = GetWindow(window->title_bar_window); + Window *titlebar = GetTitlebarWindow(window->id); View *titlebar_view = GetView(titlebar->active_view); Buffer *titlebar_buffer = GetBuffer(titlebar_view->active_buffer); SetActiveWindow(titlebar->id); @@ -883,8 +909,7 @@ void WindowCommand(Event event, Window *window, View *view) { } } } else if (Ctrl(SDLK_G)) { - Window *titlebar = window; - if (!window->is_title_bar) titlebar = GetWindow(window->title_bar_window); + Window *titlebar = GetTitlebarWindow(window->id); View *titlebar_view = GetView(titlebar->active_view); Buffer *titlebar_buffer = GetBuffer(titlebar_view->active_buffer); SetActiveWindow(titlebar->id); @@ -912,6 +937,7 @@ void WindowCommand(Event event, Window *window, View *view) { } else if (Alt(SDLK_Q)) { GotoBackward(GetLastActiveWindow()); } + IF_DEBUG(AssertRanges(view->carets)); } void UpdateScroll(Window *window, bool update_caret_scrolling) { diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 1ad6d47..15ee68e 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -169,6 +169,13 @@ View *FindViewWithBufferName(String name) { return NULL; } +Window *GetTitlebarWindow(WindowID id) { + Window *window = GetWindow(id); + if (!window->is_title_bar) window = GetWindow(window->title_bar_window); + Assert(window->is_title_bar); + return window; +} + Window *GetCurrentWindow() { Window *window = GetWindow(ActiveWindow); if (window->is_title_bar) { diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index aefb14a..390a84e 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -135,4 +135,10 @@ void Command_Append(ViewID view_id, String16 string, bool scroll_to_end_if_curso void Command_Append(ViewID view_id, String string, bool scroll_to_end_if_cursor_on_last_line); void ReportErrorf(const char *fmt, ...); -void ReportWarningf(const char *fmt, ...); \ No newline at end of file +void ReportWarningf(const char *fmt, ...); + +#if DEBUG_BUILD + #define IF_DEBUG(x) x +#else + #define IF_DEBUG(x) +#endif \ No newline at end of file diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index bc7847b..5becfee 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,11 +1,17 @@ - Remove pointers and use ViewIDs (enable array debug while doing this) +- Remove console and command window, provide alternatives but unify the interface? + +- Ctrl + . to chop a path from buffer name and open that +- apply clang format +- apply clang format on save +- OnWindowCommand allow config user to overwrite the WindowCommand keybinding, introduce his own +- How to remove memory issues? Maybe just store ids and pointers in arrays, rest should be allocated off of arena. Then maybe garbage collected and reclaimed. - ctrl + f - should find Search and select content or add Search - some split selection commands - assign commands or lua functions to F1-F8 keys - generate the lua function table for all functions prefixed with Lua_ -- Cycle up and down through a list of filenames and jump to every filename listed and remember the last buffer which we jumped from which contains the list - A lister which is going to show project without the full path and sorted by recency - Fix fuzzy search look