]> git.lizzy.rs Git - micro.git/commitdiff
Improve tab completion in command mode
authorZachary Yedidia <zyedidia@gmail.com>
Sun, 28 Aug 2016 17:05:07 +0000 (13:05 -0400)
committerZachary Yedidia <zyedidia@gmail.com>
Sun, 28 Aug 2016 17:05:07 +0000 (13:05 -0400)
Pressing tab when all suggestions start with the same substring
will insert that substring (this is how bash autocompletion works).

cmd/micro/autocomplete.go
cmd/micro/messenger.go
cmd/micro/util.go
cmd/micro/util_test.go

index 52889d0f736a68ac7bd21da670ff8337a0fc76e1..d052c0c512e14ef5d13fee51555a8d14fdc26c00 100644 (file)
@@ -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
index 39e226335b49f9c0a2155ffcb53d65337457c4f7..06774e3d856fe38c68838cea6e82aa7d938d1545 100644 (file)
@@ -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
index 3a1cb4bb71924fc779647704ec7a97e500d1c317..4dc2fddeef1ea59fce6dec715a68b9dcd18b8fba 100644 (file)
@@ -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 {
index 29fc009f067761034dc97d0f04c1e17972879c46..02146212fc47afbab97baaa65946a3f3652664d2 100644 (file)
@@ -1,6 +1,9 @@
 package main
 
-import "testing"
+import (
+       "fmt"
+       "testing"
+)
 
 func TestNumOccurences(t *testing.T) {
        var tests = []struct {