Skip to content

Commit

Permalink
fix: cmp slash commands disappeared (yetone#869)
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone authored Nov 18, 2024
1 parent cf2312a commit c551bbe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
41 changes: 13 additions & 28 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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" },
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
19 changes: 11 additions & 8 deletions lua/cmp_avante/commands.lua
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion lua/cmp_avante/mentions.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down

0 comments on commit c551bbe

Please sign in to comment.