Fix mouse, fix allocator in file iter, GetCFiles

This commit is contained in:
krzosa
2025-05-01 08:26:55 +02:00
parent e652d5b0e2
commit 6374f3cd1a
8 changed files with 63 additions and 56 deletions

View File

@@ -10,8 +10,6 @@ struct FileIter {
String path;
Allocator allocator;
Arena *arena;
TempArena temp;
union {
struct Win32_FileIter *w32;
void *dir;

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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)) {

View File

@@ -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]

View File

@@ -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;
}

View File

@@ -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},