forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
176 lines (144 loc) · 4.15 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package exec
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/aergoio/aergo/cmd/brick/context"
)
var index = make(map[string]map[string]string)
func Candidates(cmd string, chunks []context.Chunk, current int, symbol string) map[string]string {
if ret := search(cmd, chunks, current, symbol); ret != nil {
return ret
}
ret := make(map[string]string)
// if there is no suggestion, then return general
if descr, ok := context.Symbols[symbol]; ok {
ret[symbol] = descr
}
return ret
}
// search contract args using get abi
func extractContractAndFuncName(cmd string, chunks []context.Chunk) (string, string) {
var contractName string
var funcName string
executor := GetExecutor(cmd)
if executor != nil {
symbols := strings.Fields(executor.Syntax())
for i, symbol := range symbols {
if len(chunks) <= i {
break
}
if symbol == context.ContractSymbol {
// compare with symbol in syntax and extract contract name
contractName = chunks[i].Text
} else if symbol == context.FunctionSymbol {
// extract function name
funcName = chunks[i].Text
}
}
}
return contractName, funcName
}
func search(cmd string, chunks []context.Chunk, current int, symbol string) map[string]string {
if keywords, ok := index[symbol]; ok {
return keywords
}
if symbol == context.FunctionSymbol {
contractName, _ := extractContractAndFuncName(cmd, chunks)
if contractName != "" {
return searchFuncHint(contractName)
}
} else if symbol == context.ContractArgsSymbol {
contractName, funcName := extractContractAndFuncName(cmd, chunks)
if contractName != "" && funcName != "" {
// search abi using contract and function name
return searchAbiHint(contractName, funcName)
}
} else if symbol == context.PathSymbol {
if len(chunks) <= current { //there is no word yet
return searchInPath(context.Chunk{Text: ".", Accent: false})
}
return searchInPath(chunks[current])
}
return nil
}
func searchFuncHint(contractName string) map[string]string {
// read receipt and extract abi functions
ret := make(map[string]string)
abi, err := context.Get().GetABI(contractName)
if err != nil {
ret["<error>"] = err.Error()
return ret
}
for _, contractFunc := range abi.Functions {
// gather functions
ret[contractFunc.Name] = ""
}
return ret
}
func searchAbiHint(contractName, funcName string) map[string]string {
abi, err := context.Get().GetABI(contractName)
if err != nil {
return nil
}
for _, contractFunc := range abi.Functions {
if contractFunc.Name == funcName {
argsHint := "`["
for i, funcArg := range contractFunc.GetArguments() {
argsHint += funcArg.Name
if i+1 != len(contractFunc.GetArguments()) {
argsHint += ", "
}
}
argsHint += "]`"
ret := make(map[string]string)
ret[argsHint] = context.Symbols[context.ContractArgsSymbol]
return ret
}
}
return nil
}
func searchInPath(chunk context.Chunk) map[string]string {
if strings.HasSuffix(chunk.Text, ".") {
// attach file sperator, to get files in this relative path
chunk.Text = fmt.Sprintf("%s%c", chunk.Text, filepath.Separator)
}
ret := make(map[string]string)
// extract parent directory path
dir := filepath.Dir(chunk.Text)
// navigate file list in the parent directory
fileInfo, err := ioutil.ReadDir(dir)
if err != nil {
ret[err.Error()] = ""
return ret
}
// detatch last base path
// other function internally use filepath.Clean() that remove text . or ..
// it makes prompt filter hard to match suggestions and the input
currentDir, _ := filepath.Split(chunk.Text)
for _, file := range fileInfo {
// generate suggestion text
fullPath := currentDir + file.Name()
if file.IsDir() {
fullPath += string(os.PathSeparator)
}
if chunk.Accent {
fullPath = "`" + fullPath // attach accent again
}
// if contains white space...
if wsIdx := strings.LastIndex(fullPath, " "); wsIdx != -1 {
// cut it because auto completer will switch text only after whitespace
fullPath = fullPath[wsIdx+1:]
}
ret[fullPath] = ""
}
return ret
}
func Index(symbol, text string) {
if _, ok := index[symbol]; !ok {
index[symbol] = make(map[string]string)
}
index[symbol][text] = symbol
}