ListCode, fix unique buffer opening an existing file on disk

This commit is contained in:
Krzosa Karol
2025-05-12 17:12:37 +02:00
parent a94b914e06
commit 70858fdec5
7 changed files with 128 additions and 44 deletions

View File

@@ -251,13 +251,16 @@ function MatchExec(s, meta)
return {kind = "skip"} return {kind = "skip"}
end end
OnOpenMatchers = { BuiltinOnOpenMatchers = {
MatchWindowsPath, MatchWindowsPath,
MatchGitCommit, MatchGitCommit,
MatchURL, MatchURL,
MatchGotoBuild, MatchGotoBuild,
MatchExec, MatchExec,
} }
OnOpenMatchers = BuiltinOnOpenMatchers
function OnOpen(path, meta) function OnOpen(path, meta)
for i = #OnOpenMatchers,1,-1 do for i = #OnOpenMatchers,1,-1 do
@@ -320,3 +323,37 @@ function OnSave(buffer_id)
ApplyClangFormat(buffer_id) ApplyClangFormat(buffer_id)
end end
end end
function IsCodeExclude(s, meta)
if s:match("/.git$") or s:match("\\.git$") or
s:match(".exe$") or
s:match(".bin$") or
s:match(".obj$") or
s:match(".o$") or
s:match(".lib$") or
s:match(".ilk$") or
s:match(".cache$") or
s:match(".exp$") or
s:match(".pdb$") or
s:match("/external/") or s:match("\\external\\")
then
return false
end
return true
end
BuiltinIsCodeMatchers = {
IsCodeExclude,
}
IsCodeMatchers = BuiltinIsCodeMatchers
function IsCode(path, meta)
for i = #IsCodeMatchers,1,-1 do
rule = IsCodeMatchers[i]
result = rule(path, meta)
if result then
return result
end
end
return false
end

View File

@@ -40,10 +40,12 @@ void GotoForward(Window *window) {
UpdateScroll(window, true); UpdateScroll(window, true);
} }
void JumpGarbageBuffer(BSet *set) { void JumpGarbageBuffer(BSet *set, String buffer_name = "") {
CheckpointBeforeGoto(set->window); CheckpointBeforeGoto(set->window);
if (buffer_name.len == 0) {
String current_dir = ChopLastSlash(set->buffer->name); String current_dir = ChopLastSlash(set->buffer->name);
String buffer_name = GetUniqueBufferName(current_dir, "temp"); buffer_name = GetUniqueBufferName(current_dir, "temp");
}
set->view = WindowOpenBufferView(set->window, buffer_name); set->view = WindowOpenBufferView(set->window, buffer_name);
set->buffer = GetBuffer(set->view->active_buffer); set->buffer = GetBuffer(set->view->active_buffer);
set->buffer->garbage = true; set->buffer->garbage = true;
@@ -140,28 +142,6 @@ void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string) {
view->carets = carets; view->carets = carets;
} }
void ListFilesRecursive(Buffer *buffer, String filename) {
Scratch scratch(buffer->line_starts.allocator);
for (FileIter it = IterateFiles(scratch, filename); IsValid(it); Advance(&it)) {
if (it.filename == ".git") {
continue;
}
if (it.is_directory) {
ListFilesRecursive(buffer, it.absolute_path);
} else {
bool good = EndsWith(it.filename, ".c") ||
EndsWith(it.filename, ".h") ||
EndsWith(it.filename, ".cpp") ||
EndsWith(it.filename, ".hpp") ||
EndsWith(it.filename, ".bat");
if (!good) {
continue;
}
RawAppendf(buffer, "%.*s\n", FmtString(it.absolute_path));
}
}
}
// @todo: revamp interface since it scrolls ALL VIEWS??? or maybe not?? // @todo: revamp interface since it scrolls ALL VIEWS??? or maybe not??
void Command_Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line) { void Command_Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line) {
Scratch scratch; Scratch scratch;
@@ -611,6 +591,7 @@ void TrimTrailingWhitespace(Buffer *buffer, bool trim_lines_with_caret = false)
EndEdit(buffer, &edits, &view->carets, !KILL_SELECTION); EndEdit(buffer, &edits, &view->carets, !KILL_SELECTION);
view->update_scroll = false; view->update_scroll = false;
} }
int Lua_TrimTrailingWhitespace(lua_State *L) { int Lua_TrimTrailingWhitespace(lua_State *L) {
lua_Integer buffer_id = luaL_checkinteger(L, 1); lua_Integer buffer_id = luaL_checkinteger(L, 1);
int trim_lines_with_caret = lua_toboolean(L, 2); int trim_lines_with_caret = lua_toboolean(L, 2);
@@ -651,6 +632,7 @@ void ConvertLineEndingsToLF(Buffer *buffer, bool trim_lines_with_caret = false)
EndEdit(buffer, &edits, &view->carets, !KILL_SELECTION); EndEdit(buffer, &edits, &view->carets, !KILL_SELECTION);
view->update_scroll = false; view->update_scroll = false;
} }
int Lua_ConvertLineEndingsToLF(lua_State *L) { int Lua_ConvertLineEndingsToLF(lua_State *L) {
lua_Integer buffer_id = luaL_checkinteger(L, 1); lua_Integer buffer_id = luaL_checkinteger(L, 1);
int trim_lines_with_caret = lua_toboolean(L, 2); int trim_lines_with_caret = lua_toboolean(L, 2);
@@ -1038,7 +1020,6 @@ int Lua_NewDir(lua_State *L) {
return 0; return 0;
} }
void Command_ToggleFullscreen() { void Command_ToggleFullscreen() {
if (IsInFullscreen) { if (IsInFullscreen) {
SDL_SetWindowSize(SDLWindow, FullScreenSizeX, FullScreenSizeY); SDL_SetWindowSize(SDLWindow, FullScreenSizeX, FullScreenSizeY);
@@ -1055,23 +1036,39 @@ void Command_ToggleFullscreen() {
IsInFullscreen = !IsInFullscreen; IsInFullscreen = !IsInFullscreen;
} }
int Lua_ToggleFullscreen(lua_State *L) { int Lua_ToggleFullscreen(lua_State *L) {
Command_ToggleFullscreen(); Command_ToggleFullscreen();
return 0; return 0;
} }
void Command_GetCFiles(void) { void ListFilesRecursive(Buffer *buffer, String filename) {
BSet main = GetActiveMainSet(); Scratch scratch(buffer->line_starts.allocator);
for (FileIter it = IterateFiles(scratch, filename); IsValid(it); Advance(&it)) {
Scratch scratch; if (it.filename == ".git") {
String buffer_name = GetUniqueBufferName(GetDir(main.buffer), "getcfiles"); continue;
Buffer *buffer = CreateBuffer(GetSystemAllocator(), buffer_name, 4096 * 4);
ListFilesRecursive(buffer, Command_GetMainDir());
WindowOpenBufferView(main.window, buffer_name);
} }
int Lua_GetCFiles(lua_State *L) { if (it.is_directory) {
Command_GetCFiles(); ListFilesRecursive(buffer, it.absolute_path);
} else {
bool good = CallIsCode(it.absolute_path);
if (!good) {
continue;
}
RawAppendf(buffer, "%.*s\n", FmtString(it.absolute_path));
}
}
}
void Command_ListCode(void) {
BSet main = GetActiveMainSet();
JumpGarbageBuffer(&main);
ListFilesRecursive(main.buffer, WorkDir);
main.view->fuzzy_search = true;
}
int Lua_ListCode(lua_State *L) {
Command_ListCode();
return 0; return 0;
} }
@@ -1106,8 +1103,7 @@ BSet Command_Open(Window *window, String path, String meta, bool set_active = tr
ActiveWindow = set.window->id; ActiveWindow = set.window->id;
} }
if (IsDir(ores.file_path)) { if (IsDir(ores.file_path)) {
JumpGarbageBuffer(&set); JumpGarbageBuffer(&set, GetUniqueBufferName(ores.file_path, "dir"));
set.buffer->name = GetUniqueBufferName(ores.file_path, "temp");
Command_Appendf(set.view, "..\n", FmtString(ores.file_path)); Command_Appendf(set.view, "..\n", FmtString(ores.file_path));
for (FileIter it = IterateFiles(scratch, ores.file_path); IsValid(it); Advance(&it)) { for (FileIter it = IterateFiles(scratch, ores.file_path); IsValid(it); Advance(&it)) {
Command_Appendf(set.view, "%.*s\n", FmtString(it.filename)); Command_Appendf(set.view, "%.*s\n", FmtString(it.filename));

View File

@@ -285,7 +285,8 @@ void OnCommand(Event event) {
if (CtrlPress(SDLK_P)) { if (CtrlPress(SDLK_P)) {
void Command_ListBuffers(); Command_ListCode();
} else if (CtrlAltPress(SDLK_P)) {
Command_ListBuffers(); Command_ListBuffers();
} }

View File

@@ -321,13 +321,16 @@ function MatchExec(s, meta)
return {kind = "skip"} return {kind = "skip"}
end end
OnOpenMatchers = { BuiltinOnOpenMatchers = {
MatchWindowsPath, MatchWindowsPath,
MatchGitCommit, MatchGitCommit,
MatchURL, MatchURL,
MatchGotoBuild, MatchGotoBuild,
MatchExec, MatchExec,
} }
OnOpenMatchers = BuiltinOnOpenMatchers
function OnOpen(path, meta) function OnOpen(path, meta)
for i = #OnOpenMatchers,1,-1 do for i = #OnOpenMatchers,1,-1 do
@@ -390,6 +393,40 @@ function OnSave(buffer_id)
ApplyClangFormat(buffer_id) ApplyClangFormat(buffer_id)
end end
end end
function IsCodeExclude(s, meta)
if s:match("/.git$") or s:match("\\.git$") or
s:match(".exe$") or
s:match(".bin$") or
s:match(".obj$") or
s:match(".o$") or
s:match(".lib$") or
s:match(".ilk$") or
s:match(".cache$") or
s:match(".exp$") or
s:match(".pdb$") or
s:match("/external/") or s:match("\\external\\")
then
return false
end
return true
end
BuiltinIsCodeMatchers = {
IsCodeExclude,
}
IsCodeMatchers = BuiltinIsCodeMatchers
function IsCode(path, meta)
for i = #IsCodeMatchers,1,-1 do
rule = IsCodeMatchers[i]
result = rule(path, meta)
if result then
return result
end
end
return false
end
)=="; )==";
void ReloadStyle() { void ReloadStyle() {
ColorText = GetColor("Text", ColorText); ColorText = GetColor("Text", ColorText);

View File

@@ -318,6 +318,19 @@ OnOpenResult CallOnOpen(String path, String meta) {
return result; return result;
} }
bool CallIsCode(String path, String meta = "") {
lua_getglobal(LuaState, "IsCode");
lua_pushlstring(LuaState, path.data, path.len);
lua_pushlstring(LuaState, meta.data, meta.len);
if (!CallLuaFunc("IsCode", 2, 1)) {
return false;
}
bool result = lua_toboolean(LuaState, -1);
lua_pop(LuaState, 1);
return result;
}
void CallOnSave(BufferID buffer_id) { void CallOnSave(BufferID buffer_id) {
lua_getglobal(LuaState, "OnSave"); lua_getglobal(LuaState, "OnSave");
lua_pushinteger(LuaState, buffer_id.id); lua_pushinteger(LuaState, buffer_id.id);

View File

@@ -21,7 +21,7 @@ luaL_Reg LuaFunctions[] = {
{"New", Lua_New}, {"New", Lua_New},
{"NewDir", Lua_NewDir}, {"NewDir", Lua_NewDir},
{"ToggleFullscreen", Lua_ToggleFullscreen}, {"ToggleFullscreen", Lua_ToggleFullscreen},
{"GetCFiles", Lua_GetCFiles}, {"ListCode", Lua_ListCode},
{"C", Lua_C}, {"C", Lua_C},
{"Open", Lua_Open}, {"Open", Lua_Open},
{"Cmd", Lua_Cmd}, {"Cmd", Lua_Cmd},

View File

@@ -58,7 +58,7 @@ String GetUniqueBufferName(String working_dir, String prepend_name) {
buffer_name = Format(scratch, "%.*s/%.*s%d.log", FmtString(working_dir), FmtString(prepend_name), i); buffer_name = Format(scratch, "%.*s/%.*s%d.log", FmtString(working_dir), FmtString(prepend_name), i);
buffer_name = GetAbsolutePath(scratch, buffer_name); buffer_name = GetAbsolutePath(scratch, buffer_name);
Buffer *exists = FindBuffer(buffer_name); Buffer *exists = FindBuffer(buffer_name);
if (!exists) { if (!exists && !FileExists(buffer_name)) {
break; break;
} }
} }