From 61e38024dc73db9c65fdf6f007137aa15a2423e2 Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Mon, 14 Jul 2025 12:32:56 -0700 Subject: [PATCH] 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(). --- lua/avante/file_selector.lua | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lua/avante/file_selector.lua b/lua/avante/file_selector.lua index 0c423ae..29b3418 100644 --- a/lua/avante/file_selector.lua +++ b/lua/avante/file_selector.lua @@ -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