longest match completion for multiple match

This commit is contained in:
Gokcehan 2016-08-16 23:13:57 +03:00
parent 98839a42f2
commit 4c5e5584ed

33
comp.go
View File

@ -12,8 +12,10 @@ var (
gOptWords = []string{ gOptWords = []string{
"preview", "preview",
"nopreview", "nopreview",
"preview!",
"hidden", "hidden",
"nohidden", "nohidden",
"hidden!",
"tabstop", "tabstop",
"scrolloff", "scrolloff",
"sortby", "sortby",
@ -23,20 +25,31 @@ var (
} }
) )
func matchLongest(s1, s2 string) string {
i := 0
for ; i < len(s1) && i < len(s2); i++ {
if s1[i] != s2[i] {
break
}
}
return s1[:i]
}
func matchWord(s string, words []string) string { func matchWord(s string, words []string) string {
var match string var match string
for _, w := range words { for _, w := range words {
if strings.HasPrefix(w, s) { if strings.HasPrefix(w, s) {
if match != "" { if match != "" {
return s match = matchLongest(match, w)
} else {
match = w + " "
} }
match = w
} }
} }
if match != "" { if match != "" {
return match + " " return match
} }
return s return s
@ -59,15 +72,16 @@ func matchExec(s string) string {
continue continue
} }
if match != "" { if match != "" {
return s match = matchLongest(match, f.Name())
} else {
match = f.Name() + " "
} }
match = f.Name()
} }
} }
} }
if match != "" { if match != "" {
return match + " " return match
} }
return s return s
@ -89,14 +103,15 @@ func matchFile(s string) string {
for _, f := range fi { for _, f := range fi {
if strings.HasPrefix(f.Name(), s) { if strings.HasPrefix(f.Name(), s) {
if match != "" { if match != "" {
return s match = matchLongest(match, f.Name())
} else {
match = f.Name() + " "
} }
match = f.Name()
} }
} }
if match != "" { if match != "" {
return match + " " return match
} }
return s return s