From 6374f3cd1a5f00b736bfe6e147dfb9eaa8717cff Mon Sep 17 00:00:00 2001 From: krzosa Date: Thu, 1 May 2025 08:26:55 +0200 Subject: [PATCH] Fix mouse, fix allocator in file iter, GetCFiles --- build_file.cpp | 16 ++++++------- src/basic/filesystem.h | 2 -- src/basic/win32.cpp | 16 +++++-------- src/text_editor/commands.cpp | 10 ++++---- src/text_editor/commands_bindings.cpp | 33 ++++++++++++++++++++++++++- src/text_editor/generated.cpp | 16 ++++++------- src/text_editor/lua_api.cpp | 24 ++----------------- src/text_editor/lua_api_generated.cpp | 2 +- 8 files changed, 63 insertions(+), 56 deletions(-) diff --git a/build_file.cpp b/build_file.cpp index 2160860..ed482c3 100644 --- a/build_file.cpp +++ b/build_file.cpp @@ -397,6 +397,14 @@ function AddCo(f) return Coroutines[i] end +OnCommandCallbacks = {} +table.insert(OnCommandCallbacks, function (e) + if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then + C("git grep -n "..GetLoadWord().." :/") end + if e.key == SDLK_L and e.ctrl == 1 then + Eval(GetLoadWord()) end +end) + function OnUpdate(e) local new_co_list = {} for key, co in pairs(Coroutines) do @@ -409,14 +417,6 @@ function OnUpdate(e) Coroutines = new_co_list end -OnCommandCallbacks = {} -table.insert(OnCommandCallbacks, function (e) - if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then - C("git grep -n "..GetLoadWord().." :/") end - if e.key == SDLK_L and e.ctrl == 1 then - Eval(GetLoadWord()) end -end) - function OnCommand(e) for i = #OnCommandCallbacks,1,-1 do on_command = OnCommandCallbacks[i] diff --git a/src/basic/filesystem.h b/src/basic/filesystem.h index 61ec566..18bd612 100644 --- a/src/basic/filesystem.h +++ b/src/basic/filesystem.h @@ -10,8 +10,6 @@ struct FileIter { String path; Allocator allocator; - Arena *arena; - TempArena temp; union { struct Win32_FileIter *w32; void *dir; diff --git a/src/basic/win32.cpp b/src/basic/win32.cpp index 175d2ba..41da40f 100644 --- a/src/basic/win32.cpp +++ b/src/basic/win32.cpp @@ -99,8 +99,6 @@ bool IsValid(const FileIter &it) { } void Advance(FileIter *it) { - if (it->temp.arena) EndTemp(it->temp); - it->temp = BeginTemp(it->arena); while (FindNextFileW(it->w32->handle, &it->w32->data) != 0) { WIN32_FIND_DATAW *data = &it->w32->data; @@ -109,11 +107,11 @@ void Advance(FileIter *it) { if (data->cFileName[0] == '.' && data->cFileName[1] == 0) continue; it->is_directory = data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; - it->filename = ToString(*it->arena, (char16_t *)data->cFileName, WideLength((char16_t *)data->cFileName)); + it->filename = ToString(it->allocator, (char16_t *)data->cFileName, WideLength((char16_t *)data->cFileName)); const char *is_dir = it->is_directory ? "/" : ""; const char *separator = it->path.data[it->path.len - 1] == '/' ? "" : "/"; - it->relative_path = Format(*it->arena, "%.*s%s%.*s%s", FmtString(it->path), separator, FmtString(it->filename), is_dir); - it->absolute_path = GetAbsolutePath(*it->arena, it->relative_path); + it->relative_path = Format(it->allocator, "%.*s%s%.*s%s", FmtString(it->path), separator, FmtString(it->filename), is_dir); + it->absolute_path = GetAbsolutePath(it->allocator, it->relative_path); it->is_valid = true; if (it->is_directory) { @@ -127,19 +125,17 @@ void Advance(FileIter *it) { DWORD error = GetLastError(); Assert(error == ERROR_NO_MORE_FILES); FindClose(it->w32->handle); - Dealloc(it->allocator, &it->arena); } FileIter IterateFiles(Allocator alo, String path) { FileIter it = {0}; it.allocator = alo; - it.arena = AllocArena(alo, MiB(2)); it.path = path; - String modified_path = Format(*it.arena, "%.*s\\*", FmtString(path)); - String16 modified_path16 = ToString16(*it.arena, modified_path); + String modified_path = Format(it.allocator, "%.*s\\*", FmtString(path)); + String16 modified_path16 = ToString16(it.allocator, modified_path); - it.w32 = AllocType(*it.arena, Win32_FileIter); + it.w32 = AllocType(it.allocator, Win32_FileIter); it.w32->handle = FindFirstFileW((wchar_t *)modified_path16.data, &it.w32->data); if (it.w32->handle == INVALID_HANDLE_VALUE) { it.is_valid = false; diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 4575214..0f93860 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -60,16 +60,18 @@ void GotoForward(Window *window) { Int ScreenSpaceToBufferPos(Window *window, View *view, Buffer *buffer, Vec2I mouse) { Vec2I mworld = mouse - window->document_rect.min + view->scroll; - Vec2I pos = mworld / Vec2I{FontCharSpacing, FontLineSpacing}; - XY xy = {(Int)(pos.x), (Int)(pos.y)}; + double px = (double)mworld.x / (double)FontCharSpacing; + double py = (double)mworld.y / (double)FontLineSpacing; + XY xy = {(Int)round(px), (Int)floor(py)}; Int result = XYToPosWithoutNL(buffer, xy); return result; } Int ScreenSpaceToBufferPosErrorOutOfBounds(Window *window, View *view, Buffer *buffer, Vec2I mouse) { Vec2I mworld = mouse - window->document_rect.min + view->scroll; - Vec2I pos = mworld / Vec2I{FontCharSpacing, FontLineSpacing}; - XY xy = {(Int)(pos.x), (Int)(pos.y)}; + double px = (double)mworld.x / (double)FontCharSpacing; + double py = (double)mworld.y / (double)FontLineSpacing; + XY xy = {(Int)round(px), (Int)floor(py)}; Int result = XYToPosErrorOutOfBounds(buffer, xy); return result; } diff --git a/src/text_editor/commands_bindings.cpp b/src/text_editor/commands_bindings.cpp index 020aee7..595c842 100644 --- a/src/text_editor/commands_bindings.cpp +++ b/src/text_editor/commands_bindings.cpp @@ -12,6 +12,35 @@ String16 GetSearchString(Window *window) { return string; } +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"); + if (!good) { + continue; + } + RawAppendf(buffer, "%.*s\n", FmtString(it.absolute_path)); + } + } +} + +void Command_GetCFiles(void) { + BSet main = GetActiveMainSet(); + + Scratch scratch; + String buffer_name = GetUniqueBufferName(scratch, GetDir(main.buffer), "+ls-"); + + Buffer *buffer = CreateBuffer(GetSystemAllocator(), buffer_name, 4096 * 4); + ListFilesRecursive(buffer, Command_GetDir()); + WindowOpenBufferView(main.window, buffer_name); +} + void OnCommand(Event event) { ProfileFunction(); WindowID start_command_active_window = ActiveWindow; @@ -245,8 +274,10 @@ void OnCommand(Event event) { } } - if (CtrlPress(SDLK_P)) { + if (CtrlShiftPress(SDLK_P)) { Command_ListBuffers(); + } else if (CtrlPress(SDLK_P)) { + Command_GetCFiles(); } if (CtrlShiftPress(SDLK_BACKSLASH)) { diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index bfe9710..7790394 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -281,6 +281,14 @@ function AddCo(f) return Coroutines[i] end +OnCommandCallbacks = {} +table.insert(OnCommandCallbacks, function (e) + if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then + C("git grep -n "..GetLoadWord().." :/") end + if e.key == SDLK_L and e.ctrl == 1 then + Eval(GetLoadWord()) end +end) + function OnUpdate(e) local new_co_list = {} for key, co in pairs(Coroutines) do @@ -293,14 +301,6 @@ function OnUpdate(e) Coroutines = new_co_list end -OnCommandCallbacks = {} -table.insert(OnCommandCallbacks, function (e) - if e.key == SDLK_F and e.ctrl == 1 and e.shift == 1 then - C("git grep -n "..GetLoadWord().." :/") end - if e.key == SDLK_L and e.ctrl == 1 then - Eval(GetLoadWord()) end -end) - function OnCommand(e) for i = #OnCommandCallbacks,1,-1 do on_command = OnCommandCallbacks[i] diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 03f13d4..9a4d2e4 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -250,28 +250,8 @@ int Lua_BufferExists(lua_State *L) { return 1; } -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 { - RawAppendf(buffer, "%.*s\n", FmtString(it.absolute_path)); - } - } -} - -int Lua_Ls(lua_State *L) { - BSet main = GetActiveMainSet(); - - Scratch scratch; - String buffer_name = GetUniqueBufferName(scratch, GetDir(main.buffer), "+ls-"); - - Buffer *buffer = CreateBuffer(GetSystemAllocator(), buffer_name, 4096 * 4); - ListFilesRecursive(buffer, Command_GetDir()); - WindowOpenBufferView(main.window, buffer_name); - +int Lua_GetCFiles(lua_State *L) { + Command_GetCFiles(); return 0; } diff --git a/src/text_editor/lua_api_generated.cpp b/src/text_editor/lua_api_generated.cpp index 967b77d..9e90087 100644 --- a/src/text_editor/lua_api_generated.cpp +++ b/src/text_editor/lua_api_generated.cpp @@ -14,7 +14,7 @@ luaL_Reg LuaFunctions[] = { {"ListBuffers", Lua_ListBuffers}, {"GetBufferList", Lua_GetBufferList}, {"BufferExists", Lua_BufferExists}, - {"Ls", Lua_Ls}, + {"GetCFiles", Lua_GetCFiles}, {"Rename", Lua_Rename}, {"GetSelection", Lua_GetSelection}, {"GetEntireBuffer", Lua_GetEntireBuffer},