-
Notifications
You must be signed in to change notification settings - Fork 1
/
builtins.go
336 lines (293 loc) · 7.83 KB
/
builtins.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
package analysis
import (
"context"
_ "embed"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"github.com/pkg/errors"
"go.lsp.dev/protocol"
"go.lsp.dev/uri"
"github.com/kurtosis-tech/vscode-kurtosis/starlark-lsp/pkg/document"
"github.com/kurtosis-tech/vscode-kurtosis/starlark-lsp/pkg/query"
)
type Builtins struct {
Functions map[string]query.Signature
Symbols []query.Symbol
Types map[string]query.Type
Methods map[string]query.Signature
Members []query.Symbol
}
//go:embed builtins.py
var StarlarkBuiltins []byte
func symbolNames(ss []query.Symbol) []string {
names := make([]string, len(ss))
for i, sym := range ss {
names[i] = sym.Name
}
return names
}
func NewBuiltins() *Builtins {
return &Builtins{
Functions: make(map[string]query.Signature),
Symbols: []query.Symbol{},
Types: make(map[string]query.Type),
Methods: make(map[string]query.Signature),
Members: []query.Symbol{},
}
}
func (b *Builtins) IsEmpty() bool {
return len(b.Functions) == 0 && len(b.Symbols) == 0 &&
len(b.Methods) == 0 && len(b.Members) == 0
}
func (b *Builtins) Update(other *Builtins) {
if len(other.Functions) > 0 {
for name, fn := range other.Functions {
b.Functions[name] = fn
}
}
if len(other.Symbols) > 0 {
b.Symbols = append(b.Symbols, other.Symbols...)
}
if len(other.Types) > 0 {
for name, t := range other.Types {
b.Types[name] = t
}
}
if len(other.Methods) > 0 {
for name, fn := range other.Methods {
b.Methods[name] = fn
}
}
if len(other.Members) > 0 {
b.Members = append(b.Members, other.Members...)
}
}
func WithBuiltinPaths(paths []string) AnalyzerOption {
return func(analyzer *Analyzer) error {
for _, path := range paths {
builtins, err := LoadBuiltins(analyzer.context, path)
if err != nil {
return err
}
analyzer.builtins.Update(builtins)
}
return nil
}
}
func WithBuiltins(f fs.FS) AnalyzerOption {
return func(analyzer *Analyzer) error {
builtins, err := LoadBuiltinsFromFS(analyzer.context, f)
if err != nil {
return err
}
analyzer.builtins.Update(builtins)
return nil
}
}
func WithStarlarkBuiltinsWithCustomBuiltIn(kurtosisBuiltIn *Builtins) AnalyzerOption {
return func(analyzer *Analyzer) error {
builtins, err := LoadBuiltinsFromSource(analyzer.context, StarlarkBuiltins, "builtins.py")
if err != nil {
return errors.Wrapf(err, "loading builtins from builtins.py")
}
analyzer.builtins.Update(&Builtins{
Symbols: []query.Symbol{
{Name: "False", Kind: protocol.SymbolKindBoolean},
{Name: "None", Kind: protocol.SymbolKindNull},
{Name: "True", Kind: protocol.SymbolKindBoolean},
},
})
analyzer.builtins.Update(builtins)
analyzer.builtins.Update(kurtosisBuiltIn)
return nil
}
}
func WithStarlarkBuiltins() AnalyzerOption {
return func(analyzer *Analyzer) error {
builtins, err := LoadBuiltinsFromSource(analyzer.context, StarlarkBuiltins, "builtins.py")
if err != nil {
return errors.Wrapf(err, "loading builtins from builtins.py")
}
analyzer.builtins.Update(&Builtins{
Symbols: []query.Symbol{
{Name: "False", Kind: protocol.SymbolKindBoolean},
{Name: "None", Kind: protocol.SymbolKindNull},
{Name: "True", Kind: protocol.SymbolKindBoolean},
},
})
analyzer.builtins.Update(builtins)
return nil
}
}
func LoadBuiltinsFromSource(ctx context.Context, contents []byte, path string) (*Builtins, error) {
tree, err := query.Parse(ctx, contents)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %q", path)
}
doc := document.NewDocument(uri.File(path), contents, tree)
functions := doc.Functions()
symbols := doc.Symbols()
types := query.Types(doc, tree.RootNode())
typeMap := make(map[string]query.Type)
methodMap := make(map[string]query.Signature)
members := []query.Symbol{}
for _, t := range types {
typeMap[t.Name] = t
for _, method := range t.Methods {
methodMap[method.Name] = method
}
members = append(members, t.Members...)
}
doc.Close()
// NewDocument returns these symbols with a location of __init__.py, which isn't helpful to anyone
for i, s := range symbols {
s.Location = protocol.Location{}
symbols[i] = s
}
return &Builtins{
Functions: functions,
Symbols: symbols,
Types: typeMap,
Methods: methodMap,
Members: members,
}, nil
}
func LoadBuiltinsFromFile(ctx context.Context, path string, f fs.FS) (*Builtins, error) {
var contents []byte
var err error
if f != nil {
contents, err = fs.ReadFile(f, path)
} else {
contents, err = os.ReadFile(path)
}
if err != nil {
return nil, errors.Wrapf(err, "reading %s", path)
}
return LoadBuiltinsFromSource(ctx, contents, path)
}
func loadBuiltinsWalker(ctx context.Context, f fs.FS) (map[string]*Builtins, fs.WalkDirFunc) {
builtins := make(map[string]*Builtins)
return builtins, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
entryName := entry.Name()
if !entry.IsDir() && !strings.HasSuffix(entryName, ".py") {
return nil
}
modPath := path
if entry.IsDir() {
builtins[modPath] = NewBuiltins()
return nil
}
if entryName == "__init__.py" {
modPath = filepath.Dir(modPath)
} else {
modPath = path[:len(path)-len(".py")]
}
modBuiltins, err := LoadBuiltinsFromFile(ctx, path, f)
if err != nil {
return errors.Wrapf(err, "loading builtins from %s", path)
}
if b, ok := builtins[modPath]; ok {
b.Update(modBuiltins)
} else {
builtins[modPath] = modBuiltins
}
return nil
}
}
func LoadBuiltinsFromFS(ctx context.Context, f fs.FS) (*Builtins, error) {
root := "."
builtinsMap, walker := loadBuiltinsWalker(ctx, f)
err := fs.WalkDir(f, root, walker)
if err != nil {
return nil, errors.Wrapf(err, "walking %s", root)
}
modulePaths := make([]string, len(builtinsMap))
i := 0
for modPath := range builtinsMap {
modulePaths[i] = modPath
i++
}
sort.Sort(sort.Reverse(sort.StringSlice(modulePaths)))
for _, modPath := range modulePaths {
mod := builtinsMap[modPath]
if mod.IsEmpty() || modPath == root {
continue
}
modName := filepath.Base(modPath)
parentModPath := filepath.Dir(modPath)
parentMod, ok := builtinsMap[parentModPath]
if !ok {
return nil, fmt.Errorf("no entry for parent %s", parentModPath)
}
copyBuiltinsToParent(mod, parentMod, modName)
}
builtins, ok := builtinsMap[root]
if !ok {
return nil, fmt.Errorf("no entry for root %s", root)
}
return builtins, nil
}
func copyBuiltinsToParent(mod, parentMod *Builtins, modName string) {
for name, fn := range mod.Functions {
parentMod.Functions[modName+"."+name] = fn
}
children := []query.Symbol{}
for _, sym := range mod.Symbols {
var kind protocol.SymbolKind
switch sym.Kind {
case protocol.SymbolKindFunction:
kind = protocol.SymbolKindMethod
default:
kind = protocol.SymbolKindField
}
childSym := sym
childSym.Kind = kind
children = append(children, childSym)
}
if len(children) > 0 {
existingIndex := -1
for i, sym := range parentMod.Symbols {
if sym.Name == modName {
existingIndex = i
break
}
}
if existingIndex >= 0 {
parentMod.Symbols[existingIndex].Children = append(parentMod.Symbols[existingIndex].Children, children...)
} else {
parentMod.Symbols = append(parentMod.Symbols, query.Symbol{
Name: modName,
Kind: protocol.SymbolKindVariable,
Children: children,
})
}
}
parentMod.Update(&Builtins{
Types: mod.Types,
Methods: mod.Methods,
Members: mod.Members,
})
}
func LoadBuiltins(ctx context.Context, path string) (*Builtins, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return nil, errors.Wrapf(err, "statting %s", path)
}
var result *Builtins
if fileInfo.IsDir() {
result, err = LoadBuiltinsFromFS(ctx, os.DirFS(path))
} else {
result, err = LoadBuiltinsFromFile(ctx, path, nil)
}
if err != nil {
return nil, errors.Wrapf(err, "loading builtins from %s", path)
}
return result, nil
}