feat(context): add current buffer to selected file ctx (#941)
This commit is contained in:
parent
a1da07097d
commit
5c20cc1779
@ -189,6 +189,9 @@ M.defaults = {
|
|||||||
remove_file = "d",
|
remove_file = "d",
|
||||||
add_file = "@",
|
add_file = "@",
|
||||||
},
|
},
|
||||||
|
files = {
|
||||||
|
add_current = "<leader>ac", -- Add current buffer to selected files
|
||||||
|
},
|
||||||
},
|
},
|
||||||
windows = {
|
windows = {
|
||||||
---@alias AvantePosition "right" | "left" | "top" | "bottom" | "smart"
|
---@alias AvantePosition "right" | "left" | "top" | "bottom" | "smart"
|
||||||
|
@ -32,7 +32,39 @@ function FileSelector:reset()
|
|||||||
self.event_handlers = {}
|
self.event_handlers = {}
|
||||||
end
|
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)
|
function FileSelector:on(event, callback)
|
||||||
local handlers = self.event_handlers[event]
|
local handlers = self.event_handlers[event]
|
||||||
|
@ -826,6 +826,21 @@ end
|
|||||||
function Sidebar:on_mount(opts)
|
function Sidebar:on_mount(opts)
|
||||||
self:refresh_winids()
|
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 })
|
api.nvim_set_option_value("wrap", Config.windows.wrap, { win = self.result_container.winid })
|
||||||
|
|
||||||
local current_apply_extmark_id = nil
|
local current_apply_extmark_id = nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user