diff options
author | Péter Szilágyi <peterke@gmail.com> | 2016-05-06 17:40:23 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2016-05-30 22:25:23 +0800 |
commit | ffaf58f0a98bd987bbe76e8669bb22c405dcd62a (patch) | |
tree | 4cd484504d4a7089cfd85d22a2a9fa62c79abb7d /internal/jsre/completion.go | |
parent | ab664c7e17009729d1083d8d4f7c37eb387012d3 (diff) | |
download | go-tangerine-ffaf58f0a98bd987bbe76e8669bb22c405dcd62a.tar.gz go-tangerine-ffaf58f0a98bd987bbe76e8669bb22c405dcd62a.tar.zst go-tangerine-ffaf58f0a98bd987bbe76e8669bb22c405dcd62a.zip |
cmd, console: split off the console into a reusable package
Diffstat (limited to 'internal/jsre/completion.go')
-rw-r--r-- | internal/jsre/completion.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/internal/jsre/completion.go b/internal/jsre/completion.go new file mode 100644 index 000000000..7f484bbbb --- /dev/null +++ b/internal/jsre/completion.go @@ -0,0 +1,74 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. + +package jsre + +import ( + "sort" + "strings" + + "github.com/robertkrimen/otto" +) + +// CompleteKeywords returns potential continuations for the given line. Since line is +// evaluated, callers need to make sure that evaluating line does not have side effects. +func (jsre *JSRE) CompleteKeywords(line string) []string { + var results []string + jsre.Do(func(vm *otto.Otto) { + results = getCompletions(vm, line) + }) + return results +} + +func getCompletions(vm *otto.Otto, line string) (results []string) { + parts := strings.Split(line, ".") + objRef := "this" + prefix := line + if len(parts) > 1 { + objRef = strings.Join(parts[0:len(parts)-1], ".") + prefix = parts[len(parts)-1] + } + + obj, _ := vm.Object(objRef) + if obj == nil { + return nil + } + iterOwnAndConstructorKeys(vm, obj, func(k string) { + if strings.HasPrefix(k, prefix) { + if objRef == "this" { + results = append(results, k) + } else { + results = append(results, strings.Join(parts[:len(parts)-1], ".")+"."+k) + } + } + }) + + // Append opening parenthesis (for functions) or dot (for objects) + // if the line itself is the only completion. + if len(results) == 1 && results[0] == line { + obj, _ := vm.Object(line) + if obj != nil { + if obj.Class() == "Function" { + results[0] += "(" + } else { + results[0] += "." + } + } + } + + sort.Strings(results) + return results +} |