More completion work

This commit is contained in:
Kovid Goyal
2022-09-16 17:29:41 +05:30
parent 946d44c43f
commit 0ff2446a1a
7 changed files with 46 additions and 5 deletions

View File

@@ -250,3 +250,20 @@ func fnmatch_completer(title string, relative_to relative_to, patterns ...string
func mimepat_completer(title string, relative_to relative_to, patterns ...string) completion_func {
return make_completer(title, relative_to, patterns, complete_by_mimepat)
}
func directory_completer(title string, relative_to relative_to) completion_func {
if title == "" {
title = "Directories"
}
cwd := get_cwd_for_completion(relative_to)
return func(completions *Completions, word string, arg_num int) {
mg := completions.add_match_group(title)
mg.IsFiles = true
complete_files(word, func(entry *FileEntry) {
if entry.mode.IsDir() {
mg.add_match(entry.completion_candidate)
}
}, cwd)
}
}

View File

@@ -14,6 +14,9 @@ import (
var _ = fmt.Print
func complete_kitty(completions *Completions, word string, arg_num int) {
if arg_num > 1 {
return
}
exes := complete_executables_in_path(word)
if len(exes) > 0 {
mg := completions.add_match_group("Executables in PATH")

View File

@@ -100,7 +100,7 @@ func complete_word(word string, completions *Completions, only_args_allowed bool
}
return
}
if arg_num == 1 && cmd.has_subcommands() {
if cmd.has_subcommands() && cmd.sub_command_allowed_at(completions, arg_num) {
for _, cg := range cmd.Groups {
group := completions.add_match_group(cg.Title)
if group.Title == "" {
@@ -163,7 +163,7 @@ func (cmd *Command) parse_args(words []string, completions *Completions) {
}
continue
}
if cmd.has_subcommands() && arg_num == 1 {
if cmd.has_subcommands() && cmd.sub_command_allowed_at(completions, arg_num) {
sc := cmd.find_subcommand_with_name(word)
if sc == nil {
only_args_allowed = true

View File

@@ -68,6 +68,7 @@ type Command struct {
Completion_for_arg completion_func
Stop_processing_at_arg int
First_arg_may_not_be_subcommand bool
Subcommand_must_be_first bool
}
func (self *Command) add_group(name string) *CommandGroup {
@@ -122,6 +123,13 @@ func (self *Command) has_subcommands() bool {
return false
}
func (self *Command) sub_command_allowed_at(completions *Completions, arg_num int) bool {
if self.Subcommand_must_be_first {
return arg_num == 1 && completions.current_word_idx == 0
}
return arg_num == 1
}
func (self *Command) add_option(opt *Option) {
self.Options = append(self.Options, opt)
}