refactor(cmp): Extract get_chat_mentions for third-party cmp source usage (#2027)

This commit is contained in:
aniaan
2025-05-12 20:19:42 +08:00
committed by GitHub
parent 786d95464c
commit aae4cc4014
4 changed files with 35 additions and 34 deletions

View File

@@ -492,35 +492,7 @@ function M.setup(opts)
if has_cmp then
cmp.register_source("avante_commands", require("cmp_avante.commands"):new())
cmp.register_source(
"avante_mentions",
require("cmp_avante.mentions"):new(function()
local mentions = Utils.get_mentions()
table.insert(mentions, {
description = "file",
command = "file",
details = "add files...",
callback = function(sidebar) sidebar.file_selector:open() end,
})
table.insert(mentions, {
description = "quickfix",
command = "quickfix",
details = "add files in quickfix list to chat context",
callback = function(sidebar) sidebar.file_selector:add_quickfix_files() end,
})
table.insert(mentions, {
description = "buffers",
command = "buffers",
details = "add open buffers to the chat context",
callback = function(sidebar) sidebar.file_selector:add_buffer_files() end,
})
return mentions
end)
)
cmp.register_source("avante_mentions", require("cmp_avante.mentions"):new(Utils.get_chat_mentions))
cmp.register_source("avante_prompt_mentions", require("cmp_avante.mentions"):new(Utils.get_mentions))

View File

@@ -452,3 +452,7 @@ vim.g.avante_login = vim.g.avante_login
---@field details string
---@field shorthelp? string
---@field callback? AvanteSlashCommandCallback
---@alias AvanteMentions "codebase" | "diagnostics" | "file" | "quickfix" | "buffers"
---@alias AvanteMentionCallback fun(args: string, cb?: fun(args: string): nil): nil
---@alias AvanteMention {description: string, command: AvanteMentions, details: string, shorthelp?: string, callback?: AvanteMentionCallback}

View File

@@ -913,9 +913,6 @@ function M.extract_mentions(content)
}
end
---@alias AvanteMentions "codebase" | "diagnostics"
---@alias AvanteMentionCallback fun(args: string, cb?: fun(args: string): nil): nil
---@alias AvanteMention {description: string, command: AvanteMentions, details: string, shorthelp?: string, callback?: AvanteMentionCallback}
---@return AvanteMention[]
function M.get_mentions()
return {
@@ -932,6 +929,34 @@ function M.get_mentions()
}
end
---@return AvanteMention[]
function M.get_chat_mentions()
local mentions = M.get_mentions()
table.insert(mentions, {
description = "file",
command = "file",
details = "add files...",
callback = function(sidebar) sidebar.file_selector:open() end,
})
table.insert(mentions, {
description = "quickfix",
command = "quickfix",
details = "add files in quickfix list to chat context",
callback = function(sidebar) sidebar.file_selector:add_quickfix_files() end,
})
table.insert(mentions, {
description = "buffers",
command = "buffers",
details = "add open buffers to the chat context",
callback = function(sidebar) sidebar.file_selector:add_buffer_files() end,
})
return mentions
end
---@param path string
---@param set_current_buf? boolean
---@return integer bufnr

View File

@@ -1,11 +1,11 @@
local api = vim.api
---@class mentions_source : cmp.Source
---@field get_mentions fun(): {description: string, command: AvanteMentions, details: string, shorthelp?: string, callback?: AvanteMentionCallback}[]
---@field get_mentions fun(): AvanteMention[]
local MentionsSource = {}
MentionsSource.__index = MentionsSource
---@param get_mentions fun(): {description: string, command: AvanteMentions, details: string, shorthelp?: string, callback?: AvanteMentionCallback}[]
---@param get_mentions fun(): AvanteMention[]
function MentionsSource:new(get_mentions)
local instance = setmetatable({}, MentionsSource)