/
symmap.go
366 lines (341 loc) · 9.53 KB
/
symmap.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright (c) 2018, The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package syms
import (
"encoding/json"
"io"
"io/ioutil"
"path/filepath"
"sort"
"strings"
"github.com/goki/ki/kit"
"github.com/goki/pi/lex"
"github.com/goki/pi/token"
)
// SymMap is a map between symbol names and their full information.
// A given project will have a top-level SymMap and perhaps local
// maps for individual files, etc. Namespaces / packages can be
// created and elements added to them to create appropriate
// scoping structure etc. Note that we have to use pointers
// for symbols b/c otherwise it is very expensive to re-assign
// values all the time -- https://github.com/golang/go/issues/3117
type SymMap map[string]*Symbol
// Alloc ensures that map is made
func (sm *SymMap) Alloc() {
if *sm == nil {
*sm = make(SymMap)
}
}
// Add adds symbol to map
func (sm *SymMap) Add(sy *Symbol) {
sm.Alloc()
(*sm)[sy.Name] = sy
}
// AddNew adds a new symbol to the map with the basic info
func (sm *SymMap) AddNew(name string, kind token.Tokens, fname string, reg lex.Reg) *Symbol {
sy := NewSymbol(name, kind, fname, reg)
sm.Alloc()
(*sm)[name] = sy
return sy
}
// Reset resets the symbol map
func (sm *SymMap) Reset() {
*sm = make(SymMap)
}
// CopyFrom copies all the symbols from given source map into this one,
// including merging everything from common elements.
// Symbols with Type resolved are retained when there are duplicates.
// srcIsNewer means that the src map has the newer information to grab for
// updating the symbol region info during the merge.
func (sm *SymMap) CopyFrom(src SymMap, srcIsNewer bool) {
sm.Alloc()
for nm, ssy := range src {
dsy, has := (*sm)[nm]
if !has {
(*sm)[nm] = ssy
continue
}
if srcIsNewer {
dsy.CopyFromSrc(ssy)
} else {
ssy.CopyFromSrc(dsy)
}
if dsy.Type != "" {
// fmt.Printf("dupe sym: %v, using existing with type: %v\n", nm, dsy.Type)
// fmt.Printf("\texisting region: %v new source region: %v\n", dsy.Region, ssy.Region)
dsy.Children.CopyFrom(ssy.Children, srcIsNewer)
} else if ssy.Type != "" {
// fmt.Printf("dupe sym: %v, using new with type: %v\n", nm, ssy.Type)
// fmt.Printf("\texisting region: %v new source region: %v\n", dsy.Region, ssy.Region)
ssy.Children.CopyFrom(dsy.Children, !srcIsNewer)
(*sm)[nm] = ssy
} else {
dsy.Children.CopyFrom(ssy.Children, srcIsNewer)
}
}
}
// First returns the first symbol in the map -- only sensible when there
// is just one such element
func (sm *SymMap) First() *Symbol {
for _, sy := range *sm {
return sy
}
return nil
}
// Slice returns a slice of the elements in the map, optionally sorted by name
func (sm *SymMap) Slice(sorted bool) []*Symbol {
sys := make([]*Symbol, len(*sm))
idx := 0
for _, sy := range *sm {
sys[idx] = sy
idx++
}
if sorted {
sort.Slice(sys, func(i, j int) bool {
return sys[i].Name < sys[j].Name
})
}
return sys
}
// FindName looks for given symbol name within this map and any children on the map
func (sm *SymMap) FindName(nm string) (*Symbol, bool) {
if *sm == nil {
return nil, false
}
sy, has := (*sm)[nm]
if has {
return sy, has
}
for _, ss := range *sm {
if sy, has = ss.Children.FindName(nm); has {
return sy, has
}
}
return nil, false
}
// FindNameScoped looks for given symbol name within this map and any children on the map
// that are of subcategory token.NameScope (i.e., namespace, module, package, library)
func (sm *SymMap) FindNameScoped(nm string) (*Symbol, bool) {
if *sm == nil {
return nil, false
}
sy, has := (*sm)[nm]
if has {
return sy, has
}
for _, ss := range *sm {
if ss.Kind.SubCat() == token.NameScope {
if sy, has = ss.Children.FindNameScoped(nm); has {
return sy, has
}
}
}
return nil, false
}
// FindKind looks for given symbol kind within this map and any children on the map
// Returns all instances found. Uses cat / subcat based token matching -- if you
// specify a category-level or subcategory level token, it will match everything in that group
func (sm *SymMap) FindKind(kind token.Tokens, matches *SymMap) {
if *sm == nil {
return
}
for _, sy := range *sm {
if kind.Match(sy.Kind) {
if *matches == nil {
*matches = make(SymMap)
}
(*matches)[sy.Name] = sy
}
}
for _, ss := range *sm {
ss.Children.FindKind(kind, matches)
}
}
// FindKindScoped looks for given symbol kind within this map and any children on the map
// that are of subcategory token.NameScope (i.e., namespace, module, package, library).
// Returns all instances found. Uses cat / subcat based token matching -- if you
// specify a category-level or subcategory level token, it will match everything in that group
func (sm *SymMap) FindKindScoped(kind token.Tokens, matches *SymMap) {
if *sm == nil {
return
}
for _, sy := range *sm {
if kind.Match(sy.Kind) {
if *matches == nil {
*matches = make(SymMap)
}
(*matches)[sy.Name] = sy
}
}
for _, ss := range *sm {
if ss.Kind.SubCat() == token.NameScope {
ss.Children.FindKindScoped(kind, matches)
}
}
}
// FindContainsRegion looks for given symbol kind that contains the given
// source file path (must be filepath.Abs file path) and position.
// Returns all instances found. Uses cat / subcat based token matching -- if you
// specify a category-level or subcategory level token, it will match everything
// in that group. if you specify kind = token.None then all tokens that contain
// region will be returned. extraLns are extra lines added to the symbol region
// for purposes of matching.
func (sm *SymMap) FindContainsRegion(fpath string, pos lex.Pos, extraLns int, kind token.Tokens, matches *SymMap) {
if *sm == nil {
return
}
for _, sy := range *sm {
fp, _ := filepath.Abs(sy.Filename)
if fp != fpath {
continue
}
reg := sy.Region
if extraLns > 0 {
reg.Ed.Ln += extraLns
}
if !reg.Contains(pos) {
continue
}
if kind == token.None || kind.Match(sy.Kind) {
if *matches == nil {
*matches = make(SymMap)
}
(*matches)[sy.Name] = sy
}
}
for _, ss := range *sm {
ss.Children.FindContainsRegion(fpath, pos, extraLns, kind, matches)
}
}
// OpenJSON opens from a JSON-formatted file.
func (sm *SymMap) OpenJSON(filename string) error {
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return json.Unmarshal(b, sm)
}
// SaveJSON saves to a JSON-formatted file.
func (sm *SymMap) SaveJSON(filename string) error {
b, err := json.MarshalIndent(sm, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(filename, b, 0644)
return err
}
// Names returns a slice of the names in this map, optionally sorted
func (sm *SymMap) Names(sorted bool) []string {
nms := make([]string, len(*sm))
idx := 0
for _, sy := range *sm {
nms[idx] = sy.Name
idx++
}
if sorted {
sort.StringSlice(nms).Sort()
}
return nms
}
// KindNames returns a slice of the kind:names in this map, optionally sorted
func (sm *SymMap) KindNames(sorted bool) []string {
nms := make([]string, len(*sm))
idx := 0
for _, sy := range *sm {
nms[idx] = sy.Kind.String() + ":" + sy.Name
idx++
}
if sorted {
sort.StringSlice(nms).Sort()
}
return nms
}
// WriteDoc writes basic doc info, sorted by kind and name
func (sm *SymMap) WriteDoc(out io.Writer, depth int) {
nms := sm.KindNames(true)
for _, nm := range nms {
ci := strings.Index(nm, ":")
sy := (*sm)[nm[ci+1:]]
sy.WriteDoc(out, depth)
}
}
//////////////////////////////////////////////////////////////////////
// Partial lookups
// FindNamePrefix looks for given symbol name prefix within this map
// adds to given matches map (which can be nil), for more efficient recursive use
func (sm *SymMap) FindNamePrefix(seed string, matches *SymMap) {
noCase := true
if kit.HasUpperCase(seed) {
noCase = false
}
for _, sy := range *sm {
nm := sy.Name
if noCase {
nm = strings.ToLower(nm)
}
if strings.HasPrefix(nm, seed) {
if *matches == nil {
*matches = make(SymMap)
}
(*matches)[sy.Name] = sy
}
}
}
// FindNamePrefixRecursive looks for given symbol name prefix within this map
// and any children on the map.
// adds to given matches map (which can be nil), for more efficient recursive use
func (sm *SymMap) FindNamePrefixRecursive(seed string, matches *SymMap) {
noCase := true
if kit.HasUpperCase(seed) {
noCase = false
}
for _, sy := range *sm {
nm := sy.Name
if noCase {
nm = strings.ToLower(nm)
}
if strings.HasPrefix(nm, seed) {
if *matches == nil {
*matches = make(SymMap)
}
(*matches)[sy.Name] = sy
}
}
for _, ss := range *sm {
ss.Children.FindNamePrefixRecursive(seed, matches)
}
}
// FindNamePrefixScoped looks for given symbol name prefix within this map
// and any children on the map that are of subcategory
// token.NameScope (i.e., namespace, module, package, library)
// adds to given matches map (which can be nil), for more efficient recursive use
func (sm *SymMap) FindNamePrefixScoped(seed string, matches *SymMap) {
noCase := true
if kit.HasUpperCase(seed) {
noCase = false
}
for _, sy := range *sm {
nm := sy.Name
if nm[0] == '"' {
nm = strings.Trim(nm, `"`) // path names may be quoted
nm = filepath.Base(nm) // sorry, this is a bit of a Go-specific hack to look at package names only
sy = sy.Clone()
sy.Name = nm
}
if noCase {
nm = strings.ToLower(nm)
}
if strings.HasPrefix(nm, seed) {
if *matches == nil {
*matches = make(SymMap)
}
(*matches)[nm] = sy
}
}
for _, ss := range *sm {
if ss.Kind.SubCat() == token.NameScope {
ss.Children.FindNamePrefixScoped(seed, matches)
}
}
}