Go/repl: improve completion

This commit is contained in:
Přemysl Eric Janouch 2018-10-10 15:40:18 +02:00
parent b3e27a5df3
commit 1ae1b9bb98
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 14 additions and 8 deletions

View File

@ -39,21 +39,24 @@ func run(L *ell.Ell, program *ell.V) {
}
}
func complete(L *ell.Ell, line string) (res []string) {
// This never actually completes anything, just shows the options,
// we'd have to figure out the longest common prefix.
res = append(res, line)
func complete(L *ell.Ell, line string, pos int) (
head string, completions []string, tail string) {
tail = string([]rune(line)[pos:])
lastSpace := strings.LastIndexAny(string([]rune(line)[:pos]), " ()[]{};\n")
if lastSpace > -1 {
head, line = line[:lastSpace+1], line[lastSpace+1:]
}
line = strings.ToLower(line)
for v := L.Globals; v != nil; v = v.Next {
name := v.Head.String
if strings.HasPrefix(strings.ToLower(name), line) {
res = append(res, name)
completions = append(completions, name)
}
}
for name := range L.Native {
if strings.HasPrefix(strings.ToLower(name), line) {
res = append(res, name)
completions = append(completions, name)
}
}
return
@ -66,7 +69,10 @@ func main() {
}
line := liner.NewLiner()
line.SetCompleter(func(line string) []string { return complete(L, line) })
line.SetWordCompleter(func(line string, pos int) (
string, []string, string) {
return complete(L, line, pos)
})
line.SetMultiLineMode(true)
line.SetTabCompletionStyle(liner.TabPrints)