feat(context): add current buffer to selected file ctx (#941)

This commit is contained in:
Fernando Freire 2024-12-13 06:57:42 -08:00 committed by GitHub
parent a1da07097d
commit 5c20cc1779
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 1 deletions

View File

@ -189,6 +189,9 @@ M.defaults = {
remove_file = "d",
add_file = "@",
},
files = {
add_current = "<leader>ac", -- Add current buffer to selected files
},
},
windows = {
---@alias AvantePosition "right" | "left" | "top" | "bottom" | "smart"

View File

@ -32,7 +32,39 @@ function FileSelector:reset()
self.event_handlers = {}
end
function FileSelector:add_selected_file(filepath) table.insert(self.selected_filepaths, Utils.uniform_path(filepath)) end
function FileSelector:add_selected_file(filepath)
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)
self:emit("update")
end
end
function FileSelector:add_current_buffer()
local current_buf = vim.api.nvim_get_current_buf()
local filepath = vim.api.nvim_buf_get_name(current_buf)
-- Only process if it's a real file buffer
if filepath and filepath ~= "" and not vim.startswith(filepath, "avante://") then
local relative_path = require("avante.utils").relative_path(filepath)
-- Check if file is already in list
for i, path in ipairs(self.selected_filepaths) do
if path == relative_path then
-- Remove if found
table.remove(self.selected_filepaths, i)
self:emit("update")
return true
end
end
-- Add if not found
self:add_selected_file(relative_path)
return true
end
return false
end
function FileSelector:on(event, callback)
local handlers = self.event_handlers[event]

View File

@ -826,6 +826,21 @@ end
function Sidebar:on_mount(opts)
self:refresh_winids()
-- Add keymap to add current buffer while sidebar is open
if Config.mappings.files and Config.mappings.files.add_current then
vim.keymap.set("n", Config.mappings.files.add_current, function()
if self:is_open() and self.file_selector:add_current_buffer() then
vim.notify("Added current buffer to file selector", vim.log.levels.DEBUG, { title = "Avante" })
else
vim.notify("Failed to add current buffer", vim.log.levels.WARN, { title = "Avante" })
end
end, {
desc = "avante: add current buffer to file selector",
noremap = true,
silent = true,
})
end
api.nvim_set_option_value("wrap", Config.windows.wrap, { win = self.result_container.winid })
local current_apply_extmark_id = nil