mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-30 06:37:14 +09:00
Improve tab completion in command mode
Pressing tab when all suggestions start with the same substring will insert that substring (this is how bash autocompletion works).
This commit is contained in:
@@ -49,6 +49,10 @@ func FileComplete(input string) (string, []string) {
|
|||||||
} else {
|
} else {
|
||||||
chosen = suggestions[0]
|
chosen = suggestions[0]
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if len(dirs) > 1 {
|
||||||
|
chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return chosen, suggestions
|
return chosen, suggestions
|
||||||
|
|||||||
@@ -195,6 +195,10 @@ func (m *Messenger) Prompt(prompt, historyType string, completionTypes ...Comple
|
|||||||
chosen, suggestions = OptionComplete(currentArg)
|
chosen, suggestions = OptionComplete(currentArg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(suggestions) > 1 {
|
||||||
|
chosen = chosen + CommonSubstring(suggestions...)
|
||||||
|
}
|
||||||
|
|
||||||
if chosen != "" {
|
if chosen != "" {
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
chosen = " " + chosen
|
chosen = " " + chosen
|
||||||
|
|||||||
@@ -182,6 +182,34 @@ func runePos(p int, str string) int {
|
|||||||
return utf8.RuneCountInString(str[:p])
|
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
|
// Abs is a simple absolute value function for ints
|
||||||
func Abs(n int) int {
|
func Abs(n int) int {
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestNumOccurences(t *testing.T) {
|
func TestNumOccurences(t *testing.T) {
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user