From c1df9970bcc0e171a11fe9b5a71d2d6f9837d0eb Mon Sep 17 00:00:00 2001 From: Nick Dichev Date: Wed, 19 Feb 2025 02:22:07 -0800 Subject: [PATCH] feat(file_selector): add @buffers mention to add open buffers to chat context (#1303) --- lua/avante/file_selector.lua | 14 ++++++++++++++ lua/avante/sidebar.lua | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/lua/avante/file_selector.lua b/lua/avante/file_selector.lua index f5b0d22..8675ae8 100644 --- a/lua/avante/file_selector.lua +++ b/lua/avante/file_selector.lua @@ -404,4 +404,18 @@ function FileSelector:add_quickfix_files() end end +---@return nil +function FileSelector:add_buffer_files() + local buffers = vim.api.nvim_list_bufs() + for _, bufnr in ipairs(buffers) do + if vim.api.nvim_buf_is_loaded(bufnr) then + local filepath = vim.api.nvim_buf_get_name(bufnr) + if filepath and filepath ~= "" and not vim.startswith(filepath, "avante://") then + local relative_path = Utils.relative_path(filepath) + self:add_selected_file(relative_path) + end + end + end +end + return FileSelector diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index dbc5903..4707fe6 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -2474,6 +2474,13 @@ function Sidebar:create_input_container(opts) callback = function() self.file_selector:add_quickfix_files() end, }) + table.insert(mentions, { + description = "buffers", + command = "buffers", + details = "add open buffers to the chat context", + callback = function() self.file_selector:add_buffer_files() end, + }) + cmp.register_source( "avante_commands", require("cmp_avante.commands"):new(self:get_commands(), self.input_container.bufnr)