From c551bbe5b6a95765e50659e1472f0f670f9d36c5 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 19 Nov 2024 06:01:50 +0800 Subject: [PATCH] fix: cmp slash commands disappeared (#869) --- lua/avante/sidebar.lua | 41 ++++++++++++------------------------- lua/cmp_avante/commands.lua | 19 +++++++++-------- lua/cmp_avante/mentions.lua | 4 +++- 3 files changed, 27 insertions(+), 37 deletions(-) diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 68cde47..328fe81 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -1099,7 +1099,7 @@ local function get_timestamp() return os.date("%Y-%m-%d %H:%M:%S") end ---@param provider string ---@param model string ---@param request string ----@param selected_file {filepath: string, content: string}? +---@param selected_file {filepath: string}? ---@param selected_code {filetype: string, content: string}? ---@return string local function render_chat_record_prefix(timestamp, provider, model, request, selected_file, selected_code) @@ -1207,10 +1207,10 @@ function Sidebar:get_content_between_separators() return content, start_line end ----@alias AvanteSlashCommands "clear" | "help" | "lines" | "reset" ----@alias AvanteSlashCallback fun(args: string, cb?: fun(args: string): nil): nil ----@alias AvanteSlash {description: string, command: AvanteSlashCommands, details: string, shorthelp?: string, callback?: AvanteSlashCallback} ----@return AvanteSlash[] +---@alias AvanteSlashCommandType "clear" | "help" | "lines" | "reset" +---@alias AvanteSlashCommandCallback fun(args: string, cb?: fun(args: string): nil): nil +---@alias AvanteSlashCommand {description: string, command: AvanteSlashCommandType, details: string, shorthelp?: string, callback?: AvanteSlashCommandCallback} +---@return AvanteSlashCommand[] function Sidebar:get_commands() ---@param items_ {command: string, description: string, shorthelp?: string}[] ---@return string @@ -1222,7 +1222,7 @@ function Sidebar:get_commands() return help_text end - ---@type AvanteSlash[] + ---@type AvanteSlashCommand[] local items = { { description = "Show help message", command = "help" }, { description = "Clear chat history", command = "clear" }, @@ -1234,7 +1234,7 @@ function Sidebar:get_commands() }, } - ---@type {[AvanteSlashCommands]: AvanteSlashCallback} + ---@type {[AvanteSlashCommandType]: AvanteSlashCommandCallback} local cbs = { help = function(args, cb) local help_text = get_help_text(items) @@ -1282,7 +1282,7 @@ function Sidebar:get_commands() return vim .iter(items) :map( - ---@param item AvanteSlash + ---@param item AvanteSlashCommand function(item) return { command = item.command, @@ -1377,7 +1377,7 @@ function Sidebar:create_input(opts) return end local cmds = self:get_commands() - ---@type AvanteSlash + ---@type AvanteSlashCommand local cmd = vim.iter(cmds):filter(function(_) return _.command == command end):totable()[1] if cmd then if command == "lines" then @@ -1594,7 +1594,10 @@ function Sidebar:create_input(opts) callback = function() local has_cmp, cmp = pcall(require, "cmp") if has_cmp then - cmp.register_source("avante_commands", require("cmp_avante.commands").new(self)) + cmp.register_source( + "avante_commands", + require("cmp_avante.commands").new(self:get_commands(), self.input.bufnr) + ) cmp.register_source( "avante_mentions", require("cmp_avante.mentions").new(Utils.get_mentions(), self.input.bufnr) @@ -1610,24 +1613,6 @@ function Sidebar:create_input(opts) end, }) - -- Unregister completion - api.nvim_create_autocmd("BufLeave", { - group = self.augroup, - buffer = self.input.bufnr, - once = false, - desc = "Unregister the completion of helpers in the input buffer", - callback = function() - local has_cmp, cmp = pcall(require, "cmp") - if has_cmp then - for _, source in ipairs(cmp.core:get_sources()) do - if source.name == "avante_commands" or source.name == "avante_mentions" then - cmp.unregister_source(source.id) - end - end - end - end, - }) - -- Close the floating window local function close_hint() if hint_window and api.nvim_win_is_valid(hint_window) then diff --git a/lua/cmp_avante/commands.lua b/lua/cmp_avante/commands.lua index 9546937..9d53015 100644 --- a/lua/cmp_avante/commands.lua +++ b/lua/cmp_avante/commands.lua @@ -1,16 +1,21 @@ +local api = vim.api + ---@class commands_source ----@field sidebar avante.Sidebar +---@field commands AvanteSlashCommand[] +---@field bufnr integer local commands_source = {} ----@param sidebar avante.Sidebar -function commands_source.new(sidebar) +---@param commands AvanteSlashCommand[] +---@param bufnr integer +function commands_source.new(commands, bufnr) ---@type cmp.Source return setmetatable({ - sidebar = sidebar, + commands = commands, + bufnr = bufnr, }, { __index = commands_source }) end -function commands_source:is_available() return vim.bo.filetype == "AvanteInput" end +function commands_source:is_available() return api.nvim_get_current_buf() == self.bufnr end commands_source.get_position_encoding_kind = function() return "utf-8" end @@ -23,9 +28,7 @@ function commands_source:complete(_, callback) local items = {} - local commands = self.sidebar:get_commands() - - for _, command in ipairs(commands) do + for _, command in ipairs(self.commands) do table.insert(items, { label = "/" .. command.command, kind = kind, diff --git a/lua/cmp_avante/mentions.lua b/lua/cmp_avante/mentions.lua index 3e58f0c..558640e 100644 --- a/lua/cmp_avante/mentions.lua +++ b/lua/cmp_avante/mentions.lua @@ -1,3 +1,5 @@ +local api = vim.api + ---@class mentions_source ---@field mentions {description: string, command: AvanteMentions, details: string, shorthelp?: string, callback?: AvanteMentionCallback}[] ---@field bufnr integer @@ -13,7 +15,7 @@ function mentions_source.new(mentions, bufnr) }, { __index = mentions_source }) end -function mentions_source:is_available() return vim.api.nvim_get_current_buf() == self.bufnr end +function mentions_source:is_available() return api.nvim_get_current_buf() == self.bufnr end mentions_source.get_position_encoding_kind = function() return "utf-8" end