Various fixes from the completion merge

This commit is contained in:
Kovid Goyal 2022-09-26 11:22:48 +05:30
parent 97716fea8b
commit 262e2fb7a3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
8 changed files with 15 additions and 9 deletions

View File

@ -47,6 +47,7 @@ def replace(template: str, **kw: str) -> str:
# }}}
# Completions {{{
def generate_kittens_completion() -> None:
from kittens.runner import (
all_kitten_names, get_kitten_cli_docs, get_kitten_wrapper_of,
@ -109,6 +110,7 @@ def generate_completions_for_kitty() -> None:
print('plus_open := plus.AddSubCommand(&cli.Command{'
'Name:"open", Group:"Entry points", ArgCompleter: complete_plus_open, ShortDescription: "Open files and URLs"})')
print('for _, og := range k.OptionGroups { plus_open.OptionGroups = append(plus_open.OptionGroups, og.Clone(plus_open)) }')
print('k.AddClone("", plus_open).Name = "+open"')
# kitty +kitten
@ -129,6 +131,7 @@ def generate_completions_for_kitty() -> None:
print('func init() {')
print('cli.RegisterExeForCompletion(kitty)')
print('}')
# }}}
# rc command wrappers {{{

View File

@ -136,7 +136,7 @@ def completion(self: TestCompletion, tdir: str):
add('kitty --start-as ', all_words('minimized', 'maximized', 'fullscreen', 'normal'))
add('kitty -1 ', does_not_have_words('@ls', '@'))
add('kitty --directory ', all_words('bin/', 'sub/'))
add('kitty -1d ', all_words('bin/', 'sub/'))
add('kitty -1d ', all_words('exe1'))
add('kitty -1d', all_words('-1d'))
add('kitty -o a', has_words('allow_remote_control='))
add('kitty --listen-on ', all_words('unix:', 'tcp:'))

View File

@ -209,6 +209,8 @@ func (self *Command) ResetAfterParseArgs() {
o.reset()
}
}
self.option_map = nil
self.IndexOfFirstArg = 0
self.Args = make([]string, 0, 8)
}

View File

@ -91,6 +91,7 @@ func GenerateCompletions(args []string) error {
all_completions := make([]*Completions, 0, 1)
for _, argv := range all_argv {
all_completions = append(all_completions, root.GetCompletions(argv, init_completions[output_type]))
root.ResetAfterParseArgs()
}
output, err := output_serializer(all_completions, shell_state)
if err == nil {

View File

@ -113,7 +113,7 @@ func complete_word(word string, completions *Completions, only_args_allowed bool
}
}
}
if !cmd.SubCommandMustBeFirst && cmd.ArgCompleter != nil {
if cmd.SubCommandIsOptional && cmd.ArgCompleter != nil {
cmd.ArgCompleter(completions, word, arg_num)
}
return

View File

@ -107,10 +107,10 @@ type Completions struct {
Groups []*MatchGroup `json:"groups,omitempty"`
Delegate Delegate `json:"delegate,omitempty"`
CurrentCmd *Command
AllWords []string // all words passed to parse_args()
CurrentWordIdx int // index of current word in all_words
CurrentWordIdxInParent int // index of current word in parents command line 1 for first word after parent
CurrentCmd *Command `json:"-"`
AllWords []string `json:"-"` // all words passed to parse_args()
CurrentWordIdx int `json:"-"` // index of current word in all_words
CurrentWordIdxInParent int `json:"-"` // index of current word in parents command line 1 for first word after parent
split_on_equals bool // true if the cmdline is split on = (BASH does this because readline does this)
}

View File

@ -24,9 +24,9 @@ func (self *CommandGroup) HasVisibleSubCommands() bool {
}
func (self *CommandGroup) Clone(parent *Command) *CommandGroup {
ans := CommandGroup{Title: self.Title, SubCommands: make([]*Command, 0, len(self.SubCommands))}
ans := CommandGroup{Title: self.Title, SubCommands: make([]*Command, len(self.SubCommands))}
for i, o := range self.SubCommands {
self.SubCommands[i] = o.Clone(parent)
ans.SubCommands[i] = o.Clone(parent)
}
return &ans
}

View File

@ -95,7 +95,7 @@ func run_CMD_NAME(cmd *cli.Command, args []string) (return_code int, err error)
func setup_CMD_NAME(parent *cli.Command) *cli.Command {
ans := parent.AddSubCommand(&cli.Command{
Name: "CMD_NAME",
Name: "CLI_NAME",
Usage: "ARGSPEC",
ShortDescription: "SHORT_DESC",
HelpText: "LONG_DESC",