diff options
author | Přemysl Janouch <p@janouch.name> | 2018-10-10 15:40:18 +0200 |
---|---|---|
committer | Přemysl Janouch <p@janouch.name> | 2018-10-10 15:40:18 +0200 |
commit | 1ae1b9bb981f1c630ed5e68fa3bd1f937c6397ac (patch) | |
tree | 7cc92e9e910c9e1510cba521cda5cd92cc39f393 | |
parent | b3e27a5df38cf3856676b29d8d906c2abc2c5f67 (diff) | |
download | ell-1ae1b9bb981f1c630ed5e68fa3bd1f937c6397ac.tar.gz ell-1ae1b9bb981f1c630ed5e68fa3bd1f937c6397ac.tar.xz ell-1ae1b9bb981f1c630ed5e68fa3bd1f937c6397ac.zip |
Go/repl: improve completion
-rw-r--r-- | cmd/repl/main.go | 22 |
1 files changed, 14 insertions, 8 deletions
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) |