github.com/unicornultrafoundation/go-ethereum@v1.9.7-0.20191031044858-21506be82b68/internal/jsre/completion.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package jsre 18 19 import ( 20 "sort" 21 "strings" 22 23 "github.com/robertkrimen/otto" 24 ) 25 26 // CompleteKeywords returns potential continuations for the given line. Since line is 27 // evaluated, callers need to make sure that evaluating line does not have side effects. 28 func (jsre *JSRE) CompleteKeywords(line string) []string { 29 var results []string 30 jsre.Do(func(vm *otto.Otto) { 31 results = getCompletions(vm, line) 32 }) 33 return results 34 } 35 36 func getCompletions(vm *otto.Otto, line string) (results []string) { 37 parts := strings.Split(line, ".") 38 objRef := "this" 39 prefix := line 40 if len(parts) > 1 { 41 objRef = strings.Join(parts[0:len(parts)-1], ".") 42 prefix = parts[len(parts)-1] 43 } 44 45 obj, _ := vm.Object(objRef) 46 if obj == nil { 47 return nil 48 } 49 iterOwnAndConstructorKeys(vm, obj, func(k string) { 50 if strings.HasPrefix(k, prefix) { 51 if objRef == "this" { 52 results = append(results, k) 53 } else { 54 results = append(results, strings.Join(parts[:len(parts)-1], ".")+"."+k) 55 } 56 } 57 }) 58 59 // Append opening parenthesis (for functions) or dot (for objects) 60 // if the line itself is the only completion. 61 if len(results) == 1 && results[0] == line { 62 obj, _ := vm.Object(line) 63 if obj != nil { 64 if obj.Class() == "Function" { 65 results[0] += "(" 66 } else { 67 results[0] += "." 68 } 69 } 70 } 71 72 sort.Strings(results) 73 return results 74 }