Add an entry point for the completion tool

This commit is contained in:
Kovid Goyal
2022-09-06 13:17:15 +05:30
parent f4de6d2a10
commit 005a9c7090
4 changed files with 183 additions and 59 deletions

View File

@@ -3,44 +3,62 @@
package completion
type Match struct {
Word string
FullForm string
Description string
Word string `json:"word,omitempty"`
FullForm string `json:"full_form,omitempty"`
Description string `json:"description,omitempty"`
}
type MatchGroup struct {
Title string
NoTrailingSpace, IsFiles bool
Matches []*Match
WordPrefix string
Title string `json:"title,omitempty"`
NoTrailingSpace bool `json:"no_trailing_space,omitempty"`
IsFiles bool `json:"is_files,omitempty"`
Matches []*Match `json:"matches,omitempty"`
WordPrefix string `json:"word_prefix,omitempty"`
}
type Completions struct {
Groups []*MatchGroup
WordPrefix string
Groups []*MatchGroup `json:"groups,omitempty"`
WordPrefix string `json:"word_prefix,omitempty"`
current_cmd *command
current_cmd *Command
}
type completion_func func(completions *Completions, partial_word string)
type option struct {
name string
aliases []string
description string
has_following_arg bool
completion_for_arg completion_func
type Option struct {
Name string
Aliases []string
Description string
Has_following_arg bool
Completion_for_arg completion_func
}
type command struct {
name string
description string
type Command struct {
Name string
Description string
options []option
// List of options for this command
Options []*Option
subcommands []command
subcommands_title string
// List of subcommands
Subcommands []*Command
// Optional title used as a header when displaying the list of matching sub-commands for a completion
Subcommands_title string
completion_for_arg completion_func
stop_processing_at_arg int
Completion_for_arg completion_func
Stop_processing_at_arg int
}
var Root = Command{Options: make([]*Option, 0), Subcommands: make([]*Command, 0, 32)}
func GetCompletions(argv []string) *Completions {
ans := Completions{Groups: make([]*MatchGroup, 0, 4)}
if len(argv) > 0 {
exe := argv[0]
cmd := Root.find_subcommand(exe)
if cmd != nil {
cmd.parse_args(argv[1:], &ans)
}
}
return &ans
}