You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
---@classMyCmdSubcommand---@fieldimplfun(args:string[], opts: table) The command implementation---@fieldcomplete? fun(subcmd_arg_lead: string): string[] (optional) Command completions callback, taking the lead of the subcommand's arguments---@typetable<string, MyCmdSubcommand>localsubcommand_tbl= {
update= {
impl=function(args, opts)
-- Implementation (args is a list of strings)end,
-- This subcommand has no completions
},
install= {
impl=function(args, opts)
-- Implementationend,
complete=function(subcmd_arg_lead)
-- Simplified examplelocalinstall_args= {
"neorg",
"rest.nvim",
"rustaceanvim",
}
returnvim.iter(install_args)
:filter(function(install_arg)
-- If the user has typed `:Rocks install ne`,-- this will match 'neorg'returninstall_arg:find(subcmd_arg_lead) ~=nilend)
:totable()
end,
-- ...
},
}
---@paramoptstable :h lua-guide-commands-createlocalfunctionmy_cmd(opts)
localfargs=opts.fargslocalsubcommand_key=fargs[1]
-- Get the subcommand's arguments, if anylocalargs=#fargs>1andvim.list_slice(fargs, 2, #fargs) or {}
localsubcommand=subcommand_tbl[subcommand_key]
ifnotsubcommandthenvim.notify("Rocks: Unknown command: " ..cmd, vim.log.levels.ERROR)
returnend-- Invoke the subcommandsubcommand.impl(args, opts)
end-- NOTE: the options will vary, based on your use case.vim.api.nvim_create_user_command("Rocks", my_cmd, {
nargs="+",
desc="My awesome command with subcommand completions",
complete=function(arg_lead, cmdline, _)
-- Get the subcommand.localsubcmd_key, subcmd_arg_lead=cmdline:match("^Rocks[!]*%s(%S+)%s(.*)$")
ifsubcmd_keyandsubcmd_arg_leadandsubcommand_tbl[subcmd_key] andsubcommand_tbl[subcmd_key].completethen-- The subcommand has completions. Return them.returnsubcommand_tbl[subcmd_key].complete(subcmd_arg_lead)
end-- Check if cmdline is a subcommandifcmdline:match("^Rocks[!]*%s+%w*$") then-- Filter subcommands that matchlocalsubcommand_keys=vim.tbl_keys(subcommand_tbl)
returnvim.iter(subcommand_keys)
:filter(function(key)
returnkey:find(arg_lead) ~=nilend)
:totable()
endend,
bang=true, -- If you want to support ! modifiers
})
Add that to some plugin/foo.lua in 'runtimepath' and type :Rocks install ne. The Neovim command-line should complete to ne to neorg but doesn't.
Also worth mentioning that there's a bug in the README.md. The snippet above as the fix listed in #3
The text was updated successfully, but these errors were encountered:
Add that to some
plugin/foo.lua
in'runtimepath'
and type:Rocks install ne
. The Neovim command-line should complete tone
toneorg
but doesn't.Also worth mentioning that there's a bug in the
README.md
. The snippet above as the fix listed in #3The text was updated successfully, but these errors were encountered: