diff --git a/lua/avante/file_selector.lua b/lua/avante/file_selector.lua index 6a3a476..c5241d6 100644 --- a/lua/avante/file_selector.lua +++ b/lua/avante/file_selector.lua @@ -36,6 +36,7 @@ function FileSelector:add_selected_file(filepath) if not filepath or filepath == "" then return end local uniform_path = Utils.uniform_path(filepath) + -- Avoid duplicates if not vim.tbl_contains(self.selected_filepaths, uniform_path) then table.insert(self.selected_filepaths, uniform_path) @@ -273,11 +274,16 @@ end function FileSelector:get_selected_files_contents() local contents = {} for _, file_path in ipairs(self.selected_filepaths) do - local file = io.open(file_path, "r") + local file, open_err = io.open(file_path, "r") + + if open_err then Utils.debug("error reading file:", open_err) end + if file then - local content = file:read("*all") + local content, read_err = file:read("*all") file:close() + if read_err then Utils.debug("failed to read:", file_path, read_err) end + -- Detect the file type local filetype = vim.filetype.match({ filename = file_path, contents = contents }) or "unknown" diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 1b55ecb..cfbd71f 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -1148,8 +1148,13 @@ function Sidebar:initialize() if not self.code.bufnr or not api.nvim_buf_is_valid(self.code.bufnr) then return self end + local buf_path = api.nvim_buf_get_name(self.code.bufnr) + -- if the filepath is outside of the current working directory then we want the absolute path + local file_path = Utils.file.is_in_cwd(buf_path) and Utils.relative_path(buf_path) or buf_path + Utils.debug("Sidebar:initialize adding buffer to file selector", buf_path) + self.file_selector:reset() - self.file_selector:add_selected_file(Utils.relative_path(api.nvim_buf_get_name(self.code.bufnr))) + self.file_selector:add_selected_file(file_path) return self end diff --git a/lua/avante/utils/file.lua b/lua/avante/utils/file.lua index 30df171..93fe275 100644 --- a/lua/avante/utils/file.lua +++ b/lua/avante/utils/file.lua @@ -39,6 +39,15 @@ function M.exists(filepath) return stat ~= nil end +function M.is_in_cwd(filepath) + local cwd = vim.fn.getcwd() + -- Make both paths absolute for comparison + local abs_filepath = vim.fn.fnamemodify(filepath, ":p") + local abs_cwd = vim.fn.fnamemodify(cwd, ":p") + -- Check if filepath starts with cwd + return abs_filepath:sub(1, #abs_cwd) == abs_cwd +end + function M.get_file_icon(filepath) local filetype = Filetype.detect(filepath, {}) or "unknown" ---@type string diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index a5ec394..80cded3 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -826,6 +826,7 @@ function M.get_current_selection_diagnostics(bufnr, selection) end function M.uniform_path(path) + if not M.file.is_in_cwd(path) then return path end local project_root = M.get_project_root() local abs_path = Path:new(project_root):joinpath(path):absolute() local relative_path = Path:new(abs_path):make_relative(project_root)