mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.1 KiB
75 lines
2.1 KiB
9 years ago
|
// Copyright 2016 The go-ethereum Authors
|
||
9 years ago
|
// 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
|
||
9 years ago
|
jsre.Do(func(vm *otto.Otto) {
|
||
|
results = getCompletions(vm, line)
|
||
|
})
|
||
9 years ago
|
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]
|
||
|
}
|
||
|
|
||
9 years ago
|
obj, _ := vm.Object(objRef)
|
||
|
if obj == nil {
|
||
9 years ago
|
return nil
|
||
|
}
|
||
9 years ago
|
iterOwnAndConstructorKeys(vm, obj, func(k string) {
|
||
9 years ago
|
if strings.HasPrefix(k, prefix) {
|
||
|
if objRef == "this" {
|
||
|
results = append(results, k)
|
||
|
} else {
|
||
|
results = append(results, strings.Join(parts[:len(parts)-1], ".")+"."+k)
|
||
|
}
|
||
|
}
|
||
9 years ago
|
})
|
||
9 years ago
|
|
||
|
// 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] += "."
|
||
|
}
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
sort.Strings(results)
|
||
|
return results
|
||
|
}
|