argparse icon indicating copy to clipboard operation
argparse copied to clipboard

[feature] complete for neovim

Open Freed-Wu opened this issue 1 year ago • 1 comments

https://github.com/mpeterv/argparse/issues/28

local parser = argparse("git", "a git implemented by lua")
parser:option("-C", "run as if git was started in given path", get_toplevel(getcwd()))

parser:command("init", "Create an empty Git repository or reinitialize an existing one")
    :argument("directory", "Where to init the repository (optional)", getcwd())

local cmd = parser:command("add", "Add file contents to the index")
cmd:option("-A", "add, modify, and remove index entries to match the working tree")
cmd:argument("file", "file to be added", "")

vim.api.nvim_create_user_command("Git", function(input)
    local args = parser:parse(input.fargs)
    vim.print(args)
end, { nargs = "*" })

:luafile test.lua in neovim, you can get a :Git command.

Notice

nvim_create_user_command({name}, {command}, {opts})
      • {opts}     Optional |command-attributes|.
                   • "complete" |:command-complete| also accepts a Lua
                     function which works like
                     |:command-completion-customlist|.

				*:command-completion-customlist* *E467* *E468*
It is possible to define customized completion schemes via the "custom,{func}"
or the "customlist,{func}" completion argument.  The {func} part should be a
function with the following signature: >

	:function {func}(ArgLead, CmdLine, CursorPos)

The function need not use all these arguments. The function should provide the
completion candidates as the return value.

For the "customlist" argument, the function should return the completion
candidates as a Vim List.  Non-string items in the list are ignored.

The function arguments are:
	ArgLead		the leading portion of the argument currently being
			completed on
	CmdLine		the entire command line
	CursorPos	the cursor position in it (byte index)
The function may use these for determining context.  For the "custom"
argument, it is not necessary to filter candidates against the (implicit
pattern in) ArgLead.  Vim will filter the candidates with its regexp engine
after function return, and this is probably more efficient in most cases. If
'wildoptions' contains "fuzzy", then the candidates will be filtered using
|fuzzy-matching|.  For the "customlist" argument, Vim will not
filter the returned completion candidates and the user supplied function
should filter the candidates.

If parser have a method complete(ArgLead, CmdLine, CursorPos), You can call

vim.api.nvim_create_user_command("Git", function(input)
    local args = parser:parse(input.fargs)
    vim.print(args)
end, { nargs = "*", complete = parser.complete })

to get :Git <TAB> in neovim.

Can we have a API like parser.complete()?

Freed-Wu avatar Dec 02 '24 01:12 Freed-Wu

https://github.com/ColinKennedy/mega.cmdparse

Image

mega.cmdparse has different syntax from argparse.

I provide a compatible API to combine them.

Freed-Wu avatar Feb 02 '25 13:02 Freed-Wu