mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-08 22:28:24 +02:00
More work on merging completions with parse tree
This commit is contained in:
@@ -40,8 +40,8 @@ type Command struct {
|
||||
|
||||
Args []string
|
||||
|
||||
option_map map[string]*Option
|
||||
index_of_first_arg int
|
||||
option_map map[string]*Option
|
||||
IndexOfFirstArg int
|
||||
}
|
||||
|
||||
func (self *Command) Clone(parent *Command) *Command {
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"kitty/tools/utils"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func complete_kitty(completions *Completions, word string, arg_num int) {
|
||||
if arg_num > 1 {
|
||||
completions.Delegate.NumToRemove = completions.current_cmd.index_of_first_arg + 1 // +1 because the first word is not present in all_words
|
||||
completions.Delegate.Command = completions.all_words[completions.current_cmd.index_of_first_arg]
|
||||
return
|
||||
}
|
||||
exes := complete_executables_in_path(word)
|
||||
if len(exes) > 0 {
|
||||
mg := completions.add_match_group("Executables in PATH")
|
||||
for _, exe := range exes {
|
||||
mg.add_match(exe)
|
||||
}
|
||||
}
|
||||
|
||||
if len(word) > 0 {
|
||||
mg := completions.add_match_group("Executables")
|
||||
mg.IsFiles = true
|
||||
|
||||
complete_files(word, func(entry *FileEntry) {
|
||||
if entry.is_dir && !entry.is_empty_dir {
|
||||
// only allow directories that have sub-dirs or executable files in them
|
||||
entries, err := os.ReadDir(entry.abspath)
|
||||
if err == nil {
|
||||
for _, x := range entries {
|
||||
if x.IsDir() || unix.Access(filepath.Join(entry.abspath, x.Name()), unix.X_OK) == nil {
|
||||
mg.add_match(entry.completion_candidate)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if unix.Access(entry.abspath, unix.X_OK) == nil {
|
||||
mg.add_match(entry.completion_candidate)
|
||||
}
|
||||
}, "")
|
||||
}
|
||||
}
|
||||
|
||||
func complete_kitty_override(title string, names []string) CompletionFunc {
|
||||
return func(completions *Completions, word string, arg_num int) {
|
||||
mg := completions.add_match_group(title)
|
||||
mg.NoTrailingSpace = true
|
||||
for _, q := range names {
|
||||
if strings.HasPrefix(q, word) {
|
||||
mg.add_match(q + "=")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func complete_kitty_listen_on(completions *Completions, word string, arg_num int) {
|
||||
if !strings.Contains(word, ":") {
|
||||
mg := completions.add_match_group("Address family")
|
||||
mg.NoTrailingSpace = true
|
||||
for _, q := range []string{"unix:", "tcp:"} {
|
||||
if strings.HasPrefix(q, word) {
|
||||
mg.add_match(q)
|
||||
}
|
||||
}
|
||||
} else if strings.HasPrefix(word, "unix:") && !strings.HasPrefix(word, "unix:@") {
|
||||
fnmatch_completer("UNIX sockets", CWD, "*")(completions, word[len("unix:"):], arg_num)
|
||||
completions.add_prefix_to_all_matches("unix:")
|
||||
}
|
||||
}
|
||||
|
||||
func complete_plus_launch(completions *Completions, word string, arg_num int) {
|
||||
if arg_num == 1 {
|
||||
fnmatch_completer("Python scripts", CWD, "*.py")(completions, word, arg_num)
|
||||
if strings.HasPrefix(word, ":") {
|
||||
exes := complete_executables_in_path(word[1:])
|
||||
mg := completions.add_match_group("Python scripts in PATH")
|
||||
for _, exe := range exes {
|
||||
mg.add_match(":" + exe)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fnmatch_completer("Files", CWD, "*")(completions, word, arg_num)
|
||||
}
|
||||
}
|
||||
|
||||
func complete_plus_runpy(completions *Completions, word string, arg_num int) {
|
||||
if arg_num > 1 {
|
||||
fnmatch_completer("Files", CWD, "*")(completions, word, arg_num)
|
||||
}
|
||||
}
|
||||
|
||||
func complete_plus_open(completions *Completions, word string, arg_num int) {
|
||||
fnmatch_completer("Files", CWD, "*")(completions, word, arg_num)
|
||||
}
|
||||
|
||||
func complete_themes(completions *Completions, word string, arg_num int) {
|
||||
kitty, err := utils.KittyExe()
|
||||
if err == nil {
|
||||
out, err := exec.Command(kitty, "+runpy", "from kittens.themes.collection import *; print_theme_names()").Output()
|
||||
if err == nil {
|
||||
mg := completions.add_match_group("Themes")
|
||||
scanner := bufio.NewScanner(bytes.NewReader(out))
|
||||
for scanner.Scan() {
|
||||
theme_name := strings.TrimSpace(scanner.Text())
|
||||
if theme_name != "" && strings.HasPrefix(theme_name, word) {
|
||||
mg.add_match(theme_name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func completion_for_wrapper(wrapped_cmd string) func(*Command, []string, *Completions) {
|
||||
return func(cmd *Command, args []string, completions *Completions) {
|
||||
completions.Delegate.NumToRemove = completions.current_word_idx + 1
|
||||
completions.Delegate.Command = wrapped_cmd
|
||||
}
|
||||
}
|
||||
@@ -42,9 +42,16 @@ func init() {
|
||||
output_serializers["json"] = json_output_serializer
|
||||
}
|
||||
|
||||
var registered_exes = make(map[string]func(root *Command))
|
||||
var registered_exes []func(root *Command)
|
||||
|
||||
func Main(args []string) error {
|
||||
func RegisterExeForCompletion(x func(root *Command)) {
|
||||
if registered_exes == nil {
|
||||
registered_exes = make([]func(root *Command), 0, 4)
|
||||
}
|
||||
registered_exes = append(registered_exes, x)
|
||||
}
|
||||
|
||||
func GenerateCompletions(args []string) error {
|
||||
output_type := "json"
|
||||
if len(args) > 0 {
|
||||
output_type = args[0]
|
||||
|
||||
@@ -18,7 +18,7 @@ func (self *Completions) add_group(group *MatchGroup) {
|
||||
}
|
||||
|
||||
func (self *Completions) add_options_group(options []*Option, word string) {
|
||||
group := self.add_match_group("Options")
|
||||
group := self.AddMatchGroup("Options")
|
||||
if strings.HasPrefix(word, "--") {
|
||||
if word == "--" {
|
||||
group.Matches = append(group.Matches, &Match{Word: "--", Description: "End of options"})
|
||||
@@ -38,7 +38,7 @@ func (self *Completions) add_options_group(options []*Option, word string) {
|
||||
has_single_letter_alias := false
|
||||
for _, q := range opt.Aliases {
|
||||
if q.IsShort {
|
||||
group.add_match("-"+q.NameWithoutHyphens, opt.Help)
|
||||
group.AddMatch("-"+q.NameWithoutHyphens, opt.Help)
|
||||
has_single_letter_alias = true
|
||||
break
|
||||
}
|
||||
@@ -46,7 +46,7 @@ func (self *Completions) add_options_group(options []*Option, word string) {
|
||||
if !has_single_letter_alias {
|
||||
for _, q := range opt.Aliases {
|
||||
if !q.IsShort {
|
||||
group.add_match(q.String(), opt.Help)
|
||||
group.AddMatch(q.String(), opt.Help)
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ func (self *Completions) add_options_group(options []*Option, word string) {
|
||||
for _, opt := range options {
|
||||
for _, q := range opt.Aliases {
|
||||
if q.IsShort && q.NameWithoutHyphens == last_letter {
|
||||
group.add_match(word, opt.Help)
|
||||
group.AddMatch(word, opt.Help)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -69,13 +69,13 @@ func (self *Completions) add_options_group(options []*Option, word string) {
|
||||
|
||||
func (self *Command) sub_command_allowed_at(completions *Completions, arg_num int) bool {
|
||||
if self.SubCommandMustBeFirst {
|
||||
return arg_num == 1 && completions.current_word_idx_in_parent == 1
|
||||
return arg_num == 1 && completions.CurrentWordIdxInParent == 1
|
||||
}
|
||||
return arg_num == 1
|
||||
}
|
||||
|
||||
func complete_word(word string, completions *Completions, only_args_allowed bool, expecting_arg_for *Option, arg_num int) {
|
||||
cmd := completions.current_cmd
|
||||
cmd := completions.CurrentCmd
|
||||
if expecting_arg_for != nil {
|
||||
if expecting_arg_for.Completer != nil {
|
||||
expecting_arg_for.Completer(completions, word, arg_num)
|
||||
@@ -89,7 +89,7 @@ func complete_word(word string, completions *Completions, only_args_allowed bool
|
||||
if option != nil {
|
||||
if option.Completer != nil {
|
||||
option.Completer(completions, word[idx+1:], arg_num)
|
||||
completions.add_prefix_to_all_matches(word[:idx+1])
|
||||
completions.AddPrefixToAllMatches(word[:idx+1])
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -99,13 +99,17 @@ func complete_word(word string, completions *Completions, only_args_allowed bool
|
||||
}
|
||||
if cmd.HasVisibleSubCommands() && cmd.sub_command_allowed_at(completions, arg_num) {
|
||||
for _, cg := range cmd.SubCommandGroups {
|
||||
group := completions.add_match_group(cg.Title)
|
||||
group := completions.AddMatchGroup(cg.Title)
|
||||
if group.Title == "" {
|
||||
group.Title = "Sub-commands"
|
||||
}
|
||||
for _, sc := range cg.SubCommands {
|
||||
if strings.HasPrefix(sc.Name, word) {
|
||||
group.add_match(sc.Name, sc.HelpText)
|
||||
t := sc.ShortDescription
|
||||
if t == "" {
|
||||
t = sc.HelpText
|
||||
}
|
||||
group.AddMatch(sc.Name, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -122,21 +126,21 @@ func complete_word(word string, completions *Completions, only_args_allowed bool
|
||||
}
|
||||
|
||||
func completion_parse_args(cmd *Command, words []string, completions *Completions) {
|
||||
completions.current_cmd = cmd
|
||||
completions.CurrentCmd = cmd
|
||||
if len(words) == 0 {
|
||||
complete_word("", completions, false, nil, 0)
|
||||
return
|
||||
}
|
||||
completions.all_words = words
|
||||
completions.AllWords = words
|
||||
|
||||
var expecting_arg_for *Option
|
||||
only_args_allowed := false
|
||||
arg_num := 0
|
||||
|
||||
for i, word := range words {
|
||||
cmd = completions.current_cmd
|
||||
completions.current_word_idx = i
|
||||
completions.current_word_idx_in_parent++
|
||||
cmd = completions.CurrentCmd
|
||||
completions.CurrentWordIdx = i
|
||||
completions.CurrentWordIdxInParent++
|
||||
is_last_word := i == len(words)-1
|
||||
is_option_equal := completions.split_on_equals && word == "=" && expecting_arg_for != nil
|
||||
if only_args_allowed || (expecting_arg_for == nil && !strings.HasPrefix(word, "-")) {
|
||||
@@ -144,7 +148,7 @@ func completion_parse_args(cmd *Command, words []string, completions *Completion
|
||||
arg_num++
|
||||
}
|
||||
if arg_num == 1 {
|
||||
cmd.index_of_first_arg = completions.current_word_idx
|
||||
cmd.IndexOfFirstArg = completions.CurrentWordIdx
|
||||
}
|
||||
}
|
||||
if is_last_word {
|
||||
@@ -179,10 +183,10 @@ func completion_parse_args(cmd *Command, words []string, completions *Completion
|
||||
only_args_allowed = true
|
||||
continue
|
||||
}
|
||||
completions.current_cmd = sc
|
||||
completions.CurrentCmd = sc
|
||||
cmd = sc
|
||||
arg_num = 0
|
||||
completions.current_word_idx_in_parent = 0
|
||||
completions.CurrentWordIdxInParent = 0
|
||||
only_args_allowed = false
|
||||
if cmd.ParseArgsForCompletion != nil {
|
||||
cmd.ParseArgsForCompletion(cmd, words[i+1:], completions)
|
||||
|
||||
@@ -46,13 +46,13 @@ func (self *MatchGroup) remove_common_prefix() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *MatchGroup) add_match(word string, description ...string) *Match {
|
||||
func (self *MatchGroup) AddMatch(word string, description ...string) *Match {
|
||||
ans := Match{Word: word, Description: strings.Join(description, " ")}
|
||||
self.Matches = append(self.Matches, &ans)
|
||||
return &ans
|
||||
}
|
||||
|
||||
func (self *MatchGroup) add_prefix_to_all_matches(prefix string) {
|
||||
func (self *MatchGroup) AddPrefixToAllMatches(prefix string) {
|
||||
for _, m := range self.Matches {
|
||||
m.Word = prefix + m.Word
|
||||
}
|
||||
@@ -107,21 +107,21 @@ type Completions struct {
|
||||
Groups []*MatchGroup `json:"groups,omitempty"`
|
||||
Delegate Delegate `json:"delegate,omitempty"`
|
||||
|
||||
current_cmd *Command
|
||||
all_words []string // all words passed to parse_args()
|
||||
current_word_idx int // index of current word in all_words
|
||||
current_word_idx_in_parent int // index of current word in parents command line 1 for first word after parent
|
||||
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
|
||||
|
||||
split_on_equals bool // true if the cmdline is split on = (BASH does this because readline does this)
|
||||
}
|
||||
|
||||
func (self *Completions) add_prefix_to_all_matches(prefix string) {
|
||||
func (self *Completions) AddPrefixToAllMatches(prefix string) {
|
||||
for _, mg := range self.Groups {
|
||||
mg.add_prefix_to_all_matches(prefix)
|
||||
mg.AddPrefixToAllMatches(prefix)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Completions) add_match_group(title string) *MatchGroup {
|
||||
func (self *Completions) AddMatchGroup(title string) *MatchGroup {
|
||||
for _, q := range self.Groups {
|
||||
if q.Title == title {
|
||||
return q
|
||||
@@ -136,10 +136,10 @@ type CompletionFunc func(completions *Completions, word string, arg_num int)
|
||||
|
||||
func NamesCompleter(title string, names ...string) CompletionFunc {
|
||||
return func(completions *Completions, word string, arg_num int) {
|
||||
mg := completions.add_match_group(title)
|
||||
mg := completions.AddMatchGroup(title)
|
||||
for _, q := range names {
|
||||
if strings.HasPrefix(q, word) {
|
||||
mg.add_match(q)
|
||||
mg.AddMatch(q)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -152,3 +152,10 @@ func ChainCompleters(completers ...CompletionFunc) CompletionFunc {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CompletionForWrapper(wrapped_cmd string) func(completions *Completions, word string, arg_num int) {
|
||||
return func(completions *Completions, word string, arg_num int) {
|
||||
completions.Delegate.NumToRemove = completions.CurrentWordIdx + 1
|
||||
completions.Delegate.Command = wrapped_cmd
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package completion
|
||||
|
||||
import (
|
||||
"kitty/tools/utils"
|
||||
"kitty/tools/wcswidth"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Option struct {
|
||||
Name string
|
||||
Aliases []string
|
||||
Description string
|
||||
Has_following_arg bool
|
||||
Completion_for_arg CompletionFunc
|
||||
}
|
||||
|
||||
type CommandGroup struct {
|
||||
Title string
|
||||
Commands []*Command
|
||||
}
|
||||
|
||||
type Command struct {
|
||||
Name string
|
||||
Description string
|
||||
|
||||
Options []*Option
|
||||
Groups []*CommandGroup
|
||||
|
||||
Completion_for_arg CompletionFunc
|
||||
Stop_processing_at_arg int
|
||||
First_arg_may_not_be_subcommand bool
|
||||
Subcommand_must_be_first bool
|
||||
|
||||
Parse_args func(*Command, []string, *Completions)
|
||||
|
||||
// index in Completions.all_words of the first non-option argument to this command.
|
||||
// A value of zero means no arg was found while parsing.
|
||||
index_of_first_arg int
|
||||
}
|
||||
|
||||
func (self *Command) clone_options_from(other *Command) {
|
||||
for _, opt := range other.Options {
|
||||
self.Options = append(self.Options, opt)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Command) add_group(name string) *CommandGroup {
|
||||
for _, g := range self.Groups {
|
||||
if g.Title == name {
|
||||
return g
|
||||
}
|
||||
}
|
||||
g := CommandGroup{Title: name, Commands: make([]*Command, 0, 8)}
|
||||
self.Groups = append(self.Groups, &g)
|
||||
return &g
|
||||
}
|
||||
|
||||
func (self *Command) add_command(name string, group_title string) *Command {
|
||||
ans := Command{Name: name}
|
||||
ans.Options = make([]*Option, 0, 8)
|
||||
ans.Groups = make([]*CommandGroup, 0, 2)
|
||||
g := self.add_group(group_title)
|
||||
g.Commands = append(g.Commands, &ans)
|
||||
return &ans
|
||||
}
|
||||
|
||||
func (self *Command) add_clone(name string, group_title string, clone_of *Command) *Command {
|
||||
ans := *clone_of
|
||||
ans.Name = name
|
||||
g := self.add_group(group_title)
|
||||
g.Commands = append(g.Commands, &ans)
|
||||
return &ans
|
||||
}
|
||||
|
||||
func (self *Command) find_subcommand(is_ok func(cmd *Command) bool) *Command {
|
||||
for _, g := range self.Groups {
|
||||
for _, q := range g.Commands {
|
||||
if is_ok(q) {
|
||||
return q
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *Command) find_subcommand_with_name(name string) *Command {
|
||||
return self.find_subcommand(func(cmd *Command) bool { return cmd.Name == name })
|
||||
}
|
||||
|
||||
func (self *Command) has_subcommands() bool {
|
||||
for _, g := range self.Groups {
|
||||
if len(g.Commands) > 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
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_in_parent == 1
|
||||
}
|
||||
return arg_num == 1
|
||||
}
|
||||
|
||||
func (self *Command) add_option(opt *Option) {
|
||||
self.Options = append(self.Options, opt)
|
||||
}
|
||||
|
||||
func (self *Command) GetCompletions(argv []string, init_completions func(*Completions)) *Completions {
|
||||
ans := Completions{Groups: make([]*MatchGroup, 0, 4)}
|
||||
if init_completions != nil {
|
||||
init_completions(&ans)
|
||||
}
|
||||
if len(argv) > 0 {
|
||||
exe := argv[0]
|
||||
cmd := self.find_subcommand_with_name(exe)
|
||||
if cmd != nil {
|
||||
if cmd.Parse_args != nil {
|
||||
cmd.Parse_args(cmd, argv[1:], &ans)
|
||||
} else {
|
||||
default_parse_args(cmd, argv[1:], &ans)
|
||||
}
|
||||
}
|
||||
}
|
||||
non_empty_groups := make([]*MatchGroup, 0, len(ans.Groups))
|
||||
for _, gr := range ans.Groups {
|
||||
if len(gr.Matches) > 0 {
|
||||
non_empty_groups = append(non_empty_groups, gr)
|
||||
}
|
||||
}
|
||||
ans.Groups = non_empty_groups
|
||||
return &ans
|
||||
}
|
||||
|
||||
func names_completer(title string, names ...string) CompletionFunc {
|
||||
return func(completions *Completions, word string, arg_num int) {
|
||||
mg := completions.add_match_group(title)
|
||||
for _, q := range names {
|
||||
if strings.HasPrefix(q, word) {
|
||||
mg.add_match(q)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func chain_completers(completers ...CompletionFunc) CompletionFunc {
|
||||
return func(completions *Completions, word string, arg_num int) {
|
||||
for _, f := range completers {
|
||||
f(completions, word, arg_num)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,12 +26,12 @@ func absolutize_path(path string) string {
|
||||
}
|
||||
|
||||
type FileEntry struct {
|
||||
name, completion_candidate, abspath string
|
||||
mode os.FileMode
|
||||
is_dir, is_symlink, is_empty_dir bool
|
||||
Name, CompletionCandidate, Abspath string
|
||||
Mode os.FileMode
|
||||
IsDir, IsSymlink, IsEmptyDir bool
|
||||
}
|
||||
|
||||
func complete_files(prefix string, callback func(*FileEntry), cwd string) error {
|
||||
func CompleteFiles(prefix string, callback func(*FileEntry), cwd string) error {
|
||||
if cwd == "" {
|
||||
var err error
|
||||
cwd, err = os.Getwd()
|
||||
@@ -90,24 +90,24 @@ func complete_files(prefix string, callback func(*FileEntry), cwd string) error
|
||||
abspath := filepath.Join(base_dir, entry.Name())
|
||||
dir_to_check := ""
|
||||
data := FileEntry{
|
||||
name: entry.Name(), abspath: abspath, mode: entry.Type(), is_dir: entry.IsDir(),
|
||||
is_symlink: entry.Type()&os.ModeSymlink == os.ModeSymlink, completion_candidate: q}
|
||||
if data.is_symlink {
|
||||
Name: entry.Name(), Abspath: abspath, Mode: entry.Type(), IsDir: entry.IsDir(),
|
||||
IsSymlink: entry.Type()&os.ModeSymlink == os.ModeSymlink, CompletionCandidate: q}
|
||||
if data.IsSymlink {
|
||||
target, err := filepath.EvalSymlinks(abspath)
|
||||
if err == nil && target != base_dir {
|
||||
td, err := os.Stat(target)
|
||||
if err == nil && td.IsDir() {
|
||||
dir_to_check = target
|
||||
data.is_dir = true
|
||||
data.IsDir = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if dir_to_check != "" {
|
||||
subentries, err := os.ReadDir(dir_to_check)
|
||||
data.is_empty_dir = err != nil || len(subentries) == 0
|
||||
data.IsEmptyDir = err != nil || len(subentries) == 0
|
||||
}
|
||||
if data.is_dir {
|
||||
data.completion_candidate += utils.Sep
|
||||
if data.IsDir {
|
||||
data.CompletionCandidate += utils.Sep
|
||||
}
|
||||
callback(&data)
|
||||
}
|
||||
@@ -150,22 +150,22 @@ func is_dir_or_symlink_to_dir(entry os.DirEntry, path string) bool {
|
||||
|
||||
func fname_based_completer(prefix, cwd string, is_match func(string) bool) []string {
|
||||
ans := make([]string, 0, 1024)
|
||||
complete_files(prefix, func(entry *FileEntry) {
|
||||
if entry.is_dir && !entry.is_empty_dir {
|
||||
entries, err := os.ReadDir(entry.abspath)
|
||||
CompleteFiles(prefix, func(entry *FileEntry) {
|
||||
if entry.IsDir && !entry.IsEmptyDir {
|
||||
entries, err := os.ReadDir(entry.Abspath)
|
||||
if err == nil {
|
||||
for _, e := range entries {
|
||||
if is_match(e.Name()) || is_dir_or_symlink_to_dir(e, filepath.Join(entry.abspath, e.Name())) {
|
||||
ans = append(ans, entry.completion_candidate)
|
||||
if is_match(e.Name()) || is_dir_or_symlink_to_dir(e, filepath.Join(entry.Abspath, e.Name())) {
|
||||
ans = append(ans, entry.CompletionCandidate)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
q := strings.ToLower(entry.name)
|
||||
q := strings.ToLower(entry.Name)
|
||||
if is_match(q) {
|
||||
ans = append(ans, entry.completion_candidate)
|
||||
ans = append(ans, entry.CompletionCandidate)
|
||||
}
|
||||
}, cwd)
|
||||
return ans
|
||||
@@ -244,15 +244,52 @@ func make_completer(title string, relative_to relative_to, patterns []string, f
|
||||
return func(completions *Completions, word string, arg_num int) {
|
||||
q := f(word, cwd, lpats)
|
||||
if len(q) > 0 {
|
||||
mg := completions.add_match_group(title)
|
||||
mg := completions.AddMatchGroup(title)
|
||||
mg.IsFiles = true
|
||||
for _, c := range q {
|
||||
mg.add_match(c)
|
||||
mg.AddMatch(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CompleteExecutableFirstArg(completions *Completions, word string, arg_num int) {
|
||||
if arg_num > 1 {
|
||||
completions.Delegate.NumToRemove = completions.CurrentCmd.IndexOfFirstArg + 1 // +1 because the first word is not present in all_words
|
||||
completions.Delegate.Command = completions.AllWords[completions.CurrentCmd.IndexOfFirstArg]
|
||||
return
|
||||
}
|
||||
exes := CompleteExecutablesInPath(word)
|
||||
if len(exes) > 0 {
|
||||
mg := completions.AddMatchGroup("Executables in PATH")
|
||||
for _, exe := range exes {
|
||||
mg.AddMatch(exe)
|
||||
}
|
||||
}
|
||||
|
||||
if len(word) > 0 {
|
||||
mg := completions.AddMatchGroup("Executables")
|
||||
mg.IsFiles = true
|
||||
|
||||
CompleteFiles(word, func(entry *FileEntry) {
|
||||
if entry.IsDir && !entry.IsEmptyDir {
|
||||
// only allow directories that have sub-dirs or executable files in them
|
||||
entries, err := os.ReadDir(entry.Abspath)
|
||||
if err == nil {
|
||||
for _, x := range entries {
|
||||
if x.IsDir() || unix.Access(filepath.Join(entry.Abspath, x.Name()), unix.X_OK) == nil {
|
||||
mg.AddMatch(entry.CompletionCandidate)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if unix.Access(entry.Abspath, unix.X_OK) == nil {
|
||||
mg.AddMatch(entry.CompletionCandidate)
|
||||
}
|
||||
}, "")
|
||||
}
|
||||
}
|
||||
|
||||
func FnmatchCompleter(title string, relative_to relative_to, patterns ...string) CompletionFunc {
|
||||
return make_completer(title, relative_to, patterns, complete_by_fnmatch)
|
||||
}
|
||||
@@ -268,12 +305,12 @@ func DirectoryCompleter(title string, relative_to relative_to) CompletionFunc {
|
||||
cwd := get_cwd_for_completion(relative_to)
|
||||
|
||||
return func(completions *Completions, word string, arg_num int) {
|
||||
mg := completions.add_match_group(title)
|
||||
mg := completions.AddMatchGroup(title)
|
||||
mg.NoTrailingSpace = true
|
||||
mg.IsFiles = true
|
||||
complete_files(word, func(entry *FileEntry) {
|
||||
if entry.mode.IsDir() {
|
||||
mg.add_match(entry.completion_candidate)
|
||||
CompleteFiles(word, func(entry *FileEntry) {
|
||||
if entry.Mode.IsDir() {
|
||||
mg.AddMatch(entry.CompletionCandidate)
|
||||
}
|
||||
}, cwd)
|
||||
}
|
||||
|
||||
@@ -39,10 +39,10 @@ func TestCompleteFiles(t *testing.T) {
|
||||
}
|
||||
sort.Strings(expected)
|
||||
actual := make([]string, 0, len(expected))
|
||||
complete_files(prefix, func(entry *FileEntry) {
|
||||
actual = append(actual, entry.completion_candidate)
|
||||
if _, err := os.Stat(entry.abspath); err != nil {
|
||||
t.Fatalf("Abspath does not exist: %#v", entry.abspath)
|
||||
CompleteFiles(prefix, func(entry *FileEntry) {
|
||||
actual = append(actual, entry.CompletionCandidate)
|
||||
if _, err := os.Stat(entry.Abspath); err != nil {
|
||||
t.Fatalf("Abspath does not exist: %#v", entry.Abspath)
|
||||
}
|
||||
}, "")
|
||||
sort.Strings(actual)
|
||||
|
||||
@@ -18,9 +18,9 @@ func fish_output_serializer(completions []*Completions, shell_state map[string]s
|
||||
n := completions[0].Delegate.NumToRemove
|
||||
fm := markup.New(false) // fish freaks out if there are escape codes in the description strings
|
||||
if n > 0 {
|
||||
words := make([]string, len(completions[0].all_words)-n+1)
|
||||
words := make([]string, len(completions[0].AllWords)-n+1)
|
||||
words[0] = completions[0].Delegate.Command
|
||||
copy(words[1:], completions[0].all_words[n:])
|
||||
copy(words[1:], completions[0].AllWords[n:])
|
||||
for i, w := range words {
|
||||
words[i] = fmt.Sprintf("(string escape -- %s)", utils.QuoteStringForFish(w))
|
||||
}
|
||||
|
||||
96
tools/cmd/completion/kitty.go
Normal file
96
tools/cmd/completion/kitty.go
Normal file
@@ -0,0 +1,96 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package completion
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"kitty/tools/cli"
|
||||
"kitty/tools/utils"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func complete_kitty_override(completions *cli.Completions, word string, arg_num int) {
|
||||
mg := completions.AddMatchGroup("Config directives")
|
||||
mg.NoTrailingSpace = true
|
||||
for _, q := range kitty_option_names_for_completion {
|
||||
if strings.HasPrefix(q, word) {
|
||||
mg.AddMatch(q + "=")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func complete_kitty_listen_on(completions *cli.Completions, word string, arg_num int) {
|
||||
if !strings.Contains(word, ":") {
|
||||
mg := completions.AddMatchGroup("Address family")
|
||||
mg.NoTrailingSpace = true
|
||||
for _, q := range []string{"unix:", "tcp:"} {
|
||||
if strings.HasPrefix(q, word) {
|
||||
mg.AddMatch(q)
|
||||
}
|
||||
}
|
||||
} else if strings.HasPrefix(word, "unix:") && !strings.HasPrefix(word, "unix:@") {
|
||||
cli.FnmatchCompleter("UNIX sockets", cli.CWD, "*")(completions, word[len("unix:"):], arg_num)
|
||||
completions.AddPrefixToAllMatches("unix:")
|
||||
}
|
||||
}
|
||||
|
||||
func complete_plus_launch(completions *cli.Completions, word string, arg_num int) {
|
||||
if arg_num == 1 {
|
||||
cli.FnmatchCompleter("Python scripts", cli.CWD, "*.py")(completions, word, arg_num)
|
||||
if strings.HasPrefix(word, ":") {
|
||||
exes := cli.CompleteExecutablesInPath(word[1:])
|
||||
mg := completions.AddMatchGroup("Python scripts in PATH")
|
||||
for _, exe := range exes {
|
||||
mg.AddMatch(":" + exe)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cli.FnmatchCompleter("Files", cli.CWD, "*")(completions, word, arg_num)
|
||||
}
|
||||
}
|
||||
|
||||
func complete_plus_runpy(completions *cli.Completions, word string, arg_num int) {
|
||||
if arg_num > 1 {
|
||||
cli.FnmatchCompleter("Files", cli.CWD, "*")(completions, word, arg_num)
|
||||
}
|
||||
}
|
||||
|
||||
func complete_plus_open(completions *cli.Completions, word string, arg_num int) {
|
||||
cli.FnmatchCompleter("Files", cli.CWD, "*")(completions, word, arg_num)
|
||||
}
|
||||
|
||||
func complete_themes(completions *cli.Completions, word string, arg_num int) {
|
||||
kitty, err := utils.KittyExe()
|
||||
if err == nil {
|
||||
out, err := exec.Command(kitty, "+runpy", "from kittens.themes.collection import *; print_theme_names()").Output()
|
||||
if err == nil {
|
||||
mg := completions.AddMatchGroup("Themes")
|
||||
scanner := bufio.NewScanner(bytes.NewReader(out))
|
||||
for scanner.Scan() {
|
||||
theme_name := strings.TrimSpace(scanner.Text())
|
||||
if theme_name != "" && strings.HasPrefix(theme_name, word) {
|
||||
mg.AddMatch(theme_name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func EntryPoint(tool_root *cli.Command) {
|
||||
tool_root.AddSubCommand(&cli.Command{
|
||||
Name: "__complete__", Hidden: true,
|
||||
Usage: "output_type [shell state...]",
|
||||
ShortDescription: "Generate completions for kitty commands",
|
||||
HelpText: "Generate completion candidates for kitty commands. The command line is read from STDIN. output_type can be one of the supported shells or 'json' for JSON output.",
|
||||
Run: func(cmd *cli.Command, args []string) (ret int, err error) {
|
||||
return ret, cli.GenerateCompletions(args)
|
||||
},
|
||||
})
|
||||
|
||||
}
|
||||
@@ -4,28 +4,19 @@ package main
|
||||
|
||||
import (
|
||||
"kitty/tools/cli"
|
||||
"kitty/tools/cli/completion"
|
||||
"kitty/tools/cmd/at"
|
||||
"kitty/tools/cmd/completion"
|
||||
)
|
||||
|
||||
func completion_entry_point(tool_root *cli.Command) {
|
||||
tool_root.AddSubCommand(&cli.Command{
|
||||
Name: "__complete__", Hidden: true,
|
||||
Usage: "output_type [shell state...]",
|
||||
ShortDescription: "Generate completions for kitty commands",
|
||||
HelpText: "Generate completion candidates for kitty commands. The command line is read from STDIN. output_type can be one of the supported shells or 'json' for JSON output.",
|
||||
Run: func(cmd *cli.Command, args []string) (ret int, err error) {
|
||||
return ret, completion.Main(args)
|
||||
},
|
||||
})
|
||||
}
|
||||
func main() {
|
||||
root := cli.NewRootCommand()
|
||||
root.ShortDescription = "Fast, statically compiled implementations for various kitty command-line tools"
|
||||
root.Usage = "command [command options] [command args]"
|
||||
|
||||
// @
|
||||
at.EntryPoint(root)
|
||||
completion_entry_point(root)
|
||||
// __complete__
|
||||
completion.EntryPoint(root)
|
||||
|
||||
root.Exec()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user