Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (sidebar) support files outside of the current working directory. #1065

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lua/avante/file_selector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"

Expand Down
7 changes: 6 additions & 1 deletion lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lua/avante/utils/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lua/avante/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading