From 262e2fb7a3da6bdd19c5a66d7f2bd28eb183fe0a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 26 Sep 2022 11:22:48 +0530 Subject: [PATCH] Various fixes from the completion merge --- gen-go-code.py | 3 +++ kitty_tests/completion.py | 2 +- tools/cli/command.go | 2 ++ tools/cli/completion-main.go | 1 + tools/cli/completion-parse-args.go | 2 +- tools/cli/completion.go | 8 ++++---- tools/cli/group.go | 4 ++-- tools/cmd/at/template.go | 2 +- 8 files changed, 15 insertions(+), 9 deletions(-) diff --git a/gen-go-code.py b/gen-go-code.py index f660fcbb4..423f86b69 100755 --- a/gen-go-code.py +++ b/gen-go-code.py @@ -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 {{{ diff --git a/kitty_tests/completion.py b/kitty_tests/completion.py index ff23ba3c0..b97a4b530 100644 --- a/kitty_tests/completion.py +++ b/kitty_tests/completion.py @@ -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:')) diff --git a/tools/cli/command.go b/tools/cli/command.go index 76ec0c5e0..a0d99f5ef 100644 --- a/tools/cli/command.go +++ b/tools/cli/command.go @@ -209,6 +209,8 @@ func (self *Command) ResetAfterParseArgs() { o.reset() } } + self.option_map = nil + self.IndexOfFirstArg = 0 self.Args = make([]string, 0, 8) } diff --git a/tools/cli/completion-main.go b/tools/cli/completion-main.go index 5c560c1b6..d787c1e55 100644 --- a/tools/cli/completion-main.go +++ b/tools/cli/completion-main.go @@ -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 { diff --git a/tools/cli/completion-parse-args.go b/tools/cli/completion-parse-args.go index 335902685..0d79e4ad3 100644 --- a/tools/cli/completion-parse-args.go +++ b/tools/cli/completion-parse-args.go @@ -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 diff --git a/tools/cli/completion.go b/tools/cli/completion.go index 393710bf2..93e5ac3ba 100644 --- a/tools/cli/completion.go +++ b/tools/cli/completion.go @@ -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) } diff --git a/tools/cli/group.go b/tools/cli/group.go index 29d412a37..040766635 100644 --- a/tools/cli/group.go +++ b/tools/cli/group.go @@ -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 } diff --git a/tools/cmd/at/template.go b/tools/cmd/at/template.go index 8d41c36e6..d5dd5398f 100644 --- a/tools/cmd/at/template.go +++ b/tools/cmd/at/template.go @@ -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",