diff --git a/tools/completion/bash.go b/tools/completion/bash.go index d80976202..f85ef1bb3 100644 --- a/tools/completion/bash.go +++ b/tools/completion/bash.go @@ -14,6 +14,7 @@ var _ = fmt.Print func bash_output_serializer(completions []*Completions, shell_state map[string]string) ([]byte, error) { output := strings.Builder{} for _, mg := range completions[0].Groups { + mg.remove_common_prefix() for _, m := range mg.Matches { fmt.Fprintln(&output, "COMPREPLY+=("+utils.QuoteStringForSH(m.Word)+")") } diff --git a/tools/completion/types.go b/tools/completion/types.go index 16d9d0afe..86d95cdb5 100644 --- a/tools/completion/types.go +++ b/tools/completion/types.go @@ -5,6 +5,7 @@ package completion import ( "kitty/tools/utils" "kitty/tools/wcswidth" + "path/filepath" "strings" ) @@ -20,6 +21,27 @@ type MatchGroup struct { Matches []*Match `json:"matches,omitempty"` } +func (self *MatchGroup) remove_common_prefix() string { + if self.IsFiles { + if len(self.Matches) > 1 { + lcp := self.longest_common_prefix() + if strings.Contains(lcp, utils.Sep) { + lcp = strings.TrimRight(filepath.Dir(lcp), utils.Sep) + utils.Sep + self.remove_prefix_from_all_matches(lcp) + return lcp + } + } + } else if len(self.Matches) > 1 && strings.HasPrefix(self.Matches[0].Word, "--") && strings.Contains(self.Matches[0].Word, "=") { + lcp, _, _ := utils.Cut(self.longest_common_prefix(), "=") + lcp += "=" + if len(lcp) > 3 { + self.remove_prefix_from_all_matches(lcp) + return lcp + } + } + return "" +} + func (self *MatchGroup) add_match(word string, description ...string) *Match { ans := Match{Word: word, Description: strings.Join(description, " ")} self.Matches = append(self.Matches, &ans) diff --git a/tools/completion/zsh.go b/tools/completion/zsh.go index 70da1c0f4..434e708e4 100644 --- a/tools/completion/zsh.go +++ b/tools/completion/zsh.go @@ -9,7 +9,6 @@ import ( "kitty/tools/tty" "kitty/tools/utils" "kitty/tools/wcswidth" - "path/filepath" "strings" ) @@ -97,23 +96,11 @@ func serialize(completions *Completions, f *markup.Context, screen_width int) ([ } if mg.IsFiles { cmd.WriteString(" -f") - if len(mg.Matches) > 1 { - lcp := mg.longest_common_prefix() - if strings.Contains(lcp, utils.Sep) { - lcp = strings.TrimRight(filepath.Dir(lcp), utils.Sep) + utils.Sep - cmd.WriteString(" -p ") - cmd.WriteString(utils.QuoteStringForSH(lcp)) - mg.remove_prefix_from_all_matches(lcp) - } - } - } else if len(mg.Matches) > 1 && strings.HasPrefix(mg.Matches[0].Word, "--") && strings.Contains(mg.Matches[0].Word, "=") { - lcp, _, _ := utils.Cut(mg.longest_common_prefix(), "=") - lcp += "=" - if len(lcp) > 3 { - cmd.WriteString(" -p ") - cmd.WriteString(utils.QuoteStringForSH(lcp)) - mg.remove_prefix_from_all_matches(lcp) - } + } + lcp := mg.remove_common_prefix() + if lcp != "" { + cmd.WriteString(" -p ") + cmd.WriteString(utils.QuoteStringForSH(lcp)) } if mg.has_descriptions() { fmt.Fprintln(&output, "compdescriptions=(")