Files
avante.nvim/lua/cmp_avante/commands.lua
Dmitry Torokhov 7d60b51324 refactor(sidebar): move sub-windows into a table
Maintaining secondary table of window IDs is cumbersome and is prone to
getting out of sync with the true state of the sidebar. In preparation
for removal of winids table move all containers (sub-windows of the
sidebar) into "containers" table.

The change is mostly mechanical rename with following exceptions:

 - Sidebar:reifresh_winids() and other places where the code scanned
   entire Sidebar object looking for tables with specific fields, such
   as "winid", or "mount" needed to be adjusted for the new structure

 - Sidebar:new() and Sidebar:reset() have been adjusted to make better
   use of the new sub-table.
2025-07-21 13:31:07 +08:00

68 lines
1.8 KiB
Lua

local api = vim.api
---@class CommandsSource : cmp.Source
local CommandsSource = {}
CommandsSource.__index = CommandsSource
function CommandsSource:new()
local instance = setmetatable({}, CommandsSource)
return instance
end
function CommandsSource:is_available() return vim.bo.filetype == "AvanteInput" end
function CommandsSource.get_position_encoding_kind() return "utf-8" end
function CommandsSource:get_trigger_characters() return { "/" } end
function CommandsSource:get_keyword_pattern() return [[\%(@\|#\|/\)\k*]] end
function CommandsSource:complete(_, callback)
local Utils = require("avante.utils")
local kind = require("cmp").lsp.CompletionItemKind.Variable
local commands = Utils.get_commands()
local items = {}
for _, command in ipairs(commands) do
table.insert(items, {
label = "/" .. command.name,
kind = kind,
detail = command.details,
data = {
name = command.name,
},
})
end
callback({
items = items,
isIncomplete = false,
})
end
function CommandsSource:execute(item, callback)
local Utils = require("avante.utils")
local commands = Utils.get_commands()
local command = vim.iter(commands):find(function(command) return command.name == item.data.name end)
if not command then return end
local sidebar = require("avante").get()
command.callback(sidebar, nil, function()
local bufnr = sidebar.containers.input.bufnr ---@type integer
local content = table.concat(api.nvim_buf_get_lines(bufnr, 0, -1, false), "\n")
vim.defer_fn(function()
if vim.api.nvim_buf_is_valid(bufnr) then
local lines = vim.split(content:gsub(item.label, ""), "\n") ---@type string[]
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
end
end, 100)
callback()
end)
end
return CommandsSource