feat(file_selector): add intergration with nvim-tree.lua (#1987)
Co-authored-by: doodleEsc <cokie@foxmail.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
@@ -249,6 +249,42 @@ function M.add_buffer_files()
|
||||
sidebar.file_selector:add_buffer_files()
|
||||
end
|
||||
|
||||
function M.add_selected_file(filepath)
|
||||
local rel_path = Utils.uniform_path(filepath)
|
||||
|
||||
local sidebar = require("avante").get()
|
||||
if not sidebar then
|
||||
require("avante.api").ask()
|
||||
sidebar = require("avante").get()
|
||||
end
|
||||
if not sidebar:is_open() then sidebar:open({}) end
|
||||
sidebar.file_selector:add_selected_file(rel_path)
|
||||
end
|
||||
|
||||
function M.remove_selected_file(filepath)
|
||||
---@diagnostic disable-next-line: undefined-field
|
||||
local stat = vim.loop.fs_stat(filepath)
|
||||
local files
|
||||
if stat and stat.type == "directory" then
|
||||
files = Utils.scan_directory({ directory = filepath, add_dirs = true })
|
||||
else
|
||||
files = { filepath }
|
||||
end
|
||||
|
||||
local sidebar = require("avante").get()
|
||||
if not sidebar then
|
||||
require("avante.api").ask()
|
||||
sidebar = require("avante").get()
|
||||
end
|
||||
if not sidebar:is_open() then sidebar:open({}) end
|
||||
|
||||
for _, file in ipairs(files) do
|
||||
local rel_path = Utils.uniform_path(file)
|
||||
vim.notify(rel_path)
|
||||
sidebar.file_selector:remove_selected_file(rel_path)
|
||||
end
|
||||
end
|
||||
|
||||
function M.stop() require("avante.llm").cancel_inflight_request() end
|
||||
|
||||
return setmetatable(M, {
|
||||
|
||||
10
lua/avante/extensions/init.lua
Normal file
10
lua/avante/extensions/init.lua
Normal file
@@ -0,0 +1,10 @@
|
||||
---@class avante.extensions
|
||||
local M = {}
|
||||
|
||||
setmetatable(M, {
|
||||
__index = function(t, k)
|
||||
---@diagnostic disable-next-line: no-unknown
|
||||
t[k] = require("avante.extensions." .. k)
|
||||
return t[k]
|
||||
end,
|
||||
})
|
||||
56
lua/avante/extensions/nvim_tree.lua
Normal file
56
lua/avante/extensions/nvim_tree.lua
Normal file
@@ -0,0 +1,56 @@
|
||||
local Api = require("avante.api")
|
||||
|
||||
--- @class avante.extensions.nvim_tree
|
||||
local M = {}
|
||||
|
||||
--- Adds the currently selected file in NvimTree to the selection via Api.add_selected_file.
|
||||
-- Notifies the user if not invoked within NvimTree or if errors occur.
|
||||
--- @return nil
|
||||
function M.add_file()
|
||||
if vim.bo.filetype ~= "NvimTree" then
|
||||
vim.notify("This action can only be used inside NvimTree.", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
local ok, nvim_tree_api = pcall(require, "nvim-tree.api")
|
||||
if not ok then
|
||||
vim.notify("nvim-tree needed", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local success, node = pcall(function() return nvim_tree_api.tree.get_node_under_cursor() end)
|
||||
if not success then
|
||||
vim.notify("Error getting node: " .. tostring(node), vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local filepath = node.absolute_path
|
||||
Api.add_selected_file(filepath)
|
||||
end
|
||||
|
||||
--- Removes the currently selected file in NvimTree from the selection via Api.remove_selected_file.
|
||||
-- Notifies the user if not invoked within NvimTree or if errors occur.
|
||||
--- @return nil
|
||||
function M.remove_file()
|
||||
if vim.bo.filetype ~= "NvimTree" then
|
||||
vim.notify("This action can only be used inside NvimTree.", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
local ok, nvim_tree_api = pcall(require, "nvim-tree.api")
|
||||
if not ok then
|
||||
vim.notify("nvim-tree needed", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local success, node = pcall(function() return nvim_tree_api.tree.get_node_under_cursor() end)
|
||||
if not success then
|
||||
vim.notify("Error getting node: " .. tostring(node), vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local filepath = node.absolute_path
|
||||
Api.remove_selected_file(filepath)
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user