fix: cmp slash commands disappeared (#869)

This commit is contained in:
yetone 2024-11-19 06:01:50 +08:00 committed by GitHub
parent cf2312abbc
commit c551bbe5b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 37 deletions

View File

@ -1099,7 +1099,7 @@ local function get_timestamp() return os.date("%Y-%m-%d %H:%M:%S") end
---@param provider string ---@param provider string
---@param model string ---@param model string
---@param request string ---@param request string
---@param selected_file {filepath: string, content: string}? ---@param selected_file {filepath: string}?
---@param selected_code {filetype: string, content: string}? ---@param selected_code {filetype: string, content: string}?
---@return string ---@return string
local function render_chat_record_prefix(timestamp, provider, model, request, selected_file, selected_code) 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 return content, start_line
end end
---@alias AvanteSlashCommands "clear" | "help" | "lines" | "reset" ---@alias AvanteSlashCommandType "clear" | "help" | "lines" | "reset"
---@alias AvanteSlashCallback fun(args: string, cb?: fun(args: string): nil): nil ---@alias AvanteSlashCommandCallback fun(args: string, cb?: fun(args: string): nil): nil
---@alias AvanteSlash {description: string, command: AvanteSlashCommands, details: string, shorthelp?: string, callback?: AvanteSlashCallback} ---@alias AvanteSlashCommand {description: string, command: AvanteSlashCommandType, details: string, shorthelp?: string, callback?: AvanteSlashCommandCallback}
---@return AvanteSlash[] ---@return AvanteSlashCommand[]
function Sidebar:get_commands() function Sidebar:get_commands()
---@param items_ {command: string, description: string, shorthelp?: string}[] ---@param items_ {command: string, description: string, shorthelp?: string}[]
---@return string ---@return string
@ -1222,7 +1222,7 @@ function Sidebar:get_commands()
return help_text return help_text
end end
---@type AvanteSlash[] ---@type AvanteSlashCommand[]
local items = { local items = {
{ description = "Show help message", command = "help" }, { description = "Show help message", command = "help" },
{ description = "Clear chat history", command = "clear" }, { description = "Clear chat history", command = "clear" },
@ -1234,7 +1234,7 @@ function Sidebar:get_commands()
}, },
} }
---@type {[AvanteSlashCommands]: AvanteSlashCallback} ---@type {[AvanteSlashCommandType]: AvanteSlashCommandCallback}
local cbs = { local cbs = {
help = function(args, cb) help = function(args, cb)
local help_text = get_help_text(items) local help_text = get_help_text(items)
@ -1282,7 +1282,7 @@ function Sidebar:get_commands()
return vim return vim
.iter(items) .iter(items)
:map( :map(
---@param item AvanteSlash ---@param item AvanteSlashCommand
function(item) function(item)
return { return {
command = item.command, command = item.command,
@ -1377,7 +1377,7 @@ function Sidebar:create_input(opts)
return return
end end
local cmds = self:get_commands() local cmds = self:get_commands()
---@type AvanteSlash ---@type AvanteSlashCommand
local cmd = vim.iter(cmds):filter(function(_) return _.command == command end):totable()[1] local cmd = vim.iter(cmds):filter(function(_) return _.command == command end):totable()[1]
if cmd then if cmd then
if command == "lines" then if command == "lines" then
@ -1594,7 +1594,10 @@ function Sidebar:create_input(opts)
callback = function() callback = function()
local has_cmp, cmp = pcall(require, "cmp") local has_cmp, cmp = pcall(require, "cmp")
if has_cmp then 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( cmp.register_source(
"avante_mentions", "avante_mentions",
require("cmp_avante.mentions").new(Utils.get_mentions(), self.input.bufnr) require("cmp_avante.mentions").new(Utils.get_mentions(), self.input.bufnr)
@ -1610,24 +1613,6 @@ function Sidebar:create_input(opts)
end, 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 -- Close the floating window
local function close_hint() local function close_hint()
if hint_window and api.nvim_win_is_valid(hint_window) then if hint_window and api.nvim_win_is_valid(hint_window) then

View File

@ -1,16 +1,21 @@
local api = vim.api
---@class commands_source ---@class commands_source
---@field sidebar avante.Sidebar ---@field commands AvanteSlashCommand[]
---@field bufnr integer
local commands_source = {} local commands_source = {}
---@param sidebar avante.Sidebar ---@param commands AvanteSlashCommand[]
function commands_source.new(sidebar) ---@param bufnr integer
function commands_source.new(commands, bufnr)
---@type cmp.Source ---@type cmp.Source
return setmetatable({ return setmetatable({
sidebar = sidebar, commands = commands,
bufnr = bufnr,
}, { __index = commands_source }) }, { __index = commands_source })
end 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 commands_source.get_position_encoding_kind = function() return "utf-8" end
@ -23,9 +28,7 @@ function commands_source:complete(_, callback)
local items = {} local items = {}
local commands = self.sidebar:get_commands() for _, command in ipairs(self.commands) do
for _, command in ipairs(commands) do
table.insert(items, { table.insert(items, {
label = "/" .. command.command, label = "/" .. command.command,
kind = kind, kind = kind,

View File

@ -1,3 +1,5 @@
local api = vim.api
---@class mentions_source ---@class mentions_source
---@field mentions {description: string, command: AvanteMentions, details: string, shorthelp?: string, callback?: AvanteMentionCallback}[] ---@field mentions {description: string, command: AvanteMentions, details: string, shorthelp?: string, callback?: AvanteMentionCallback}[]
---@field bufnr integer ---@field bufnr integer
@ -13,7 +15,7 @@ function mentions_source.new(mentions, bufnr)
}, { __index = mentions_source }) }, { __index = mentions_source })
end 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 mentions_source.get_position_encoding_kind = function() return "utf-8" end