completion.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. package jsre
  17. import (
  18. "sort"
  19. "strings"
  20. "github.com/dop251/goja"
  21. )
  22. // CompleteKeywords returns potential continuations for the given line. Since line is
  23. // evaluated, callers need to make sure that evaluating line does not have side effects.
  24. func (jsre *JSRE) CompleteKeywords(line string) []string {
  25. var results []string
  26. jsre.Do(func(vm *goja.Runtime) {
  27. results = getCompletions(vm, line)
  28. })
  29. return results
  30. }
  31. func getCompletions(vm *goja.Runtime, line string) (results []string) {
  32. parts := strings.Split(line, ".")
  33. if len(parts) == 0 {
  34. return nil
  35. }
  36. // Find the right-most fully named object in the line. e.g. if line = "x.y.z"
  37. // and "x.y" is an object, obj will reference "x.y".
  38. obj := vm.GlobalObject()
  39. for i := 0; i < len(parts)-1; i++ {
  40. v := obj.Get(parts[i])
  41. if v == nil {
  42. return nil // No object was found
  43. }
  44. obj = v.ToObject(vm)
  45. }
  46. // Go over the keys of the object and retain the keys matching prefix.
  47. // Example: if line = "x.y.z" and "x.y" exists and has keys "zebu", "zebra"
  48. // and "platypus", then "x.y.zebu" and "x.y.zebra" will be added to results.
  49. prefix := parts[len(parts)-1]
  50. iterOwnAndConstructorKeys(vm, obj, func(k string) {
  51. if strings.HasPrefix(k, prefix) {
  52. if len(parts) == 1 {
  53. results = append(results, k)
  54. } else {
  55. results = append(results, strings.Join(parts[:len(parts)-1], ".")+"."+k)
  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 := obj.Get(parts[len(parts)-1])
  63. if obj != nil {
  64. if _, isfunc := goja.AssertFunction(obj); isfunc {
  65. results[0] += "("
  66. } else {
  67. results[0] += "."
  68. }
  69. }
  70. }
  71. sort.Strings(results)
  72. return results
  73. }