From 4c5e5584ed67582b1f664b10e8e458dab8220ff0 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Tue, 16 Aug 2016 23:13:57 +0300 Subject: [PATCH] longest match completion for multiple match --- comp.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/comp.go b/comp.go index 6d18812..b908c73 100644 --- a/comp.go +++ b/comp.go @@ -12,8 +12,10 @@ var ( gOptWords = []string{ "preview", "nopreview", + "preview!", "hidden", "nohidden", + "hidden!", "tabstop", "scrolloff", "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 { var match string for _, w := range words { if strings.HasPrefix(w, s) { if match != "" { - return s + match = matchLongest(match, w) + } else { + match = w + " " } - match = w } } if match != "" { - return match + " " + return match } return s @@ -59,15 +72,16 @@ func matchExec(s string) string { continue } if match != "" { - return s + match = matchLongest(match, f.Name()) + } else { + match = f.Name() + " " } - match = f.Name() } } } if match != "" { - return match + " " + return match } return s @@ -89,14 +103,15 @@ func matchFile(s string) string { for _, f := range fi { if strings.HasPrefix(f.Name(), s) { if match != "" { - return s + match = matchLongest(match, f.Name()) + } else { + match = f.Name() + " " } - match = f.Name() } } if match != "" { - return match + " " + return match } return s