aboutsummaryrefslogtreecommitdiffstats
path: root/jsre
diff options
context:
space:
mode:
Diffstat (limited to 'jsre')
-rw-r--r--jsre/completion.go19
-rw-r--r--jsre/completion_test.go6
-rw-r--r--jsre/jsre.go16
-rw-r--r--jsre/pretty.go2
4 files changed, 29 insertions, 14 deletions
diff --git a/jsre/completion.go b/jsre/completion.go
index e84a5b75c..7f94dabfc 100644
--- a/jsre/completion.go
+++ b/jsre/completion.go
@@ -27,7 +27,9 @@ import (
// 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) })
+ jsre.Do(func(vm *otto.Otto) {
+ results = getCompletions(vm, line)
+ })
return results
}
@@ -53,9 +55,18 @@ func getCompletions(vm *otto.Otto, line string) (results []string) {
}
}
})
- // e.g. web3<tab><tab> append dot since its an object
- if obj, _ = vm.Object(line); obj != nil {
- results = append(results, line+".")
+
+ // 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)
diff --git a/jsre/completion_test.go b/jsre/completion_test.go
index 6d42b2bd1..8765281e5 100644
--- a/jsre/completion_test.go
+++ b/jsre/completion_test.go
@@ -40,7 +40,11 @@ func TestCompleteKeywords(t *testing.T) {
}{
{
input: "x",
- want: []string{"x", "x."},
+ want: []string{"x."},
+ },
+ {
+ input: "x.someMethod",
+ want: []string{"x.someMethod("},
},
{
input: "x.",
diff --git a/jsre/jsre.go b/jsre/jsre.go
index f4464910d..7df022cb1 100644
--- a/jsre/jsre.go
+++ b/jsre/jsre.go
@@ -214,8 +214,8 @@ loop:
self.loopWg.Done()
}
-// do schedules the given function on the event loop.
-func (self *JSRE) do(fn func(*otto.Otto)) {
+// Do executes the given function on the JS event loop.
+func (self *JSRE) Do(fn func(*otto.Otto)) {
done := make(chan bool)
req := &evalReq{fn, done}
self.evalQueue <- req
@@ -235,7 +235,7 @@ func (self *JSRE) Exec(file string) error {
if err != nil {
return err
}
- self.do(func(vm *otto.Otto) { _, err = vm.Run(code) })
+ self.Do(func(vm *otto.Otto) { _, err = vm.Run(code) })
return err
}
@@ -247,19 +247,19 @@ func (self *JSRE) Bind(name string, v interface{}) error {
// Run runs a piece of JS code.
func (self *JSRE) Run(code string) (v otto.Value, err error) {
- self.do(func(vm *otto.Otto) { v, err = vm.Run(code) })
+ self.Do(func(vm *otto.Otto) { v, err = vm.Run(code) })
return v, err
}
// Get returns the value of a variable in the JS environment.
func (self *JSRE) Get(ns string) (v otto.Value, err error) {
- self.do(func(vm *otto.Otto) { v, err = vm.Get(ns) })
+ self.Do(func(vm *otto.Otto) { v, err = vm.Get(ns) })
return v, err
}
// Set assigns value v to a variable in the JS environment.
func (self *JSRE) Set(ns string, v interface{}) (err error) {
- self.do(func(vm *otto.Otto) { err = vm.Set(ns, v) })
+ self.Do(func(vm *otto.Otto) { err = vm.Set(ns, v) })
return err
}
@@ -288,7 +288,7 @@ func (self *JSRE) loadScript(call otto.FunctionCall) otto.Value {
// EvalAndPrettyPrint evaluates code and pretty prints the result to
// standard output.
func (self *JSRE) EvalAndPrettyPrint(code string) (err error) {
- self.do(func(vm *otto.Otto) {
+ self.Do(func(vm *otto.Otto) {
var val otto.Value
val, err = vm.Run(code)
if err != nil {
@@ -302,7 +302,7 @@ func (self *JSRE) EvalAndPrettyPrint(code string) (err error) {
// Compile compiles and then runs a piece of JS code.
func (self *JSRE) Compile(filename string, src interface{}) (err error) {
- self.do(func(vm *otto.Otto) { _, err = compileAndRun(vm, filename, src) })
+ self.Do(func(vm *otto.Otto) { _, err = compileAndRun(vm, filename, src) })
return err
}
diff --git a/jsre/pretty.go b/jsre/pretty.go
index 8b2b35e8e..cd7fa5232 100644
--- a/jsre/pretty.go
+++ b/jsre/pretty.go
@@ -177,7 +177,7 @@ func (ctx ppctx) fields(obj *otto.Object) []string {
seen = make(map[string]bool)
)
add := func(k string) {
- if seen[k] || boringKeys[k] {
+ if seen[k] || boringKeys[k] || strings.HasPrefix(k, "_") {
return
}
seen[k] = true