From 1ae1b9bb981f1c630ed5e68fa3bd1f937c6397ac Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Wed, 10 Oct 2018 15:40:18 +0200 Subject: Go/repl: improve completion --- cmd/repl/main.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'cmd/repl') diff --git a/cmd/repl/main.go b/cmd/repl/main.go index eb253d0..6d1f421 100644 --- a/cmd/repl/main.go +++ b/cmd/repl/main.go @@ -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) -- cgit v1.2.3-70-g09d2