From 5c0858d6b77ad35eca4f37ff75c9fe90901fa0a1 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 18 Sep 2022 09:33:17 +0530 Subject: [PATCH] Start work on zsh completion serialization --- tools/completion/zsh.go | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tools/completion/zsh.go diff --git a/tools/completion/zsh.go b/tools/completion/zsh.go new file mode 100644 index 000000000..8537030c8 --- /dev/null +++ b/tools/completion/zsh.go @@ -0,0 +1,48 @@ +// License: GPLv3 Copyright: 2022, Kovid Goyal, + +package completion + +import ( + "bufio" + "fmt" + "strings" +) + +var _ = fmt.Print + +func zsh_input_parser(data []byte, shell_state map[string]string) ([][]string, error) { + matcher := shell_state["_matcher"] + q := strings.Split(strings.ToLower(matcher), ":")[0][:1] + if strings.Contains("lrbe", q) { + // this is zsh anchor based matching + // https://zsh.sourceforge.io/Doc/Release/Completion-Widgets.html#Completion-Matching-Control + // can be specified with matcher-list and some systems do it by default, + // for example, Debian, which adds the following to zshrc + // zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*' + // For some reason that I dont have the + // time/interest to figure out, returning completion candidates for + // these matcher types break completion, so just abort in this case. + return nil, fmt.Errorf("ZSH anchor based matching active, cannot complete") + } + raw := string(data) + new_word := strings.HasSuffix(raw, "\n\n") + raw = strings.TrimRight(raw, "\n \t") + scanner := bufio.NewScanner(strings.NewReader(raw)) + words := make([]string, 0, 32) + for scanner.Scan() { + words = append(words, scanner.Text()) + } + if new_word { + words = append(words, "") + } + return [][]string{words}, nil +} + +func zsh_output_serializer(completions []*Completions, shell_state map[string]string) ([]byte, error) { + return nil, nil +} + +func init() { + input_parsers["zsh"] = zsh_input_parser + output_serializers["zsh"] = zsh_output_serializer +}