chore(cmp): update slash commands sources (#206)

and more functional

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
Aaron Pham 2024-08-25 01:59:22 -04:00 committed by GitHub
parent 80250de7a6
commit 2e916b4747
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 63 deletions

View File

@ -627,6 +627,7 @@ function Sidebar:on_mount()
group = self.augroup, group = self.augroup,
buffer = self.result.bufnr, buffer = self.result.bufnr,
callback = function() callback = function()
self:focus()
if self.input and self.input.winid and api.nvim_win_is_valid(self.input.winid) then if self.input and self.input.winid and api.nvim_win_is_valid(self.input.winid) then
api.nvim_set_current_win(self.input.winid) api.nvim_set_current_win(self.input.winid)
end end
@ -1042,75 +1043,73 @@ function Sidebar:get_content_between_separators()
return content return content
end end
---@alias AvanteSlashCommands "clear" | "help" | "lines"
---@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[]
function Sidebar:get_commands() function Sidebar:get_commands()
---@param items_ {command: string, description: string, shorthelp?: string}[]
---@return string
local function get_help_text(items_) local function get_help_text(items_)
local help_text = "" local help_text = ""
for _, item in ipairs(items_) do for _, item in ipairs(items_) do
help_text = help_text .. "- " .. item.name .. ": " .. item.description .. "\n" help_text = help_text .. "- " .. item.command .. ": " .. (item.shorthelp or item.description) .. "\n"
end end
return help_text return help_text
end end
---@type AvanteSlash[]
local items = { local items = {
{ name = "help", description = "Show this help message", command = "help" }, { description = "Show help message", command = "help" },
{ name = "clear", description = "Clear chat history", command = "clear" }, { description = "Clear chat history", command = "clear" },
{ name = "lines <start>-<end> <question>", description = "Ask a question about specific lines", command = "lines" },
}
local cbs = {
{
command = "help",
---@diagnostic disable-next-line: unused-local
callback = function(args, cb)
local help_text = get_help_text(items)
self:update_content(help_text, { focus = false, scroll = false })
if cb then
cb(args)
end
end,
},
{
command = "clear",
---@diagnostic disable-next-line: unused-local
callback = function(args, cb)
local chat_history = {}
save_chat_history(self, chat_history)
self:update_content("Chat history cleared", { focus = false, scroll = false })
vim.defer_fn(function()
self:close()
if cb then
cb(args)
end
end, 1000)
end,
},
{ {
shorthelp = "Ask a question about specific lines",
description = "/lines <start>-<end> <question>",
command = "lines", command = "lines",
callback = function(args, cb)
if cb then
cb(args)
end
end,
}, },
} }
local commands = {} ---@type {[AvanteSlashCommands]: AvanteSlashCallback}
for _, item in ipairs(items) do local cbs = {
table.insert(commands, { help = function(args, cb)
name = item.name, local help_text = get_help_text(items)
command = item.command, self:update_content(help_text, { focus = false, scroll = false })
description = item.description, if cb then
callback = function(args, cb) cb(args)
for _, cb_ in ipairs(cbs) do end
if cb_.command == item.command then end,
cb_.callback(args, cb) clear = function(args, cb)
break local chat_history = {}
end save_chat_history(self, chat_history)
self:update_content("Chat history cleared", { focus = false, scroll = false })
vim.defer_fn(function()
self:close()
if cb then
cb(args)
end end
end, end, 1000)
}) end,
end lines = function(args, cb)
return commands if cb then
cb(args)
end
end,
}
return vim
.iter(items)
:map(
---@param item AvanteSlash
function(item)
return {
command = item.command,
description = item.description,
callback = cbs[item.command],
details = item.shorthelp and table.concat({ item.shorthelp, item.description }, "\n") or item.description,
}
end
)
:totable()
end end
function Sidebar:create_selected_code() function Sidebar:create_selected_code()
@ -1190,13 +1189,13 @@ function Sidebar:create_input()
return return
end end
local cmds = self:get_commands() local cmds = self:get_commands()
local cmd ---@type AvanteSlash
for _, c in ipairs(cmds) do local cmd = vim
if c.command == command then .iter(cmds)
cmd = c :filter(function(_)
break return _.command == command
end end)
end :totable()[1]
if cmd then if cmd then
if command == "lines" then if command == "lines" then
cmd.callback(args, function(args_) cmd.callback(args, function(args_)

View File

@ -35,9 +35,9 @@ function source:complete(_, callback)
for _, command in ipairs(commands) do for _, command in ipairs(commands) do
table.insert(items, { table.insert(items, {
label = "/" .. command.name, label = "/" .. command.command,
kind = kind, kind = kind,
detail = command.description, detail = command.details,
}) })
end end