From 30851656591293ed2e19340ab78c937855a11143 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Tue, 17 Oct 2023 00:14:49 +0200 Subject: commands: remove command set completion api Do not expose the completion of a command via its command set. Instead, require a single command object to be resolved in order to execute it. Extract the command names and the template terms completions in main.go. Signed-off-by: Robin Jarry Reviewed-by: Koni Marti Tested-by: Moritz Poldrack Tested-by: Inwit --- main.go | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 43e980e9..dc7d0267 100644 --- a/main.go +++ b/main.go @@ -125,21 +125,38 @@ func execCommand( return err } -func getCompletions(cmd string) ([]string, string) { - if options, prefix, ok := commands.GetTemplateCompletion(cmd); ok { +func getCompletions(cmdline string) ([]string, string) { + cmdline = strings.TrimLeft(cmdline, ":") + + // complete template terms + if options, prefix, ok := commands.GetTemplateCompletion(cmdline); ok { + sort.Strings(options) return options, prefix } - var completions []string - var prefix string - for _, set := range getCommands(app.SelectedTabContent()) { - options, s := set.GetCompletions(cmd) - if s != "" { - prefix = s + + args := opt.LexArgs(cmdline) + cmds := getCommands(app.SelectedTabContent()) + + if args.Count() < 2 && args.TrailingSpace() == "" { + // complete command names + var completions []string + for _, set := range cmds { + for _, n := range set.Names() { + if strings.HasPrefix(n, cmdline) { + completions = append(completions, n) + } + } } - completions = append(completions, options...) + sort.Strings(completions) + return completions, "" + } + + // complete command arguments + _, cmd := expandAbbreviations(args.Arg(0), cmds) + if cmd == nil { + return nil, cmdline } - sort.Strings(completions) - return completions, prefix + return commands.GetCompletions(cmd, args) } // set at build time -- cgit