Skip to content

Commit

Permalink
fix: load file content from opened buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone committed Jan 14, 2025
1 parent 24cb3f8 commit 73aadfd
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lua/avante/file_selector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ end
function FileSelector:get_selected_files_contents()
local contents = {}
for _, file_path in ipairs(self.selected_filepaths) do
--- lookup loaded buffer firstly
local bufnr = vim.fn.bufnr(file_path)
if bufnr ~= -1 then
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local content = table.concat(lines, "\n")
local filetype = vim.api.nvim_get_option_value("filetype", { buf = bufnr })
table.insert(contents, { path = file_path, content = content, file_type = filetype })
goto continue
end

local file, open_err = io.open(file_path, "r")

if open_err then Utils.debug("error reading file:", open_err) end
Expand All @@ -289,6 +299,7 @@ function FileSelector:get_selected_files_contents()

table.insert(contents, { path = file_path, content = content, file_type = filetype })
end
::continue::
end
return contents
end
Expand Down

0 comments on commit 73aadfd

Please sign in to comment.