refactor: refer to completion output as longest (#2358)

This commit is contained in:
Joe Lim authored and GitHub committed 2026-01-23 17:57:46 +11:00
1 parent 5bfc8de207
commit 8a01b4a15e
3 files changed
+89 -87

No files matched your search

+5 -5
View File
@@ -668,18 +668,18 @@ func (app *app) runShell(s string, args []string, prefix string) {
} }
func (app *app) doComplete() (matches []compMatch) { func (app *app) doComplete() (matches []compMatch) {
var result string var longest string
switch app.ui.cmdPrefix { switch app.ui.cmdPrefix {
case ":": case ":":
matches, result = completeCmd(app.ui.cmdAccLeft) matches, longest = completeCmd(app.ui.cmdAccLeft)
case "$", "%", "!", "&": case "$", "%", "!", "&":
matches, result = completeShell(app.ui.cmdAccLeft) matches, longest = completeShell(app.ui.cmdAccLeft)
case "/", "?": case "/", "?":
matches, result = completeSearch(app.ui.cmdAccLeft) matches, longest = completeSearch(app.ui.cmdAccLeft)
} }
app.ui.cmdAccLeft = []rune(result) app.ui.cmdAccLeft = []rune(longest)
app.ui.menu, app.ui.menuSelect = listMatches(app.ui.screen, matches, -1) app.ui.menu, app.ui.menuSelect = listMatches(app.ui.screen, matches, -1)
return return
} }
+72 -72
View File
@@ -161,7 +161,7 @@ func getLocalOptWords(localOpts any) (localOptWords []string) {
return return
} }
func commonPrefix(s1, s2 string) string { func getLongest(s1, s2 string) string {
r1 := []rune(s1) r1 := []rune(s1)
r2 := []rune(s2) r2 := []rune(s2)
@@ -180,7 +180,7 @@ type compMatch struct {
result string // result when cycling through completion menu result string // result when cycling through completion menu
} }
func matchWord(s string, words []string) (matches []compMatch, result string) { func matchWord(s string, words []string) (matches []compMatch, longest string) {
for _, w := range words { for _, w := range words {
if !strings.HasPrefix(w, s) { if !strings.HasPrefix(w, s) {
continue continue
@@ -188,22 +188,22 @@ func matchWord(s string, words []string) (matches []compMatch, result string) {
matches = append(matches, compMatch{w, w}) matches = append(matches, compMatch{w, w})
if len(matches) == 1 { if len(matches) == 1 {
result = w longest = w
} else { } else {
result = commonPrefix(result, w) longest = getLongest(longest, w)
} }
} }
switch len(matches) { switch len(matches) {
case 0: case 0:
result = s longest = s
case 1: case 1:
result += " " longest += " "
} }
return return
} }
func matchList(s string, words []string) (matches []compMatch, result string) { func matchList(s string, words []string) (matches []compMatch, longest string) {
toks := strings.Split(s, ":") toks := strings.Split(s, ":")
for _, w := range words { for _, w := range words {
@@ -215,31 +215,31 @@ func matchList(s string, words []string) (matches []compMatch, result string) {
matches = append(matches, compMatch{w, matchResult}) matches = append(matches, compMatch{w, matchResult})
if len(matches) == 1 { if len(matches) == 1 {
result = matchResult longest = matchResult
} else { } else {
result = commonPrefix(result, matchResult) longest = getLongest(longest, matchResult)
} }
} }
switch len(matches) { switch len(matches) {
case 0: case 0:
result = s longest = s
case 1: case 1:
if result == s { if longest == s {
result += " " longest += " "
} }
} }
return return
} }
func matchCmd(s string) (matches []compMatch, result string) { func matchCmd(s string) (matches []compMatch, longest string) {
words := slices.Concat(gCmdWords, slices.Collect(maps.Keys(gOpts.cmds))) words := slices.Concat(gCmdWords, slices.Collect(maps.Keys(gOpts.cmds)))
slices.Sort(words) slices.Sort(words)
matches, result = matchWord(s, slices.Compact(words)) matches, longest = matchWord(s, slices.Compact(words))
return return
} }
func matchFile(s string, dirOnly bool, escape, unescape func(string) string) (matches []compMatch, result string) { func matchFile(s string, dirOnly bool, escape, unescape func(string) string) (matches []compMatch, longest string) {
dir, file := filepath.Split(unescape(replaceTilde(s))) dir, file := filepath.Split(unescape(replaceTilde(s)))
d := dir d := dir
@@ -249,11 +249,11 @@ func matchFile(s string, dirOnly bool, escape, unescape func(string) string) (ma
files, err := os.ReadDir(d) files, err := os.ReadDir(d)
if err != nil { if err != nil {
log.Printf("reading directory: %s", err) log.Printf("reading directory: %s", err)
result = s longest = s
return return
} }
var commonName string var longestName string
for _, f := range files { for _, f := range files {
isDir := false isDir := false
@@ -280,40 +280,40 @@ func matchFile(s string, dirOnly bool, escape, unescape func(string) string) (ma
matches = append(matches, compMatch{name, escape(dir + name)}) matches = append(matches, compMatch{name, escape(dir + name)})
if len(matches) == 1 { if len(matches) == 1 {
commonName = name longestName = name
} else { } else {
// Match case-insensitively without changing the prefix's case. // Match case-insensitively without changing the prefix's case.
p := commonPrefix(strings.ToLower(commonName), strings.ToLower(name)) p := getLongest(strings.ToLower(longestName), strings.ToLower(name))
commonName = string([]rune(commonName)[:len([]rune(p))]) longestName = string([]rune(longestName)[:len([]rune(p))])
} }
} }
switch len(matches) { switch len(matches) {
case 0: case 0:
result = s longest = s
case 1: case 1:
result = escape(dir + commonName) longest = escape(dir + longestName)
if !strings.HasSuffix(commonName, string(filepath.Separator)) { if !strings.HasSuffix(longestName, string(filepath.Separator)) {
result += " " longest += " "
} }
default: default:
result = escape(dir + commonName) longest = escape(dir + longestName)
} }
return return
} }
func matchCmdFile(s string, dirOnly bool) (matches []compMatch, result string) { func matchCmdFile(s string, dirOnly bool) (matches []compMatch, longest string) {
matches, result = matchFile(s, dirOnly, cmdEscape, cmdUnescape) matches, longest = matchFile(s, dirOnly, cmdEscape, cmdUnescape)
return return
} }
func matchShellFile(s string) (matches []compMatch, result string) { func matchShellFile(s string) (matches []compMatch, longest string) {
matches, result = matchFile(s, false, shellEscape, shellUnescape) matches, longest = matchFile(s, false, shellEscape, shellUnescape)
return return
} }
func matchSetlocalDir(s string) (matches []compMatch, result string) { func matchSetlocalDir(s string) (matches []compMatch, longest string) {
matches, result = matchFile(s, true, cmdEscape, cmdUnescape) matches, longest = matchFile(s, true, cmdEscape, cmdUnescape)
if len(matches) == 0 { if len(matches) == 0 {
return return
} }
@@ -334,13 +334,13 @@ func matchSetlocalDir(s string) (matches []compMatch, result string) {
} }
matches = tmp matches = tmp
if result != s { if longest != s {
result = trimSepEsc(result) longest = trimSepEsc(longest)
} }
return return
} }
func matchExec(s string) (matches []compMatch, result string) { func matchExec(s string) (matches []compMatch, longest string) {
var words []string var words []string
for p := range strings.SplitSeq(envPath, string(filepath.ListSeparator)) { for p := range strings.SplitSeq(envPath, string(filepath.ListSeparator)) {
files, err := os.ReadDir(p) files, err := os.ReadDir(p)
@@ -369,15 +369,15 @@ func matchExec(s string) (matches []compMatch, result string) {
} }
slices.Sort(words) slices.Sort(words)
matches, result = matchWord(s, slices.Compact(words)) matches, longest = matchWord(s, slices.Compact(words))
return return
} }
func matchSearch(s string) (matches []compMatch, result string) { func matchSearch(s string) (matches []compMatch, longest string) {
files, err := os.ReadDir(".") files, err := os.ReadDir(".")
if err != nil { if err != nil {
log.Printf("reading directory: %s", err) log.Printf("reading directory: %s", err)
result = s longest = s
return return
} }
@@ -388,34 +388,34 @@ func matchSearch(s string) (matches []compMatch, result string) {
matches = append(matches, compMatch{f.Name(), f.Name()}) matches = append(matches, compMatch{f.Name(), f.Name()})
if len(matches) == 1 { if len(matches) == 1 {
result = f.Name() longest = f.Name()
} else { } else {
p := commonPrefix(strings.ToLower(result), strings.ToLower(f.Name())) p := getLongest(strings.ToLower(longest), strings.ToLower(f.Name()))
result = string([]rune(result)[:len([]rune(p))]) longest = string([]rune(longest)[:len([]rune(p))])
} }
} }
if len(matches) == 0 { if len(matches) == 0 {
result = s longest = s
} }
return return
} }
func completeCmd(acc []rune) (matches []compMatch, result string) { func completeCmd(acc []rune) (matches []compMatch, longest string) {
s := string(acc) s := string(acc)
f := tokenize(s) f := tokenize(s)
if len(f) == 1 { if len(f) == 1 {
matches, result = matchCmd(s) matches, longest = matchCmd(s)
return return
} }
result = f[len(f)-1] longest = f[len(f)-1]
switch f[0] { switch f[0] {
case "set": case "set":
if len(f) == 2 { if len(f) == 2 {
matches, result = matchWord(f[1], gOptWords) matches, longest = matchWord(f[1], gOptWords)
break break
} }
if len(f) != 3 { if len(f) != 3 {
@@ -423,31 +423,31 @@ func completeCmd(acc []rune) (matches []compMatch, result string) {
} }
switch f[1] { switch f[1] {
case "cleaner", "previewer", "rulerfile": case "cleaner", "previewer", "rulerfile":
matches, result = matchCmdFile(f[2], false) matches, longest = matchCmdFile(f[2], false)
case "filtermethod", "searchmethod": case "filtermethod", "searchmethod":
matches, result = matchWord(f[2], []string{"glob", "regex", "text"}) matches, longest = matchWord(f[2], []string{"glob", "regex", "text"})
case "info": case "info":
matches, result = matchList(f[2], []string{"atime", "btime", "ctime", "custom", "group", "perm", "size", "time", "user"}) matches, longest = matchList(f[2], []string{"atime", "btime", "ctime", "custom", "group", "perm", "size", "time", "user"})
case "preserve": case "preserve":
matches, result = matchList(f[2], []string{"mode", "timestamps"}) matches, longest = matchList(f[2], []string{"mode", "timestamps"})
case "selmode": case "selmode":
matches, result = matchWord(f[2], []string{"all", "dir"}) matches, longest = matchWord(f[2], []string{"all", "dir"})
case "sizeunits": case "sizeunits":
matches, result = matchWord(f[2], []string{"binary", "decimal"}) matches, longest = matchWord(f[2], []string{"binary", "decimal"})
case "sortby": case "sortby":
matches, result = matchWord(f[2], []string{"atime", "btime", "ctime", "custom", "ext", "name", "natural", "size", "time"}) matches, longest = matchWord(f[2], []string{"atime", "btime", "ctime", "custom", "ext", "name", "natural", "size", "time"})
default: default:
if slices.Contains(gOptWords, f[1]+"!") { if slices.Contains(gOptWords, f[1]+"!") {
matches, result = matchWord(f[2], []string{"false", "true"}) matches, longest = matchWord(f[2], []string{"false", "true"})
} }
} }
case "setlocal": case "setlocal":
if len(f) == 2 { if len(f) == 2 {
matches, result = matchSetlocalDir(f[1]) matches, longest = matchSetlocalDir(f[1])
break break
} }
if len(f) == 3 { if len(f) == 3 {
matches, result = matchWord(f[2], gLocalOptWords) matches, longest = matchWord(f[2], gLocalOptWords)
break break
} }
if len(f) != 4 { if len(f) != 4 {
@@ -455,56 +455,56 @@ func completeCmd(acc []rune) (matches []compMatch, result string) {
} }
switch f[2] { switch f[2] {
case "info": case "info":
matches, result = matchList(f[3], []string{"atime", "btime", "ctime", "custom", "group", "perm", "size", "time", "user"}) matches, longest = matchList(f[3], []string{"atime", "btime", "ctime", "custom", "group", "perm", "size", "time", "user"})
case "sortby": case "sortby":
matches, result = matchWord(f[3], []string{"atime", "btime", "ctime", "custom", "ext", "name", "natural", "size", "time"}) matches, longest = matchWord(f[3], []string{"atime", "btime", "ctime", "custom", "ext", "name", "natural", "size", "time"})
default: default:
if slices.Contains(gLocalOptWords, f[2]+"!") { if slices.Contains(gLocalOptWords, f[2]+"!") {
matches, result = matchWord(f[3], []string{"false", "true"}) matches, longest = matchWord(f[3], []string{"false", "true"})
} }
} }
case "map", "nmap", "vmap", "cmap": case "map", "nmap", "vmap", "cmap":
if len(f) == 3 { if len(f) == 3 {
matches, result = matchCmd(f[2]) matches, longest = matchCmd(f[2])
} }
case "cmd": case "cmd":
case "cd": case "cd":
if len(f) == 2 { if len(f) == 2 {
matches, result = matchCmdFile(f[1], true) matches, longest = matchCmdFile(f[1], true)
} }
case "addcustominfo", "select", "source": case "addcustominfo", "select", "source":
if len(f) == 2 { if len(f) == 2 {
matches, result = matchCmdFile(f[1], false) matches, longest = matchCmdFile(f[1], false)
} }
case "toggle": case "toggle":
matches, result = matchCmdFile(f[len(f)-1], false) matches, longest = matchCmdFile(f[len(f)-1], false)
default: default:
if !slices.Contains(gCmdWords, f[0]) { if !slices.Contains(gCmdWords, f[0]) {
matches, result = matchCmdFile(f[len(f)-1], false) matches, longest = matchCmdFile(f[len(f)-1], false)
} }
} }
f[len(f)-1] = result f[len(f)-1] = longest
result = strings.Join(f, " ") longest = strings.Join(f, " ")
return return
} }
func completeShell(acc []rune) (matches []compMatch, result string) { func completeShell(acc []rune) (matches []compMatch, longest string) {
f := tokenize(string(acc)) f := tokenize(string(acc))
switch len(f) { switch len(f) {
case 1: case 1:
matches, result = matchExec(f[0]) matches, longest = matchExec(f[0])
default: default:
matches, result = matchShellFile(f[len(f)-1]) matches, longest = matchShellFile(f[len(f)-1])
} }
f[len(f)-1] = result f[len(f)-1] = longest
result = strings.Join(f, " ") longest = strings.Join(f, " ")
return return
} }
func completeSearch(acc []rune) (matches []compMatch, result string) { func completeSearch(acc []rune) (matches []compMatch, longest string) {
matches, result = matchSearch(string(acc)) matches, longest = matchSearch(string(acc))
return return
} }
+12 -10
View File
@@ -43,7 +43,7 @@ func TestGetLocalOptWords(t *testing.T) {
} }
} }
func TestCommonPrefix(t *testing.T) { func TestGetLongest(t *testing.T) {
tests := []struct { tests := []struct {
s1 string s1 string
s2 string s2 string
@@ -60,7 +60,7 @@ func TestCommonPrefix(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
if got := commonPrefix(test.s1, test.s2); got != test.exp { if got := getLongest(test.s1, test.s2); got != test.exp {
t.Errorf("at input '%s' and '%s' expected '%s' but got '%s'", test.s1, test.s2, test.exp, got) t.Errorf("at input '%s' and '%s' expected '%s' but got '%s'", test.s1, test.s2, test.exp, got)
} }
} }
@@ -71,24 +71,26 @@ func TestMatchWord(t *testing.T) {
s string s string
words []string words []string
matches []compMatch matches []compMatch
result string longest string
}{ }{
{"", nil, nil, ""}, {"", nil, nil, ""},
{"", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}, {"bar", "bar"}, {"baz", "baz"}}, ""}, {"", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}, {"bar", "bar"}, {"baz", "baz"}}, ""},
{"f", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}}, "foo "},
{"b", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "bar"}, {"baz", "baz"}}, "ba"},
{"fo", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}}, "foo "}, {"fo", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}}, "foo "},
{"ba", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "bar"}, {"baz", "baz"}}, "ba"}, {"ba", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "bar"}, {"baz", "baz"}}, "ba"},
{"fo", []string{"bar", "baz"}, nil, "fo"}, {"fo", []string{"bar", "baz"}, nil, "fo"},
} }
for _, test := range tests { for _, test := range tests {
matches, result := matchWord(test.s, test.words) matches, longest := matchWord(test.s, test.words)
if !reflect.DeepEqual(matches, test.matches) { if !reflect.DeepEqual(matches, test.matches) {
t.Errorf("at input '%s' with '%s' expected '%v' but got '%v'", test.s, test.words, test.matches, matches) t.Errorf("at input '%s' with '%s' expected '%v' but got '%v'", test.s, test.words, test.matches, matches)
} }
if result != test.result { if longest != test.longest {
t.Errorf("at input '%s' with '%s' expected '%s' but got '%s'", test.s, test.words, test.result, result) t.Errorf("at input '%s' with '%s' expected '%s' but got '%s'", test.s, test.words, test.longest, longest)
} }
} }
} }
@@ -98,7 +100,7 @@ func TestMatchList(t *testing.T) {
s string s string
words []string words []string
matches []compMatch matches []compMatch
result string longest string
}{ }{
{"", nil, nil, ""}, {"", nil, nil, ""},
{"", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}, {"bar", "bar"}, {"baz", "baz"}}, ""}, {"", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}, {"bar", "bar"}, {"baz", "baz"}}, ""},
@@ -117,14 +119,14 @@ func TestMatchList(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
matches, result := matchList(test.s, test.words) matches, longest := matchList(test.s, test.words)
if !reflect.DeepEqual(matches, test.matches) { if !reflect.DeepEqual(matches, test.matches) {
t.Errorf("at input '%s' with '%s' expected '%v' but got '%v'", test.s, test.words, test.matches, matches) t.Errorf("at input '%s' with '%s' expected '%v' but got '%v'", test.s, test.words, test.matches, matches)
} }
if result != test.result { if longest != test.longest {
t.Errorf("at input '%s' with '%s' expected '%s' but got '%s'", test.s, test.words, test.result, result) t.Errorf("at input '%s' with '%s' expected '%s' but got '%s'", test.s, test.words, test.longest, longest)
} }
} }
} }