-
Notifications
You must be signed in to change notification settings - Fork 2
/
symbols.go
340 lines (309 loc) · 12.8 KB
/
symbols.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
// jsonnet-language-server: A Language Server Protocol server for Jsonnet.
// Copyright (C) 2021 Jack Baldry
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"os"
"github.com/google/go-jsonnet/ast"
"github.com/jdbaldry/go-language-server-protocol/lsp/protocol"
)
// locationRangeToProtocolRange translates a ast.LocationRange to a protocol.Range.
// The former is one indexed and the latter is zero indexed.
func locationRangeToProtocolRange(lr ast.LocationRange) protocol.Range {
return protocol.Range{
Start: protocol.Position{Line: uint32(lr.Begin.Line - 1), Character: uint32(lr.Begin.Column - 1)},
End: protocol.Position{Line: uint32(lr.End.Line - 1), Character: uint32(lr.End.Column - 1)},
}
}
// analyseSymbols traverses the Jsonnet AST and produces a hierarchy of LSP symbols.
func analyseSymbols(n ast.Node) (symbols []protocol.DocumentSymbol) {
switch n := n.(type) {
case *ast.Apply:
args := make([]protocol.DocumentSymbol, len(n.Arguments.Named))
for i, bind := range n.Arguments.Named {
args[i] = protocol.DocumentSymbol{
Name: string(bind.Name),
Kind: protocol.Variable,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Tags: []protocol.SymbolTag{symbolTagDefinition},
Children: analyseSymbols(bind.Arg),
}
}
for _, bind := range n.Arguments.Positional {
// TODO(#17): Evaluate the bind expression to know what the argument is.
args = append(args, protocol.DocumentSymbol{
Name: "expr",
Kind: protocol.Variable,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Tags: []protocol.SymbolTag{symbolTagDefinition},
Children: analyseSymbols(bind.Expr),
})
}
symbols = append(symbols, protocol.DocumentSymbol{
Name: "apply",
Kind: protocol.Function,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Children: append(args, analyseSymbols(n.Target)...),
})
case *ast.Array:
children := []protocol.DocumentSymbol{}
for _, elem := range n.Elements {
children = append(children, analyseSymbols(elem.Expr)...)
}
symbols = append(symbols, protocol.DocumentSymbol{
Name: "array",
Kind: protocol.Array,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Children: children,
})
case *ast.Binary:
children := analyseSymbols(n.Left)
children = append(children, analyseSymbols(n.Right)...)
symbols = append(symbols, protocol.DocumentSymbol{
Name: n.Op.String(),
Kind: protocol.Operator,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Children: children,
})
case *ast.Conditional:
symbols = append(symbols, protocol.DocumentSymbol{
Name: "cond",
Deprecated: false,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Children: append(append(analyseSymbols(n.Cond), analyseSymbols(n.BranchTrue)...), analyseSymbols(n.BranchFalse)...),
})
case *ast.DesugaredObject:
locals := make([]protocol.DocumentSymbol, len(n.Locals))
for i, bind := range n.Locals {
// This variable is where `$` references for all children of this object.
// Although this local has children, that is only a self reference and is currently ignored.
if string(bind.Variable) == "$" {
locals[i] = protocol.DocumentSymbol{
Name: string(bind.Variable),
Kind: protocol.Variable,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Tags: []protocol.SymbolTag{symbolTagDefinition},
}
} else {
locals[i] = protocol.DocumentSymbol{
Name: string(bind.Variable),
Kind: protocol.Variable,
Range: locationRangeToProtocolRange(bind.LocRange),
SelectionRange: locationRangeToProtocolRange(bind.LocRange),
Tags: []protocol.SymbolTag{symbolTagDefinition},
Children: analyseSymbols(bind.Body),
}
}
}
fields := make([]protocol.DocumentSymbol, len(n.Fields))
for i, field := range n.Fields {
fields[i] = protocol.DocumentSymbol{
Name: "field",
Kind: protocol.Field,
Range: locationRangeToProtocolRange(field.LocRange),
SelectionRange: locationRangeToProtocolRange(field.LocRange),
Children: append(analyseSymbols(field.Name), analyseSymbols(field.Body)...),
}
}
symbols = append(symbols, protocol.DocumentSymbol{
Name: "object",
Kind: protocol.Object,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Children: append(locals, fields...),
})
case *ast.Error:
symbols = append(symbols, protocol.DocumentSymbol{
Name: "error",
Kind: protocol.Event,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Children: analyseSymbols(n.Expr),
})
case *ast.Function:
params := make([]protocol.DocumentSymbol, len(n.Parameters))
for i, param := range n.Parameters {
params[i] = protocol.DocumentSymbol{
Name: string(param.Name),
Kind: protocol.Variable,
Range: locationRangeToProtocolRange(param.LocRange),
SelectionRange: locationRangeToProtocolRange(param.LocRange),
Tags: []protocol.SymbolTag{symbolTagDefinition},
}
if param.DefaultArg != nil {
params[i].Children = analyseSymbols(param.DefaultArg)
}
}
symbols = append(symbols, protocol.DocumentSymbol{
Name: "function",
Kind: protocol.Function,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Children: append(params, analyseSymbols(n.Body)...),
})
case *ast.Import:
// Although the literal string is a child of an Import, it is ignored and the keyword
// and the literal string are captured by a single File document.
// This is one less symbol in the hierarchy and it means that go to definition works
// from either the keyword or the literal string.
// The same is true for ImportStr.
symbols = append(symbols, protocol.DocumentSymbol{
Name: n.File.Value,
Kind: protocol.File,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
})
case *ast.ImportStr:
// See comment associated with Import.
symbols = append(symbols, protocol.DocumentSymbol{
Name: n.File.Value,
Kind: protocol.File,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
})
case *ast.Index:
symbols = append(symbols, protocol.DocumentSymbol{
Name: "index",
Kind: protocol.Field,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Tags: []protocol.SymbolTag{symbolTagDefinition},
Children: append(analyseSymbols(n.Target), analyseSymbols(n.Index)...),
})
case *ast.InSuper:
symbols = append(symbols, protocol.DocumentSymbol{
Name: "index",
Kind: protocol.Field,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Tags: []protocol.SymbolTag{symbolTagDefinition},
Children: analyseSymbols(n.Index),
})
case *ast.LiteralBoolean:
symbols = append(symbols, protocol.DocumentSymbol{
Name: fmt.Sprint(n.Value),
Kind: protocol.Boolean,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
})
case *ast.LiteralNull:
symbols = append(symbols, protocol.DocumentSymbol{
Name: "null",
Kind: protocol.Null,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
})
case *ast.LiteralNumber:
symbols = append(symbols, protocol.DocumentSymbol{
Name: n.OriginalString,
Kind: protocol.Number,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
})
case *ast.LiteralString:
symbols = append(symbols, protocol.DocumentSymbol{
Name: n.Value,
Kind: protocol.String,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
})
case *ast.Local:
binds := make([]protocol.DocumentSymbol, len(n.Binds))
for i, bind := range n.Binds {
binds[i] = protocol.DocumentSymbol{
Name: string(bind.Variable),
Kind: protocol.Variable,
Tags: []protocol.SymbolTag{symbolTagDefinition},
Children: analyseSymbols(bind.Body),
}
// If the line is zero, it must be unset as Jsonnet location ranges are indexed at one.
// This seems to only happen with local definitions of functions which are preceded with the token "local".
// Adding five (five minus the one for zero indexing plus one for a space) to the location range of the local
// symbol gets closer to the real location but any amount of whitespace could be inbetween.
// Assuming a single space, this works perfectly.
// TODO(#7): Understand why identifiers in local function binds are missing.
if bind.LocRange.Begin.Line == 0 {
binds[i].Range = protocol.Range{
Start: protocol.Position{Line: uint32(n.Loc().Begin.Line - 1), Character: uint32(n.Loc().Begin.Column + 5)},
End: protocol.Position{Line: uint32(n.Loc().End.Line - 1), Character: uint32(n.Loc().End.Column + 5)},
}
binds[i].SelectionRange = binds[i].Range
} else {
binds[i].Range = locationRangeToProtocolRange(bind.LocRange)
binds[i].SelectionRange = locationRangeToProtocolRange(bind.LocRange)
}
}
symbols = append(symbols, protocol.DocumentSymbol{
Name: "local",
Kind: protocol.Namespace,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Children: append(binds, analyseSymbols(n.Body)...),
})
case *ast.Self:
symbols = append(symbols, protocol.DocumentSymbol{
Name: "self",
Kind: protocol.Variable,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Tags: []protocol.SymbolTag{symbolTagDefinition},
})
case *ast.SuperIndex:
symbols = append(symbols, protocol.DocumentSymbol{
Name: "index",
Kind: protocol.Field,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Children: append([]protocol.DocumentSymbol{{
Name: "super",
Kind: protocol.Variable,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
}}, analyseSymbols(n.Index)...),
})
case *ast.Unary:
symbols = append(symbols, protocol.DocumentSymbol{
Name: n.Op.String(),
Kind: protocol.Operator,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
Children: analyseSymbols(n.Expr),
})
case *ast.Var:
symbols = append(symbols, protocol.DocumentSymbol{
Name: string(n.Id),
Kind: protocol.Variable,
Range: locationRangeToProtocolRange(*n.Loc()),
SelectionRange: locationRangeToProtocolRange(*n.Loc()),
})
default:
fmt.Fprintf(os.Stderr, "analyseSymbols: unhandled node: %T\n", n)
}
return
}
// isDefinition returns true if a symbol is tagged as a definition.
func isDefinition(s protocol.DocumentSymbol) bool {
for _, t := range s.Tags {
if t == symbolTagDefinition {
return true
}
}
return false
}