github.com/insight-chain/inb-go@v1.1.3-0.20191221022159-da049980ae38/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  	//inb by ssh begin
    38  	/*if strings.Contains(line, "eth") {
    39  		return nil
    40  	}
    41  	line = strings.Replace(line, "inb", "eth", -1)
    42  	flag1 := false
    43  	flag2 := false*/
    44  
    45  	parts := strings.Split(line, ".")
    46  	objRef := "this"
    47  	prefix := line
    48  	if len(parts) > 1 {
    49  		objRef = strings.Join(parts[0:len(parts)-1], ".")
    50  		prefix = parts[len(parts)-1]
    51  	} /*else {
    52  		if prefix == "i" || prefix == "in" {
    53  			flag1 = true
    54  		}
    55  		if prefix == "e" || prefix == "et" {
    56  			flag2 = true
    57  		}
    58  	}*/
    59  
    60  	obj, _ := vm.Object(objRef)
    61  	if obj == nil {
    62  		return nil
    63  	}
    64  	iterOwnAndConstructorKeys(vm, obj, func(k string) {
    65  		if strings.HasPrefix(k, prefix) {
    66  			if objRef == "this" {
    67  				results = append(results, k)
    68  			} else {
    69  				results = append(results, strings.Join(parts[:len(parts)-1], ".")+"."+k)
    70  			}
    71  		}
    72  	})
    73  
    74  	//if flag1 {
    75  	//	results = append(results, "inb")
    76  	//}
    77  
    78  	// Append opening parenthesis (for functions) or dot (for objects)
    79  	// if the line itself is the only completion.
    80  	if len(results) == 1 && results[0] == line {
    81  		obj, _ := vm.Object(line)
    82  		if obj != nil {
    83  			if obj.Class() == "Function" {
    84  				results[0] += "("
    85  			} else {
    86  				results[0] += "."
    87  			}
    88  		}
    89  	}
    90  
    91  	//if !flag2 {
    92  	//	for i, result := range results {
    93  	//		results[i] = strings.Replace(result, "eth", "inb", -1)
    94  	//	}
    95  	//}
    96  	sort.Strings(results)
    97  	return results
    98  	//inb by ssh end
    99  }