Implement completion for fish

This commit is contained in:
Kovid Goyal 2022-09-19 23:19:36 +05:30
parent ef9b765f81
commit 5666b1b0fd
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 49 additions and 3 deletions

View File

@ -1,7 +1,7 @@
function __ksi_completions
set --local ct (commandline --current-token)
set --local tokens (commandline --tokenize --cut-at-cursor --current-process)
printf "%s\n" $tokens $ct | command kitty +complete fish2
printf "%s\n" $tokens $ct | command kitty-tool __complete__ fish
end
complete -f -c clone-in-kitty -a "(__ksi_completions)"

View File

@ -1,7 +1,7 @@
function __ksi_completions
set --local ct (commandline --current-token)
set --local tokens (commandline --tokenize --cut-at-cursor --current-process)
printf "%s\n" $tokens $ct | command kitty +complete fish2
printf "%s\n" $tokens $ct | command kitty-tool __complete__ fish
end
complete -f -c edit-in-kitty -a "(__ksi_completions)"

View File

@ -1,7 +1,7 @@
function __ksi_completions
set --local ct (commandline --current-token)
set --local tokens (commandline --tokenize --cut-at-cursor --current-process)
printf "%s\n" $tokens $ct | command kitty +complete fish2
printf "%s\n" $tokens $ct | command kitty-tool __complete__ fish | source -
end
complete -f -c kitty -a "(__ksi_completions)"

45
tools/completion/fish.go Normal file
View File

@ -0,0 +1,45 @@
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
package completion
import (
"fmt"
"strings"
"kitty/tools/cli/markup"
"kitty/tools/utils"
)
var _ = fmt.Print
func fish_output_serializer(completions []*Completions, shell_state map[string]string) ([]byte, error) {
output := strings.Builder{}
f := func(format string, args ...interface{}) { fmt.Fprintf(&output, format+"\n", args...) }
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[0] = completions[0].Delegate.Command
copy(words[1:], completions[0].all_words[n:])
for i, w := range words {
words[i] = fmt.Sprintf("(string escape -- %s)", utils.QuoteStringForFish(w))
}
cmdline := strings.Join(words, " ")
f("set __ksi_cmdline " + cmdline)
f("complete -C \"$__ksi_cmdline\"")
f("set --erase __ksi_cmdline")
} else {
for _, mg := range completions[0].Groups {
for _, m := range mg.Matches {
f("echo -- %s", utils.QuoteStringForFish(m.Word+"\t"+fm.Prettify(m.Description)))
}
}
}
// debugf("%#v", output.String())
return []byte(output.String()), nil
}
func init() {
input_parsers["fish"] = shell_input_parser
output_serializers["fish"] = fish_output_serializer
}

View File

@ -74,6 +74,7 @@ func main(args []string) error {
if err != nil {
return err
}
// debugf("%#v", string(data))
all_argv, err := input_parser(data, shell_state)
if err != nil {
return err