From 00c1f405c61bf111ce4496389d43bec435a7e735 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 9 Aug 2024 10:36:33 +0200 Subject: [PATCH] GotoForward --- src/text_editor/commands.cpp | 41 +++++++++++++++++++++++------ src/text_editor/commands_window.cpp | 4 ++- src/text_editor/lua_api.cpp | 23 +++++++++++++--- src/text_editor/management.cpp | 9 +++---- src/text_editor/notes_bindings | 2 +- src/text_editor/text_editor.h | 13 +++++---- src/text_editor/todo.txt | 3 ++- 7 files changed, 71 insertions(+), 24 deletions(-) diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 9211cb6..9c6b35d 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -89,17 +89,40 @@ void ToggleConsole() { } } -void CheckpointBeforeGoto() { - Window *window = GetWindow(GetLastActiveWindow()); +void CheckpointBeforeGoto(WindowID window_id) { + Window *window = GetWindow(window_id); View *view = GetView(window->active_view); Buffer *buffer = GetBuffer(view->active_buffer); - Add(&GotoCrumbs, {buffer->id, view->carets[0]}); + Add(&window->goto_history, {buffer->id, view->carets[0]}); + window->goto_redo.len = 0; } -void GoBackToLastCrumb() { - Window *window = GetWindow(GetLastActiveWindow()); - if (GotoCrumbs.len <= 0) return; - GotoCrumb c = Pop(&GotoCrumbs); +void GotoBackward(WindowID window_id) { + Window *window = GetWindow(window_id); + if (window->goto_history.len <= 0) return; + { + Window *window = GetWindow(window_id); + View *view = GetView(window->active_view); + Buffer *buffer = GetBuffer(view->active_buffer); + Add(&window->goto_redo, {buffer->id, view->carets[0]}); + } + GotoCrumb c = Pop(&window->goto_history); + Buffer *buffer = GetBuffer(c.buffer_id); + View *view = WindowOpenBufferView(window, buffer->name); + view->carets[0] = c.caret; + UpdateScroll(window, true); +} + +void GotoForward(WindowID window_id) { + Window *window = GetWindow(window_id); + if (window->goto_redo.len <= 0) return; + { + Window *window = GetWindow(window_id); + View *view = GetView(window->active_view); + Buffer *buffer = GetBuffer(view->active_buffer); + Add(&window->goto_history, {buffer->id, view->carets[0]}); + } + GotoCrumb c = Pop(&window->goto_redo); Buffer *buffer = GetBuffer(c.buffer_id); View *view = WindowOpenBufferView(window, buffer->name); view->carets[0] = c.caret; @@ -289,10 +312,12 @@ bool GlobalCommand(Event event) { if (event.ctrl && event.shift && Mouse(RIGHT)) { MouseExecWord(event); + } else if (event.alt && event.ctrl && Mouse(RIGHT)) { + GotoForward(GetLastActiveWindow()); } else if (event.ctrl && Mouse(RIGHT)) { MouseLoadWord(event); } else if (event.alt && Mouse(RIGHT)) { - GoBackToLastCrumb(); + GotoBackward(GetLastActiveWindow()); } else if (Mouse(RIGHT)) { Vec2I mouse = MouseVec2I(); Window *window = GetActiveWindow(); diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 9e81636..e83ace3 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -896,6 +896,8 @@ void WindowCommand(Event event, Window *window, View *view) { String16 string = GetString(*buffer, range); Command_EvalLua(view, string); + } else if (CtrlAlt(SDLK_Q)) { + GotoForward(GetLastActiveWindow()); } else if (Ctrl(SDLK_Q)) { Caret caret = view->carets[0]; Range range = caret.range; @@ -904,7 +906,7 @@ void WindowCommand(Event event, Window *window, View *view) { Open(string); } else if (Alt(SDLK_Q)) { - GoBackToLastCrumb(); + GotoBackward(GetLastActiveWindow()); } } diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index e1f0978..359f00f 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -37,7 +37,7 @@ String GetUniqueBufferName(Allocator allocator, String working_dir, String p void ExecInNewBuffer(String cmd, String working_dir) { Scratch scratch; - CheckpointBeforeGoto(); + CheckpointBeforeGoto(GetLastActiveWindow()); String buffer_name = GetUniqueBufferName(scratch, working_dir, "+CMD"); Window *window = GetWindow(GetLastActiveWindow()); View *view = WindowOpenBufferView(window, buffer_name); @@ -66,8 +66,9 @@ void Open(String path) { String col_string = FieldString(LuaState, "col"); Int col = strtoll(col_string.data, NULL, 10); - CheckpointBeforeGoto(); - Window *window = GetWindow(GetLastActiveWindow()); + WindowID window_id = GetLastActiveWindow(); + CheckpointBeforeGoto(window_id); + Window *window = GetWindow(window_id); View *view = WindowOpenBufferView(window, file_path); Buffer *buffer = GetBuffer(view->active_buffer); if (line != -1 && col != -1) { @@ -192,6 +193,21 @@ int LuaGetCurrentBufferDir(lua_State *L) { return 1; } +void OpenFilesHereRecursive(Window *window, String path) { + Scratch scratch; +} + +int LuaOpenFilesHere(lua_State *L) { + Window *window = GetWindow(GetLastActiveWindow()); + + Scratch scratch; + for (FileIter it = IterateFiles(scratch, GetCurrentBufferDir()); IsValid(it); Advance(&it)) { + WindowOpenBufferView(window, it.absolute_path); + } + + return 0; +} + static void HookLuaForceExit(lua_State *L, lua_Debug *debug) { SDL_PumpEvents(); int numkeys = 0; @@ -213,6 +229,7 @@ luaL_Reg LuaFunctions[] = { { "Kill", LuaKill}, { "NewC", LuaNewCmd}, { "AppendC", LuaAppendCmd}, + { "OpenFilesHere", LuaOpenFilesHere}, { NULL, NULL}, }; diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 8c2a7f1..0cdb4f9 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -24,10 +24,9 @@ BufferID SearchBufferID; // 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 // window all of a sudden in that function call! -WindowID ActiveWindow; -Array WindowSwitchHistory; // @todo: probably better as a circular buffer -Array GotoCrumbs; -Int CaretChangeID; +WindowID ActiveWindow; +Array WindowSwitchHistory; // @todo: probably better as a circular buffer +Int CaretChangeID; WindowID ScrollbarSelected = {-1}; WindowID DocumentSelected = {-1}; @@ -267,7 +266,7 @@ Buffer *BufferOpenFile(String path) { path = GetAbsolutePath(scratch, path); path = Intern(&GlobalInternTable, path); String string = ReadFile(scratch, path); - buffer = CreateBuffer(sys_allocator, path, string.len * 4); + buffer = CreateBuffer(sys_allocator, path, string.len * 4 + 4096); buffer->len = ConvertUTF8ToUTF16UnixLine(string, buffer->str, buffer->cap); UpdateLines(buffer, {}, String16{(wchar_t *)buffer->data, buffer->len}); } diff --git a/src/text_editor/notes_bindings b/src/text_editor/notes_bindings index d9628cb..4161f64 100644 --- a/src/text_editor/notes_bindings +++ b/src/text_editor/notes_bindings @@ -14,6 +14,6 @@ alt mright - go back ctrl mright - load word shift mright - alt shift mright - -alt ctrl mright - +alt ctrl mright - go forward ctrl shift mright - exec word mmiddle - diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index aa1c0a4..11cb854 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -57,6 +57,11 @@ struct View { }; }; +struct GotoCrumb { + BufferID buffer_id; + Caret caret; +}; + struct Window { WindowID id; ViewID active_view; @@ -69,6 +74,9 @@ struct Window { Rect2I line_numbers_rect; Rect2I document_rect; + Array goto_history; + Array goto_redo; + double mouse_scroller_offset; int z; @@ -94,11 +102,6 @@ struct Scroller { Int line_count; }; -struct GotoCrumb { - BufferID buffer_id; - Caret caret; -}; - // @WARNING: be careful about using this, should only be used for debugging // the problem with this is that we want events to be reproducible. // We eat as many events as we can in a frame, we abstract the frame and so on. diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 76a5359..66ff3fc 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,3 +1,4 @@ +- jump history forward - Remove pointers and use ViewIDs (enable array debug while doing this) - try using git grep for search for now, combine with fuzzy search buffer - lua maybe try heuristic matching paths from left? @@ -32,6 +33,7 @@ Windows +- better teminology then 'active' 'last active' 'current' - switch to previous view (ctrl + tab) - shift + ctrl + click should open a new window and then with alt it probably should kill it - layout using a tree!! @@ -48,7 +50,6 @@ backlog - drop text into window - page up and down should also scroll and leave you in exactly same scroll - I think the way sublime text and we display line highlights is confusing with multiple cursors (line highlight can be confused with selection) -- ctrl + delete maybe should stop on new line but it keeps on going, sublime is much more careful with deleting - text_editor --record events.txt text_editor --playback events.txt - make the editor replayable, store events and then replay, be careful about globals - maybe open should return multiple options if there are many more? (like in sublime if many symbols you get a window and you choose and it automatically jumps you to the symbol in the background)