fix(file_selector): avoid converting to relative path twice

get_project_filepath() converts candidate file paths to relative form
twice, once in the first iterator, and then in vim.tbl_map() call while.
It also iterates the same list twice for no apparent reason.

Combine both scans into one and use dedicated vim.fn.isdirectory().
This commit is contained in:
Dmitry Torokhov
2025-07-14 12:32:56 -07:00
committed by yetone
parent b8bb0fd969
commit 61e38024dc

View File

@@ -52,17 +52,19 @@ function FileSelector:handle_path_selection(selected_paths)
self:emit("update")
end
---Scans a given directory and produces a list of files/directories with paths relative to the project root
---@return string[]
local function get_project_filepaths()
local project_root = Utils.get_project_root()
local files = Utils.scan_directory({ directory = project_root, add_dirs = true })
files = vim.iter(files):map(function(filepath) return Utils.make_relative_path(filepath, project_root) end):totable()
return vim.tbl_map(function(path)
local rel_path = Utils.make_relative_path(path, project_root)
local stat = vim.loop.fs_stat(path)
if stat and stat.type == "directory" then rel_path = rel_path .. "/" end
return rel_path
end, files)
return vim
.iter(files)
:map(function(path)
local rel_path = Utils.make_relative_path(path, project_root)
if vim.fn.isdirectory(rel_path) == 1 then rel_path = rel_path .. "/" end
return rel_path
end)
:totable()
end
---@param id integer