-
Notifications
You must be signed in to change notification settings - Fork 760
/
Copy pathSemanticTokenVisitor.cs
445 lines (385 loc) · 17.9 KB
/
SemanticTokenVisitor.cs
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Bicep.Core;
using Bicep.Core.Parsing;
using Bicep.Core.Semantics;
using Bicep.Core.Syntax;
using Bicep.Core.TypeSystem;
using Bicep.LanguageServer.Extensions;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
namespace Bicep.LanguageServer
{
public class SemanticTokenVisitor : CstVisitor
{
private readonly SemanticModel model;
private readonly List<(IPositionable positionable, SemanticTokenType tokenType)> tokens = new();
private SemanticTokenVisitor(SemanticModel model)
{
this.model = model;
}
public static void BuildSemanticTokens(SemanticTokensBuilder builder, SemanticModel model)
{
var visitor = new SemanticTokenVisitor(model);
visitor.Visit(model.SourceFile.ProgramSyntax);
// the builder is fussy about ordering. tokens are visited out of order, we need to call build after visiting everything
foreach (var (positionable, tokenType) in visitor.tokens.OrderBy(t => t.positionable.Span.Position))
{
var tokenRanges = positionable.ToRangeSpanningLines(model.SourceFile.LineStarts);
foreach (var tokenRange in tokenRanges)
{
builder.Push(tokenRange.Start.Line, tokenRange.Start.Character, tokenRange.End.Character - tokenRange.Start.Character, tokenType as SemanticTokenType?);
}
}
}
private void AddTokenType(IPositionable? positionable, SemanticTokenType tokenType)
{
if (positionable is not null)
{
tokens.Add((positionable, tokenType));
}
}
/// <summary>
/// Adds the specified positionable element if it represents the specified keyword.
/// This function only needs to be used if the parser does not prevent SkippedTriviaSyntax in place of the keyword.
/// </summary>
/// <param name="positionable">the positionable element</param>
/// <param name="keyword">the expected keyword text</param>
private void AddContextualKeyword(IPositionable positionable, string keyword)
{
// contextual keywords should only be highlighted as keywords if they are valid
if (positionable is Token { Type: TokenType.Identifier } token && string.Equals(token.Text, keyword, StringComparison.Ordinal))
{
AddTokenType(positionable, SemanticTokenType.Keyword);
}
}
public override void VisitBinaryOperationSyntax(BinaryOperationSyntax syntax)
{
AddTokenType(syntax.OperatorToken, SemanticTokenType.Operator);
base.VisitBinaryOperationSyntax(syntax);
}
public override void VisitBooleanLiteralSyntax(BooleanLiteralSyntax syntax)
{
AddTokenType(syntax.Literal, SemanticTokenType.Keyword);
base.VisitBooleanLiteralSyntax(syntax);
}
public override void VisitBooleanTypeLiteralSyntax(BooleanTypeLiteralSyntax syntax)
{
AddTokenType(syntax.Literal, SemanticTokenType.Keyword);
base.VisitBooleanTypeLiteralSyntax(syntax);
}
public override void VisitFunctionCallSyntax(FunctionCallSyntax syntax)
{
// We need to set token types for OpenParen and CloseParen in case the function call
// is inside a string interpolation. Our current textmate grammar will tag them as
// string if they are not overrode by the semantic tokens.
AddTokenType(syntax.Name, SemanticTokenType.Function);
AddTokenType(syntax.OpenParen, SemanticTokenType.Operator);
AddTokenType(syntax.CloseParen, SemanticTokenType.Operator);
base.VisitFunctionCallSyntax(syntax);
}
public override void VisitInstanceFunctionCallSyntax(InstanceFunctionCallSyntax syntax)
{
AddTokenType(syntax.Dot, SemanticTokenType.Operator);
AddTokenType(syntax.Name, SemanticTokenType.Function);
AddTokenType(syntax.OpenParen, SemanticTokenType.Operator);
AddTokenType(syntax.CloseParen, SemanticTokenType.Operator);
base.VisitInstanceFunctionCallSyntax(syntax);
}
public override void VisitNullLiteralSyntax(NullLiteralSyntax syntax)
{
AddTokenType(syntax.NullKeyword, SemanticTokenType.Keyword);
base.VisitNullLiteralSyntax(syntax);
}
public override void VisitNullTypeLiteralSyntax(NullTypeLiteralSyntax syntax)
{
AddTokenType(syntax.NullKeyword, SemanticTokenType.Keyword);
base.VisitNullTypeLiteralSyntax(syntax);
}
public override void VisitIntegerLiteralSyntax(IntegerLiteralSyntax syntax)
{
AddTokenType(syntax.Literal, SemanticTokenType.Number);
base.VisitIntegerLiteralSyntax(syntax);
}
public override void VisitObjectPropertySyntax(ObjectPropertySyntax syntax)
{
if (syntax.Key is StringSyntax @string)
{
Visit(@string);
}
else
{
AddTokenType(syntax.Key, SemanticTokenType.TypeParameter);
}
Visit(syntax.Colon);
Visit(syntax.Value);
}
public override void VisitOutputDeclarationSyntax(OutputDeclarationSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
AddTokenType(syntax.Name, SemanticTokenType.Variable);
base.VisitOutputDeclarationSyntax(syntax);
}
public override void VisitParameterDeclarationSyntax(ParameterDeclarationSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
AddTokenType(syntax.Name, SemanticTokenType.Variable);
base.VisitParameterDeclarationSyntax(syntax);
}
public override void VisitTypeDeclarationSyntax(TypeDeclarationSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
AddTokenType(syntax.Name, SemanticTokenType.Type);
base.VisitTypeDeclarationSyntax(syntax);
}
public override void VisitPropertyAccessSyntax(PropertyAccessSyntax syntax)
{
AddTokenType(syntax.Dot, SemanticTokenType.Operator);
AddTokenType(syntax.PropertyName, SemanticTokenType.Property);
base.VisitPropertyAccessSyntax(syntax);
}
public override void VisitTypePropertyAccessSyntax(TypePropertyAccessSyntax syntax)
{
AddTokenType(syntax.Dot, SemanticTokenType.Operator);
AddTokenType(syntax.PropertyName, GetSemanticTokenForPotentialTypeSymbol(model.GetSymbolInfo(syntax)));
base.VisitTypePropertyAccessSyntax(syntax);
}
public override void VisitArrayAccessSyntax(ArrayAccessSyntax syntax)
{
AddTokenType(syntax.OpenSquare, SemanticTokenType.Operator);
AddTokenType(syntax.CloseSquare, SemanticTokenType.Operator);
base.VisitArrayAccessSyntax(syntax);
}
public override void VisitResourceAccessSyntax(ResourceAccessSyntax syntax)
{
AddTokenType(syntax.ResourceName, SemanticTokenType.Property);
base.VisitResourceAccessSyntax(syntax);
}
public override void VisitResourceDeclarationSyntax(ResourceDeclarationSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
AddTokenType(syntax.Name, SemanticTokenType.Variable);
AddTokenType(syntax.ExistingKeyword, SemanticTokenType.Keyword);
base.VisitResourceDeclarationSyntax(syntax);
}
public override void VisitModuleDeclarationSyntax(ModuleDeclarationSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
AddTokenType(syntax.Name, SemanticTokenType.Variable);
base.VisitModuleDeclarationSyntax(syntax);
}
public override void VisitTestDeclarationSyntax(TestDeclarationSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
AddTokenType(syntax.Name, SemanticTokenType.Variable);
base.VisitTestDeclarationSyntax(syntax);
}
public override void VisitIfConditionSyntax(IfConditionSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
base.VisitIfConditionSyntax(syntax);
}
public override void VisitForSyntax(ForSyntax syntax)
{
AddTokenType(syntax.ForKeyword, SemanticTokenType.Keyword);
AddContextualKeyword(syntax.InKeyword, LanguageConstants.InKeyword);
base.VisitForSyntax(syntax);
}
public override void VisitLocalVariableSyntax(LocalVariableSyntax syntax)
{
AddTokenType(syntax.Name, SemanticTokenType.Variable);
base.VisitLocalVariableSyntax(syntax);
}
private void AddStringToken(Token token)
{
var endInterp = token.Type switch
{
TokenType.StringLeftPiece => LanguageConstants.StringHoleOpen,
TokenType.StringMiddlePiece => LanguageConstants.StringHoleOpen,
_ => ""
};
var startInterp = token.Type switch
{
TokenType.StringMiddlePiece => LanguageConstants.StringHoleClose,
TokenType.StringRightPiece => LanguageConstants.StringHoleClose,
_ => ""
};
// need to be extremely cautious here. it may be that lexing has failed to find a string terminating character,
// but we still have a syntax tree to display tokens for.
var hasEndOperator = endInterp.Length > 0 && token.Text.EndsWith(endInterp, StringComparison.Ordinal);
var endOperatorLength = hasEndOperator ? endInterp.Length : 0;
var hasStartOperator = startInterp.Length > 0 && token.Text.Length >= startInterp.Length;
var startOperatorLength = hasStartOperator ? startInterp.Length : 0;
if (hasStartOperator)
{
AddTokenType(token.GetSpanSlice(0, startOperatorLength), SemanticTokenType.Operator);
}
AddTokenType(token.GetSpanSlice(startOperatorLength, token.Span.Length - startOperatorLength - endOperatorLength), SemanticTokenType.String);
if (hasEndOperator)
{
AddTokenType(token.GetSpanSlice(token.Span.Length - endOperatorLength, endOperatorLength), SemanticTokenType.Operator);
}
}
public override void VisitTernaryOperationSyntax(TernaryOperationSyntax syntax)
{
AddTokenType(syntax.Colon, SemanticTokenType.Operator);
AddTokenType(syntax.Question, SemanticTokenType.Operator);
base.VisitTernaryOperationSyntax(syntax);
}
public override void VisitToken(Token token)
{
switch (token.Type)
{
case TokenType.StringComplete:
case TokenType.StringLeftPiece:
case TokenType.StringMiddlePiece:
case TokenType.StringRightPiece:
case TokenType.MultilineString:
AddStringToken(token);
break;
case TokenType.Comma:
AddTokenType(token, SemanticTokenType.Operator);
break;
default:
break;
}
base.VisitToken(token);
}
public override void VisitSyntaxTrivia(SyntaxTrivia syntaxTrivia)
{
switch (syntaxTrivia.Type)
{
case SyntaxTriviaType.SingleLineComment:
case SyntaxTriviaType.MultiLineComment:
AddTokenType(syntaxTrivia, SemanticTokenType.Comment);
break;
case SyntaxTriviaType.DisableNextLineDiagnosticsDirective:
AddTokenType(syntaxTrivia, SemanticTokenType.Macro);
break;
}
}
public override void VisitResourceTypeSyntax(ResourceTypeSyntax syntax)
{
// This is intentional, we want 'resource' to look like 'object' or 'array'.
AddTokenType(syntax.Keyword, SemanticTokenType.Type);
base.VisitResourceTypeSyntax(syntax);
}
public override void VisitUnaryOperationSyntax(UnaryOperationSyntax syntax)
{
AddTokenType(syntax.OperatorToken, SemanticTokenType.Operator);
base.VisitUnaryOperationSyntax(syntax);
}
public override void VisitVariableAccessSyntax(VariableAccessSyntax syntax)
{
AddTokenType(syntax.Name, GetSemanticTokenForPotentialTypeSymbol(model.GetSymbolInfo(syntax)));
base.VisitVariableAccessSyntax(syntax);
}
public override void VisitTypeVariableAccessSyntax(TypeVariableAccessSyntax syntax)
{
AddTokenType(syntax.Name, GetSemanticTokenForPotentialTypeSymbol(model.GetSymbolInfo(syntax)));
base.VisitTypeVariableAccessSyntax(syntax);
}
public override void VisitMetadataDeclarationSyntax(MetadataDeclarationSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
AddTokenType(syntax.Name, SemanticTokenType.Variable);
base.VisitMetadataDeclarationSyntax(syntax);
}
public override void VisitVariableDeclarationSyntax(VariableDeclarationSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
AddTokenType(syntax.Name, SemanticTokenType.Variable);
base.VisitVariableDeclarationSyntax(syntax);
}
public override void VisitTargetScopeSyntax(TargetScopeSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
base.VisitTargetScopeSyntax(syntax);
}
public override void VisitExtensionDeclarationSyntax(ExtensionDeclarationSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
this.Visit(syntax.SpecificationString);
this.Visit(syntax.WithClause);
this.Visit(syntax.AsClause);
}
public override void VisitExtensionWithClauseSyntax(ExtensionWithClauseSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
this.Visit(syntax.Config);
}
public override void VisitAliasAsClauseSyntax(AliasAsClauseSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
AddTokenType(syntax.Alias, SemanticTokenType.Namespace);
}
public override void VisitCompileTimeImportDeclarationSyntax(CompileTimeImportDeclarationSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
base.VisitCompileTimeImportDeclarationSyntax(syntax);
}
public override void VisitImportedSymbolsListItemSyntax(ImportedSymbolsListItemSyntax syntax)
{
if (syntax.OriginalSymbolName is IdentifierSyntax)
{
AddTokenType(syntax.OriginalSymbolName, model.GetSymbolInfo(syntax)?.Kind switch
{
Core.Semantics.SymbolKind.TypeAlias => SemanticTokenType.Type,
_ => SemanticTokenType.Variable,
});
}
base.VisitImportedSymbolsListItemSyntax(syntax);
}
public override void VisitWildcardImportSyntax(WildcardImportSyntax syntax)
{
AddTokenType(syntax.Wildcard, SemanticTokenType.Variable);
base.VisitWildcardImportSyntax(syntax);
}
public override void VisitCompileTimeImportFromClauseSyntax(CompileTimeImportFromClauseSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
base.VisitCompileTimeImportFromClauseSyntax(syntax);
}
public override void VisitUsingDeclarationSyntax(UsingDeclarationSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
base.VisitUsingDeclarationSyntax(syntax);
}
public override void VisitParameterAssignmentSyntax(ParameterAssignmentSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
AddTokenType(syntax.Name, SemanticTokenType.Variable);
base.VisitParameterAssignmentSyntax(syntax);
}
public override void VisitAssertDeclarationSyntax(AssertDeclarationSyntax syntax)
{
AddTokenType(syntax.Keyword, SemanticTokenType.Keyword);
AddTokenType(syntax.Name, SemanticTokenType.Variable);
base.VisitAssertDeclarationSyntax(syntax);
}
public override void VisitObjectTypePropertySyntax(ObjectTypePropertySyntax syntax)
{
if (syntax.Key is StringSyntax @string)
{
Visit(@string);
}
else
{
AddTokenType(syntax.Key, SemanticTokenType.TypeParameter);
}
Visit(syntax.Colon);
Visit(syntax.Value);
}
private static SemanticTokenType GetSemanticTokenForPotentialTypeSymbol(Symbol? symbol) =>
symbol switch
{
PropertySymbol property => GetSemanticTokenForPotentialTypeSymbol(property.Type),
TypeSymbol or
TypeAliasSymbol or
AmbientTypeSymbol or
ImportedTypeSymbol => SemanticTokenType.Type,
INamespaceSymbol => SemanticTokenType.Namespace,
_ => SemanticTokenType.Variable,
};
}
}