From c9aabdec6866045450437ba82b646eb8ae9cc624 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 5 Aug 2024 08:25:33 +0200 Subject: [PATCH] Experimenting with execution --- build_file.cpp | 2 +- src/text_editor/buffer_fuzzy_search.cpp | 4 +- src/text_editor/commands.cpp | 25 +++++- src/text_editor/commands_window.cpp | 25 ++---- src/text_editor/generated.cpp | 2 +- src/text_editor/lua_api.cpp | 108 +++++++++++++++--------- src/text_editor/todo.txt | 3 +- 7 files changed, 103 insertions(+), 66 deletions(-) diff --git a/build_file.cpp b/build_file.cpp index aef1337..d47f4bf 100644 --- a/build_file.cpp +++ b/build_file.cpp @@ -257,7 +257,7 @@ function ExtractLineAndColumn(s) end function BufferNameExists(name) - buffers = ListBuffers() + buffers = GetBufferList() for i = 1,#buffers do buff = buffers[i] if buff == name then diff --git a/src/text_editor/buffer_fuzzy_search.cpp b/src/text_editor/buffer_fuzzy_search.cpp index d71dfcd..2f5dafd 100644 --- a/src/text_editor/buffer_fuzzy_search.cpp +++ b/src/text_editor/buffer_fuzzy_search.cpp @@ -29,8 +29,8 @@ int64_t FuzzyRate(String16 string, String16 with) { } Array FuzzySearchLines(Allocator allocator, Buffer *buffer, Int line_min, Int line_max, String16 needle) { - Assert(line_min >= 0 && line_min < buffer->line_starts.len); - Assert(line_max >= 0 && line_max <= buffer->line_starts.len); + if (line_min < 0 || line_min >= buffer->line_starts.len) return {}; + if (line_max < 0 || line_min > buffer->line_starts.len) return {}; Array ratings = {allocator}; for (Int i = line_min; i < line_max; i += 1) { String16 s = GetLineStringWithoutNL(*buffer, i); diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index b6a6018..3a6b58c 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -281,6 +281,7 @@ bool GlobalCommand(Event event) { if (event.ctrl && Mouse(RIGHT)) { GoBackToLastCrumb(); + } else if (event.alt && Mouse(RIGHT)) { } // @todo: maybe move some of this stuff to window command ??? @@ -288,6 +289,23 @@ bool GlobalCommand(Event event) { // - maybe just do the check if active window is matching the DocumentSelected window // - if scrollbar selected then don't invoke window command if (event.alt && Mouse(LEFT)) { + Vec2I mouse = MouseVec2I(); + Window *window = GetActiveWindow(); + + bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect); + if (mouse_in_document) { + View *view = GetView(window->active_view); + Buffer *buffer = GetBuffer(view->active_buffer); + Int p = ScreenSpaceToBufferPosErrorOutOfBounds(window, view, buffer, mouse); + if (p != -1) { + Range enclose = EncloseExecWord(buffer, p); + String16 string = GetString(*buffer, enclose); + + // Window *last_window = GetWindow(GetLastActiveWindow()); + // View *last_view = GetView(last_window->active_view); + Command_EvalLua(view, string); + } + } } else if (event.ctrl && Mouse(LEFT)) { Vec2I mouse = MouseVec2I(); Window *window = GetActiveWindow(); @@ -298,10 +316,11 @@ bool GlobalCommand(Event event) { Buffer *buffer = GetBuffer(view->active_buffer); Int p = ScreenSpaceToBufferPosErrorOutOfBounds(window, view, buffer, mouse); if (p != -1) { - view->carets.len = 1; - view->carets[0] = MakeCaret(p); Range enclose = EncloseLoadWord(buffer, p); String16 string = GetString(*buffer, enclose); + + view->carets.len = 1; + view->carets[0] = MakeCaret(p); Open(string); } } @@ -408,8 +427,8 @@ bool GlobalCommand(Event event) { SetActiveWindow(GetLastActiveWindow()); } else { View *view = GetView(command_window->active_view); - Command_EvalLua(view, L"list_buffers()"); SetActiveWindow(command_window->id); + Command_EvalLua(view, L"ListBuffers()"); } run_window_command = false; } diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index ae60822..8b80bd9 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -426,26 +426,6 @@ void Command_SelectEntireBuffer(View *view) { view->carets[0] = MakeCaret(0, buffer->len); } -void Command_EvalLua(View *view, String16 string) { - Scratch scratch; - Buffer *buffer = GetBuffer(view->active_buffer); - String16 eval_result = EvalString(scratch, string); - - if (eval_result.len) { - Command_SelectEntireBuffer(view); - Command_Replace(view, eval_result); - Command_SelectRangeOneCursor(view, {}); - Command_Replace(view, L"\n"); - Command_SelectRangeOneCursor(view, {}); - } - - else { - Range range = GetLineRangeWithoutNL(*buffer, 0); - Command_SelectRangeOneCursor(view, range); - Command_Replace(view, {}); - } -} - // Merge carets that overlap, this needs to be handled before any edits to // make sure overlapping edits won't happen. // @@ -816,6 +796,11 @@ void WindowCommand(Event event, Window *window, View *view) { Range enclose = EncloseLoadWord(buffer, p); String16 string = GetString(*buffer, enclose); Open(string); + } else if (Alt(SDLK_Q)) { + Int p = GetFront(view->carets[0]); + Range enclose = EncloseExecWord(buffer, p); + String16 string = GetString(*buffer, enclose); + Command_EvalLua(view, string); } if (Ctrl(SDLK_W)) { diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index 52f5da0..4222a27 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -127,7 +127,7 @@ function ExtractLineAndColumn(s) end function BufferNameExists(name) - buffers = ListBuffers() + buffers = GetBufferList() for i = 1,#buffers do buff = buffers[i] if buff == name then diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 4862fbe..857f9ec 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -84,19 +84,29 @@ int LuaPrint(lua_State *L) { return 0; } -int LuaListOpenBuffers(lua_State *L) { +int LuaListBuffers(lua_State *L) { + Window *window = GetWindow(ActiveWindow); + View *view = GetView(window->active_view); + Scratch scratch; Array strings = {scratch}; For(Buffers) { String string = Format(scratch, "%.*s", FmtString(it.name)); Add(&strings, string); } - String result = Merge(scratch, strings, "\n"); - lua_pushlstring(LuaState, result.data, result.len); - return 1; + String result = Merge(scratch, strings, "\n"); + String16 string16 = ToString16(scratch, result); + + Command_SelectEntireBuffer(view); + Command_Replace(view, string16); + Command_SelectRangeOneCursor(view, {}); + Command_Replace(view, L"\n"); + Command_SelectRangeOneCursor(view, {}); + + return 0; } -int LuaListOpenBuffersObject(lua_State *L) { +int LuaGetBufferList(lua_State *L) { lua_createtable(L, 0, (int)Buffers.len); int i = 1; @@ -148,16 +158,16 @@ int LuaGetCurrentBufferDir(lua_State *L) { } luaL_Reg LuaFunctions[] = { - { "Open", LuaOpen}, - { "list_buffers", LuaListOpenBuffers}, - { "ListBuffers", LuaListOpenBuffersObject}, - { "GetWorkingDir", LuaGetWorkingDir}, - { "FileExists", LuaFileExists}, - {"GetCurrentBufferName", LuaGetCurrentBufferName}, - { "GetCurrentBufferDir", LuaGetCurrentBufferDir}, - { "print", LuaPrint}, - { "Print", LuaPrint}, - { NULL, NULL}, + { "Open", LuaOpen}, + { "ListBuffers", LuaListBuffers}, + { "GetBufferList", LuaGetBufferList}, + { "GetWorkingDir", LuaGetWorkingDir}, + { "FileExists", LuaFileExists}, + {"GetCurrentBufferName", LuaGetCurrentBufferName}, + { "GetCurrentBufferDir", LuaGetCurrentBufferDir}, + { "print", LuaPrint}, + { "Print", LuaPrint}, + { NULL, NULL}, }; void InitLua() { @@ -170,28 +180,6 @@ void InitLua() { } } -String16 EvalString(Allocator allocator, String16 string16) { - Scratch scratch((Arena *)allocator.object); - String string = ToString(scratch, string16); - string = Format(scratch, "return %.*s", FmtString(string)); - if (luaL_dostring(LuaState, string.data) != LUA_OK) { - const char *error_message = lua_tostring(LuaState, -1); - ReportWarningf("Failed to load base lua config! %s", error_message); - lua_pop(LuaState, 1); - return {}; - } - - const char *text = lua_tostring(LuaState, -1); - if (text) { - String s = text; - String16 result = ToString16(allocator, s); - lua_pop(LuaState, 1); - return result; - } else { - return {}; - } -} - Int GetStyle(String name, Int default_int) { Int result = default_int; lua_getglobal(LuaState, "Style"); @@ -284,4 +272,48 @@ void ReloadLuaConfig() { } LuaBufferChangeID = LuaBuffer->change_id; } -} \ No newline at end of file +} + +// + +String16 EvalString(Allocator allocator, String16 string16) { + Scratch scratch((Arena *)allocator.object); + String string = ToString(scratch, string16); + string = Format(scratch, "return %.*s", FmtString(string)); + if (luaL_dostring(LuaState, string.data) != LUA_OK) { + const char *error_message = lua_tostring(LuaState, -1); + ReportWarningf("Execution error! %s", error_message); + lua_pop(LuaState, 1); + return {}; + } + + const char *text = lua_tostring(LuaState, -1); + if (text) { + String s = text; + String16 result = ToString16(allocator, s); + lua_pop(LuaState, 1); + return result; + } else { + return {}; + } +} + +void Command_EvalLua(View *view, String16 string) { + Scratch scratch; + Buffer *buffer = GetBuffer(view->active_buffer); + String16 eval_result = EvalString(scratch, string); + + if (eval_result.len) { + Command_SelectEntireBuffer(view); + Command_Replace(view, eval_result); + Command_SelectRangeOneCursor(view, {}); + Command_Replace(view, L"\n"); + Command_SelectRangeOneCursor(view, {}); + } + + else { + Range range = GetLineRangeWithoutNL(*buffer, 0); + Command_SelectRangeOneCursor(view, range); + Command_Replace(view, {}); + } +} diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index d7145df..9bd7905 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -2,7 +2,7 @@ - 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 -- mouse execute +- mouse execute - alt right click what to do ? (toggle console?) - experiment with using multiple cursors to select command and it's input - should be able click on title bar of windows which disappear on losing focus @@ -13,6 +13,7 @@ - each buffer needs a directory even the special ones: C:\a\b\c\+errors? - open directories - resulting in buffer with dir listing and proper buffer name +- alt/ctrl + double click should select the exec or load word - clean \r\n into \n on trim and load - baked font as fallback