Add metadata to Command_Open and move the logic that denies the NextGotoBuild to lua

This commit is contained in:
Krzosa Karol
2025-05-09 09:17:45 +02:00
parent c57eedce49
commit d2ca655178
5 changed files with 86 additions and 36 deletions

View File

@@ -54,6 +54,15 @@ SDLK_X = 0x00000078
SDLK_Y = 0x00000079 SDLK_Y = 0x00000079
SDLK_Z = 0x0000007a 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) function SkipLineAndColumn(s)
local line, col = "1", "1" local line, col = "1", "1"
@@ -163,7 +172,7 @@ function SkipPath(s)
return s, path, drive, cells return s, path, drive, cells
end end
function MatchWindowsPath(_s) function MatchWindowsPath(_s, meta)
local s, file_path, drive = SkipPath(_s) local s, file_path, drive = SkipPath(_s)
if not file_path and FileExists(drive) then 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} return {kind = "text", file_path = file_path, line = line, col = col}
end end
function MatchGitCommit(s) function MatchGitCommit(s, meta)
local i, j = string.find(s, "^commit ([a-zA-Z0-9]+)") local i, j = string.find(s, "^commit ([a-zA-Z0-9]+)")
if i then if i then
s = s:sub(8) s = s:sub(8)
@@ -196,7 +205,7 @@ function MatchGitCommit(s)
return nil return nil
end end
function MatchURL(s) function MatchURL(s, meta)
local i, j = string.find(s, "^https://") local i, j = string.find(s, "^https://")
if i then if i then
return {kind = "exec_console", cmd = '"'..INTERNET_BROWSER..'" '..s, working_dir = GetMainDir()} return {kind = "exec_console", cmd = '"'..INTERNET_BROWSER..'" '..s, working_dir = GetMainDir()}
@@ -204,16 +213,34 @@ function MatchURL(s)
return nil return nil
end 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, MatchWindowsPath,
MatchGitCommit, MatchGitCommit,
MatchURL, MatchURL,
MatchGotoBuild,
} }
function OnOpen(s) function OnOpen(path, meta)
for i = #Rules,1,-1 do for i = #OnOpenMatchers,1,-1 do
rule = Rules[i] rule = OnOpenMatchers[i]
result = rule(s) result = rule(path, meta)
if result then if result then
return result return result
end end

View File

@@ -832,14 +832,7 @@ void Command_GotoNextInList(Window *window, Int line_offset = 1) {
continue; continue;
} }
bool windows_path = IsAlphabetic(line.data[0]) && line.data[1] == ':'; BSet set = Command_Open(line, "goto_build");
bool unix_path = line.data[0] == '/';
if (!windows_path && !unix_path) {
continue;
}
CheckpointBeforeGoto(window, active_view);
BSet set = Command_Open(line);
if (set.window == NULL) { if (set.window == NULL) {
continue; continue;
} }
@@ -966,12 +959,13 @@ int Lua_C(lua_State *L) {
return 0; return 0;
} }
BSet Command_Open(String path) { BSet Command_Open(String path, String meta) {
Scratch scratch; Scratch scratch;
lua_getglobal(LuaState, "OnOpen"); lua_getglobal(LuaState, "OnOpen");
lua_pushlstring(LuaState, path.data, path.len); 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); const char *error_message = lua_tostring(LuaState, -1);
ReportWarningf("Failed the call to OnOpen! %s", error_message); ReportWarningf("Failed the call to OnOpen! %s", error_message);
lua_pop(LuaState, 1); lua_pop(LuaState, 1);
@@ -979,8 +973,8 @@ BSet Command_Open(String path) {
} }
BSet main = GetActiveMainSet(); BSet main = GetActiveMainSet();
main.window->active_goto_list = main.view->id; String kind = FieldString(LuaState, "kind");
if (FieldString(LuaState, "kind") == "text") { if (kind == "text") {
String file_path = FieldString(LuaState, "file_path"); String file_path = FieldString(LuaState, "file_path");
String line_string = FieldString(LuaState, "line"); String line_string = FieldString(LuaState, "line");
Int line = strtoll(line_string.data, NULL, 10); Int line = strtoll(line_string.data, NULL, 10);
@@ -1005,15 +999,17 @@ BSet Command_Open(String path) {
} }
} }
UpdateScroll(main.window, true); UpdateScroll(main.window, true);
} else if (FieldString(LuaState, "kind") == "exec") { } else if (kind == "exec") {
String cmd = FieldString(LuaState, "cmd"); String cmd = FieldString(LuaState, "cmd");
String working_dir = FieldString(LuaState, "working_dir"); String working_dir = FieldString(LuaState, "working_dir");
Command_Exec(cmd, Command_GetMainDir()); 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 // this shouldn't change the focus/window/view
String cmd = FieldString(LuaState, "cmd"); String cmd = FieldString(LuaState, "cmd");
String working_dir = FieldString(LuaState, "working_dir"); String working_dir = FieldString(LuaState, "working_dir");
Exec(NullViewID, true, cmd, working_dir); Exec(NullViewID, true, cmd, working_dir);
} else if (kind == "skip") {
return {};
} else { } else {
ReportWarningf("Failed to match any of OnOpen results!"); ReportWarningf("Failed to match any of OnOpen results!");
} }
@@ -1021,10 +1017,10 @@ BSet Command_Open(String path) {
return main; return main;
} }
BSet Command_Open(String16 path) { BSet Command_Open(String16 path, String meta) {
Scratch scratch; Scratch scratch;
String string = ToString(scratch, path); String string = ToString(scratch, path);
return Command_Open(string); return Command_Open(string, meta);
} }
int Lua_Open(lua_State *L) { int Lua_Open(lua_State *L) {
@@ -1110,7 +1106,6 @@ int Lua_Eval(lua_State *L) {
return 0; return 0;
} }
int Lua_SetProjectFile(lua_State *L) { int Lua_SetProjectFile(lua_State *L) {
BSet set = GetActiveMainSet(); BSet set = GetActiveMainSet();
LuaProjectBuffer = set.buffer; LuaProjectBuffer = set.buffer;
@@ -1143,4 +1138,4 @@ int Lua_GetBufferList(lua_State *L) {
} }
/* We still have table left on top of the Lua stack. */ /* We still have table left on top of the Lua stack. */
return 1; return 1;
} }

View File

@@ -126,6 +126,15 @@ SDLK_X = 0x00000078
SDLK_Y = 0x00000079 SDLK_Y = 0x00000079
SDLK_Z = 0x0000007a 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) function SkipLineAndColumn(s)
local line, col = "1", "1" local line, col = "1", "1"
@@ -235,7 +244,7 @@ function SkipPath(s)
return s, path, drive, cells return s, path, drive, cells
end end
function MatchWindowsPath(_s) function MatchWindowsPath(_s, meta)
local s, file_path, drive = SkipPath(_s) local s, file_path, drive = SkipPath(_s)
if not file_path and FileExists(drive) then 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} return {kind = "text", file_path = file_path, line = line, col = col}
end end
function MatchGitCommit(s) function MatchGitCommit(s, meta)
local i, j = string.find(s, "^commit ([a-zA-Z0-9]+)") local i, j = string.find(s, "^commit ([a-zA-Z0-9]+)")
if i then if i then
s = s:sub(8) s = s:sub(8)
@@ -268,7 +277,7 @@ function MatchGitCommit(s)
return nil return nil
end end
function MatchURL(s) function MatchURL(s, meta)
local i, j = string.find(s, "^https://") local i, j = string.find(s, "^https://")
if i then if i then
return {kind = "exec_console", cmd = '"'..INTERNET_BROWSER..'" '..s, working_dir = GetMainDir()} return {kind = "exec_console", cmd = '"'..INTERNET_BROWSER..'" '..s, working_dir = GetMainDir()}
@@ -276,16 +285,34 @@ function MatchURL(s)
return nil return nil
end 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, MatchWindowsPath,
MatchGitCommit, MatchGitCommit,
MatchURL, MatchURL,
MatchGotoBuild,
} }
function OnOpen(s) function OnOpen(path, meta)
for i = #Rules,1,-1 do for i = #OnOpenMatchers,1,-1 do
rule = Rules[i] rule = OnOpenMatchers[i]
result = rule(s) result = rule(path, meta)
if result then if result then
return result return result
end end

View File

@@ -111,8 +111,8 @@ float DPIScale = 1.0f;
Rect2I GetVisibleCells(Window *window); Rect2I GetVisibleCells(Window *window);
void AfterEdit(View *view, Array<Edit> edits); void AfterEdit(View *view, Array<Edit> edits);
Scroller ComputeScrollerRect(Window *window); Scroller ComputeScrollerRect(Window *window);
BSet Command_Open(String path); BSet Command_Open(String path, String meta = "");
BSet Command_Open(String16 path); BSet Command_Open(String16 path, String meta = "");
void UpdateScroll(Window *window, bool update_caret_scrolling); void UpdateScroll(Window *window, bool update_caret_scrolling);
void Command_SelectEntireBuffer(View *view); void Command_SelectEntireBuffer(View *view);

View File

@@ -9,6 +9,7 @@
- Delete directory/file on disk command - Delete directory/file on disk command
- Create directory command (probably should enter it automatically - Create directory command (probably should enter it automatically
- Convert more commands to taking buffer instead of view - Convert more commands to taking buffer instead of view
- Add Command_Open that takes window/view as param
- save all relevant buffers and build - save all relevant buffers and build
- shift down on last line should move the cursor to end of line!!! same for up - shift down on last line should move the cursor to end of line!!! same for up