diff --git a/cmd/micro/autocomplete.go b/cmd/micro/autocomplete.go index 52889d0f..d052c0c5 100644 --- a/cmd/micro/autocomplete.go +++ b/cmd/micro/autocomplete.go @@ -49,6 +49,10 @@ func FileComplete(input string) (string, []string) { } else { chosen = suggestions[0] } + } else { + if len(dirs) > 1 { + chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/" + } } return chosen, suggestions diff --git a/cmd/micro/messenger.go b/cmd/micro/messenger.go index 39e22633..06774e3d 100644 --- a/cmd/micro/messenger.go +++ b/cmd/micro/messenger.go @@ -195,6 +195,10 @@ func (m *Messenger) Prompt(prompt, historyType string, completionTypes ...Comple chosen, suggestions = OptionComplete(currentArg) } + if len(suggestions) > 1 { + chosen = chosen + CommonSubstring(suggestions...) + } + if chosen != "" { if len(args) > 1 { chosen = " " + chosen diff --git a/cmd/micro/util.go b/cmd/micro/util.go index 3a1cb4bb..4dc2fdde 100644 --- a/cmd/micro/util.go +++ b/cmd/micro/util.go @@ -182,6 +182,34 @@ func runePos(p int, str string) int { return utf8.RuneCountInString(str[:p]) } +func lcs(a, b string) string { + arunes := []rune(a) + brunes := []rune(b) + + lcs := "" + for i, r := range arunes { + if i >= len(brunes) { + break + } + if r == brunes[i] { + lcs += string(r) + } else { + break + } + } + return lcs +} + +func CommonSubstring(arr ...string) string { + commonStr := arr[0] + + for _, str := range arr[1:] { + commonStr = lcs(commonStr, str) + } + + return commonStr +} + // Abs is a simple absolute value function for ints func Abs(n int) int { if n < 0 { diff --git a/cmd/micro/util_test.go b/cmd/micro/util_test.go index 29fc009f..02146212 100644 --- a/cmd/micro/util_test.go +++ b/cmd/micro/util_test.go @@ -1,6 +1,9 @@ package main -import "testing" +import ( + "fmt" + "testing" +) func TestNumOccurences(t *testing.T) { var tests = []struct {