Remove common prefix for bash as well

This commit is contained in:
Kovid Goyal
2022-09-19 17:09:42 +05:30
parent a7c997c6ef
commit b04b483b3f
3 changed files with 28 additions and 18 deletions

View File

@@ -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)+")")
}

View File

@@ -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)

View File

@@ -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=(")