From d2ca6551785970af644055b1c15362283cd625dd Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 9 May 2025 09:17:45 +0200 Subject: [PATCH] Add metadata to Command_Open and move the logic that denies the NextGotoBuild to lua --- data/init.lua | 43 ++++++++++++++++++++++++++++------- src/text_editor/commands.cpp | 31 +++++++++++-------------- src/text_editor/generated.cpp | 43 ++++++++++++++++++++++++++++------- src/text_editor/text_editor.h | 4 ++-- src/text_editor/todo.txt | 1 + 5 files changed, 86 insertions(+), 36 deletions(-) diff --git a/data/init.lua b/data/init.lua index 1537f25..7ba3dd8 100644 --- a/data/init.lua +++ b/data/init.lua @@ -54,6 +54,15 @@ SDLK_X = 0x00000078 SDLK_Y = 0x00000079 SDLK_Z = 0x0000007a +function IsAlpha(a) + if a == nil then + return false + end + local x = string.byte(a) + local result = (x >= string.byte('a') and x <= string.byte('z')) or (x >= string.byte('A') and x <= string.byte('Z')) + return result +end + function SkipLineAndColumn(s) local line, col = "1", "1" @@ -163,7 +172,7 @@ function SkipPath(s) return s, path, drive, cells end -function MatchWindowsPath(_s) +function MatchWindowsPath(_s, meta) local s, file_path, drive = SkipPath(_s) if not file_path and FileExists(drive) then @@ -186,7 +195,7 @@ function MatchWindowsPath(_s) return {kind = "text", file_path = file_path, line = line, col = col} end -function MatchGitCommit(s) +function MatchGitCommit(s, meta) local i, j = string.find(s, "^commit ([a-zA-Z0-9]+)") if i then s = s:sub(8) @@ -196,7 +205,7 @@ function MatchGitCommit(s) return nil end -function MatchURL(s) +function MatchURL(s, meta) local i, j = string.find(s, "^https://") if i then return {kind = "exec_console", cmd = '"'..INTERNET_BROWSER..'" '..s, working_dir = GetMainDir()} @@ -204,16 +213,34 @@ function MatchURL(s) return nil end -Rules = { +function MatchGotoBuild(s, meta) + if meta ~= "goto_build" then + return nil + end + + local windows_path = IsAlpha(s:sub(1,1)) and s:sub(2,2) == ':' + local windows_rela = s:sub(1,1) == '.' and s:sub(2,2) == '\\' + local unix_path = s:sub(1,1) == '/' + local unix_rela = s:sub(1,1) == '.' and s:sub(2,2) == '/' + + if not windows_path and not windows_rela and not unix_path and not windows_rela then + return {kind = "skip"} + end + + return nil +end + +OnOpenMatchers = { MatchWindowsPath, MatchGitCommit, MatchURL, + MatchGotoBuild, } -function OnOpen(s) - for i = #Rules,1,-1 do - rule = Rules[i] - result = rule(s) +function OnOpen(path, meta) + for i = #OnOpenMatchers,1,-1 do + rule = OnOpenMatchers[i] + result = rule(path, meta) if result then return result end diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 4bf3de9..819a7c5 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -832,14 +832,7 @@ void Command_GotoNextInList(Window *window, Int line_offset = 1) { continue; } - bool windows_path = IsAlphabetic(line.data[0]) && line.data[1] == ':'; - bool unix_path = line.data[0] == '/'; - if (!windows_path && !unix_path) { - continue; - } - - CheckpointBeforeGoto(window, active_view); - BSet set = Command_Open(line); + BSet set = Command_Open(line, "goto_build"); if (set.window == NULL) { continue; } @@ -966,12 +959,13 @@ int Lua_C(lua_State *L) { return 0; } -BSet Command_Open(String path) { +BSet Command_Open(String path, String meta) { Scratch scratch; lua_getglobal(LuaState, "OnOpen"); lua_pushlstring(LuaState, path.data, path.len); - if (lua_pcall(LuaState, 1, 1, 0) != 0) { + lua_pushlstring(LuaState, meta.data, meta.len); + if (lua_pcall(LuaState, 2, 1, 0) != 0) { const char *error_message = lua_tostring(LuaState, -1); ReportWarningf("Failed the call to OnOpen! %s", error_message); lua_pop(LuaState, 1); @@ -979,8 +973,8 @@ BSet Command_Open(String path) { } BSet main = GetActiveMainSet(); - main.window->active_goto_list = main.view->id; - if (FieldString(LuaState, "kind") == "text") { + String kind = FieldString(LuaState, "kind"); + if (kind == "text") { String file_path = FieldString(LuaState, "file_path"); String line_string = FieldString(LuaState, "line"); Int line = strtoll(line_string.data, NULL, 10); @@ -1005,15 +999,17 @@ BSet Command_Open(String path) { } } UpdateScroll(main.window, true); - } else if (FieldString(LuaState, "kind") == "exec") { + } else if (kind == "exec") { String cmd = FieldString(LuaState, "cmd"); String working_dir = FieldString(LuaState, "working_dir"); Command_Exec(cmd, Command_GetMainDir()); - } else if (FieldString(LuaState, "kind") == "exec_console") { + } else if (kind == "exec_console") { // this shouldn't change the focus/window/view String cmd = FieldString(LuaState, "cmd"); String working_dir = FieldString(LuaState, "working_dir"); Exec(NullViewID, true, cmd, working_dir); + } else if (kind == "skip") { + return {}; } else { ReportWarningf("Failed to match any of OnOpen results!"); } @@ -1021,10 +1017,10 @@ BSet Command_Open(String path) { return main; } -BSet Command_Open(String16 path) { +BSet Command_Open(String16 path, String meta) { Scratch scratch; String string = ToString(scratch, path); - return Command_Open(string); + return Command_Open(string, meta); } int Lua_Open(lua_State *L) { @@ -1110,7 +1106,6 @@ int Lua_Eval(lua_State *L) { return 0; } - int Lua_SetProjectFile(lua_State *L) { BSet set = GetActiveMainSet(); LuaProjectBuffer = set.buffer; @@ -1143,4 +1138,4 @@ int Lua_GetBufferList(lua_State *L) { } /* We still have table left on top of the Lua stack. */ return 1; -} +} \ No newline at end of file diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index 5b338d9..b23bb1d 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -126,6 +126,15 @@ SDLK_X = 0x00000078 SDLK_Y = 0x00000079 SDLK_Z = 0x0000007a +function IsAlpha(a) + if a == nil then + return false + end + local x = string.byte(a) + local result = (x >= string.byte('a') and x <= string.byte('z')) or (x >= string.byte('A') and x <= string.byte('Z')) + return result +end + function SkipLineAndColumn(s) local line, col = "1", "1" @@ -235,7 +244,7 @@ function SkipPath(s) return s, path, drive, cells end -function MatchWindowsPath(_s) +function MatchWindowsPath(_s, meta) local s, file_path, drive = SkipPath(_s) if not file_path and FileExists(drive) then @@ -258,7 +267,7 @@ function MatchWindowsPath(_s) return {kind = "text", file_path = file_path, line = line, col = col} end -function MatchGitCommit(s) +function MatchGitCommit(s, meta) local i, j = string.find(s, "^commit ([a-zA-Z0-9]+)") if i then s = s:sub(8) @@ -268,7 +277,7 @@ function MatchGitCommit(s) return nil end -function MatchURL(s) +function MatchURL(s, meta) local i, j = string.find(s, "^https://") if i then return {kind = "exec_console", cmd = '"'..INTERNET_BROWSER..'" '..s, working_dir = GetMainDir()} @@ -276,16 +285,34 @@ function MatchURL(s) return nil end -Rules = { +function MatchGotoBuild(s, meta) + if meta ~= "goto_build" then + return nil + end + + local windows_path = IsAlpha(s:sub(1,1)) and s:sub(2,2) == ':' + local windows_rela = s:sub(1,1) == '.' and s:sub(2,2) == '\\' + local unix_path = s:sub(1,1) == '/' + local unix_rela = s:sub(1,1) == '.' and s:sub(2,2) == '/' + + if not windows_path and not windows_rela and not unix_path and not windows_rela then + return {kind = "skip"} + end + + return nil +end + +OnOpenMatchers = { MatchWindowsPath, MatchGitCommit, MatchURL, + MatchGotoBuild, } -function OnOpen(s) - for i = #Rules,1,-1 do - rule = Rules[i] - result = rule(s) +function OnOpen(path, meta) + for i = #OnOpenMatchers,1,-1 do + rule = OnOpenMatchers[i] + result = rule(path, meta) if result then return result end diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 74ea13e..a4ba82c 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -111,8 +111,8 @@ float DPIScale = 1.0f; Rect2I GetVisibleCells(Window *window); void AfterEdit(View *view, Array edits); Scroller ComputeScrollerRect(Window *window); -BSet Command_Open(String path); -BSet Command_Open(String16 path); +BSet Command_Open(String path, String meta = ""); +BSet Command_Open(String16 path, String meta = ""); void UpdateScroll(Window *window, bool update_caret_scrolling); void Command_SelectEntireBuffer(View *view); diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 9dc391d..f77b81e 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -9,6 +9,7 @@ - Delete directory/file on disk command - Create directory command (probably should enter it automatically - Convert more commands to taking buffer instead of view + - Add Command_Open that takes window/view as param - save all relevant buffers and build - shift down on last line should move the cursor to end of line!!! same for up