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

Add other open buffer files to context. #685

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,34 @@ function Sidebar:create_input(opts)

local chat_history = Path.history.load(self.code.bufnr)

local function get_open_buffers_content()
local buffers = vim.api.nvim_list_bufs()
local result = {}
table.insert(result, "Here is the content of other open files in the project:")
table.insert(result, "These files are for context only. Return code to edit from the main file only.")
table.insert(result, "")

for _, bufnr in ipairs(buffers) do
if vim.api.nvim_buf_is_loaded(bufnr) and bufnr ~= self.code.bufnr then
local bufname = vim.api.nvim_buf_get_name(bufnr)
local relative_path = vim.fn.fnamemodify(bufname, ":.")
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
if #lines > 0 then
table.insert(result, "## " .. relative_path)
table.insert(result, "")
for _, line in ipairs(lines) do
table.insert(result, line)
end
table.insert(result, "")
table.insert(result, "---")
table.insert(result, "")
end
end
end

return table.concat(result, "\n")
end

---@param request string
local function handle_submit(request)
local model = Config.has_provider(Config.provider) and Config.get_provider(Config.provider).model or "default"
Expand Down Expand Up @@ -1270,6 +1298,17 @@ function Sidebar:create_input(opts)

local project_context = mentions.enable_project_context and RepoMap.get_repo_map(file_ext) or nil

-- Check if it's the first chat and add open buffers content if so
local is_first_chat = next(chat_history) == nil
if is_first_chat then
local open_buffers_content = get_open_buffers_content()
if project_context then
project_context.open_buffers = open_buffers_content
else
project_context = { open_buffers = open_buffers_content }
end
end

Llm.stream({
bufnr = self.code.bufnr,
ask = opts.ask,
Expand Down