fix: cmp slash commands disappeared (#869)
This commit is contained in:
parent
cf2312abbc
commit
c551bbe5b6
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user