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