avante.nvim/lua/cmp_avante/commands.lua

46 lines
1.1 KiB
Lua
Raw Normal View History

local api = vim.api
---@class commands_source
---@field commands AvanteSlashCommand[]
---@field bufnr integer
local commands_source = {}
2024-08-24 04:32:44 +08:00
---@param commands AvanteSlashCommand[]
---@param bufnr integer
function commands_source.new(commands, bufnr)
---@type cmp.Source
2024-08-24 04:32:44 +08:00
return setmetatable({
commands = commands,
bufnr = bufnr,
}, { __index = commands_source })
2024-08-24 04:32:44 +08:00
end
function commands_source:is_available() return api.nvim_get_current_buf() == self.bufnr end
2024-08-24 04:32:44 +08:00
commands_source.get_position_encoding_kind = function() return "utf-8" end
2024-08-24 04:32:44 +08:00
function commands_source:get_trigger_characters() return { "/" } end
2024-08-24 04:32:44 +08:00
function commands_source:get_keyword_pattern() return [[\%(@\|#\|/\)\k*]] end
2024-08-24 04:32:44 +08:00
function commands_source:complete(_, callback)
local kind = require("cmp").lsp.CompletionItemKind.Variable
2024-08-24 04:32:44 +08:00
local items = {}
for _, command in ipairs(self.commands) do
2024-08-24 04:32:44 +08:00
table.insert(items, {
label = "/" .. command.command,
2024-08-24 04:32:44 +08:00
kind = kind,
detail = command.details,
2024-08-24 04:32:44 +08:00
})
end
callback({
2024-08-24 04:32:44 +08:00
items = items,
isIncomplete = false,
})
2024-08-24 04:32:44 +08:00
end
return commands_source