1
- // Copyright (c) Microsoft Corporation.
1
+ // Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
3
4
4
using System ;
@@ -11,7 +11,7 @@ namespace Microsoft.PowerShell.EditorServices.Services.Symbols
11
11
/// <summary>
12
12
/// The visitor used to find the references of a symbol in a script's AST
13
13
/// </summary>
14
- internal class FindReferencesVisitor : AstVisitor
14
+ internal class FindReferencesVisitor : AstVisitor2
15
15
{
16
16
private readonly SymbolReference _symbolRef ;
17
17
private readonly IDictionary < string , List < string > > _cmdletToAliasDictionary ;
@@ -168,5 +168,164 @@ public override AstVisitAction VisitVariableExpression(VariableExpressionAst var
168
168
}
169
169
return AstVisitAction . Continue ;
170
170
}
171
+
172
+ /// <summary>
173
+ /// Decides if the current type definition is a reference of the symbol being searched for.
174
+ /// A reference of the symbol will be a of type SymbolType.Class or SymbolType.Enum and have the same name as the symbol
175
+ /// </summary>
176
+ /// <param name="typeDefinitionAst">A TypeDefinitionAst in the script's AST</param>
177
+ /// <returns>A visit action that continues the search for references</returns>
178
+ public override AstVisitAction VisitTypeDefinition ( TypeDefinitionAst typeDefinitionAst )
179
+ {
180
+ SymbolType symbolType =
181
+ typeDefinitionAst . IsEnum ?
182
+ SymbolType . Enum : SymbolType . Class ;
183
+
184
+ if ( ( _symbolRef . SymbolType is SymbolType . Type || _symbolRef . SymbolType . Equals ( symbolType ) ) &&
185
+ typeDefinitionAst . Name . Equals ( _symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
186
+ {
187
+ // Show only type name. Offset by StartColumn to include indentation etc.
188
+ int startColumnNumber =
189
+ typeDefinitionAst . Extent . StartColumnNumber +
190
+ typeDefinitionAst . Extent . Text . IndexOf ( typeDefinitionAst . Name ) ;
191
+
192
+ IScriptExtent nameExtent = new ScriptExtent ( )
193
+ {
194
+ Text = typeDefinitionAst . Name ,
195
+ StartLineNumber = typeDefinitionAst . Extent . StartLineNumber ,
196
+ EndLineNumber = typeDefinitionAst . Extent . StartLineNumber ,
197
+ StartColumnNumber = startColumnNumber ,
198
+ EndColumnNumber = startColumnNumber + typeDefinitionAst . Name . Length ,
199
+ File = typeDefinitionAst . Extent . File
200
+ } ;
201
+
202
+ FoundReferences . Add ( new SymbolReference ( symbolType , nameExtent ) ) ;
203
+ }
204
+ return AstVisitAction . Continue ;
205
+ }
206
+
207
+ /// <summary>
208
+ /// Decides if the current type expression is a reference of the symbol being searched for.
209
+ /// A reference of the symbol will be a of type SymbolType.Type and have the same name as the symbol
210
+ /// </summary>
211
+ /// <param name="typeExpressionAst">A TypeExpressionAst in the script's AST</param>
212
+ /// <returns>A visit action that continues the search for references</returns>
213
+ public override AstVisitAction VisitTypeExpression ( TypeExpressionAst typeExpressionAst )
214
+ {
215
+ // We don't know if we're looking at a class or enum, but name is likely unique
216
+ if ( IsTypeSymbol ( _symbolRef . SymbolType ) &&
217
+ typeExpressionAst . TypeName . Name . Equals ( _symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
218
+ {
219
+ FoundReferences . Add ( new SymbolReference ( SymbolType . Type , typeExpressionAst . Extent ) ) ;
220
+ }
221
+ return AstVisitAction . Continue ;
222
+ }
223
+
224
+ /// <summary>
225
+ /// Decides if the current type constraint is a reference of the symbol being searched for.
226
+ /// A reference of the symbol will be a of type SymbolType.Type and have the same name as the symbol
227
+ /// </summary>
228
+ /// <param name="typeConstraintAst">A TypeConstraintAst in the script's AST</param>
229
+ /// <returns>A visit action that continues the search for references</returns>
230
+ public override AstVisitAction VisitTypeConstraint ( TypeConstraintAst typeConstraintAst )
231
+ {
232
+ // We don't know if we're looking at a class or enum, but name is likely unique
233
+ if ( IsTypeSymbol ( _symbolRef . SymbolType ) &&
234
+ typeConstraintAst . TypeName . Name . Equals ( _symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
235
+ {
236
+ FoundReferences . Add ( new SymbolReference ( SymbolType . Type , typeConstraintAst . Extent ) ) ;
237
+ }
238
+ return AstVisitAction . Continue ;
239
+ }
240
+
241
+ /// <summary>
242
+ /// Decides if the current function member is a reference of the symbol being searched for.
243
+ /// A reference of the symbol will be a of type SymbolType.Constructor or SymbolType.Method and have the same name as the symbol
244
+ /// </summary>
245
+ /// <param name="functionMemberAst">A FunctionMemberAst in the script's AST</param>
246
+ /// <returns>A visit action that continues the search for references</returns>
247
+ public override AstVisitAction VisitFunctionMember ( FunctionMemberAst functionMemberAst )
248
+ {
249
+ SymbolType symbolType =
250
+ functionMemberAst . IsConstructor ?
251
+ SymbolType . Constructor : SymbolType . Method ;
252
+
253
+ if ( _symbolRef . SymbolType . Equals ( symbolType ) &&
254
+ functionMemberAst . Name . Equals ( _symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
255
+ {
256
+ // Show only method/ctor name. Offset by StartColumn to include indentation etc.
257
+ int startColumnNumber =
258
+ functionMemberAst . Extent . StartColumnNumber +
259
+ functionMemberAst . Extent . Text . IndexOf ( functionMemberAst . Name ) ;
260
+
261
+ IScriptExtent nameExtent = new ScriptExtent ( )
262
+ {
263
+ Text = functionMemberAst . Name ,
264
+ StartLineNumber = functionMemberAst . Extent . StartLineNumber ,
265
+ EndLineNumber = functionMemberAst . Extent . StartLineNumber ,
266
+ StartColumnNumber = startColumnNumber ,
267
+ EndColumnNumber = startColumnNumber + functionMemberAst . Name . Length ,
268
+ File = functionMemberAst . Extent . File
269
+ } ;
270
+
271
+ FoundReferences . Add ( new SymbolReference ( symbolType , nameExtent ) ) ;
272
+ }
273
+ return AstVisitAction . Continue ;
274
+ }
275
+
276
+ /// <summary>
277
+ /// Decides if the current property member is a reference of the symbol being searched for.
278
+ /// A reference of the symbol will be a of type SymbolType.Property and have the same name as the symbol
279
+ /// </summary>
280
+ /// <param name="propertyMemberAst">A PropertyMemberAst in the script's AST</param>
281
+ /// <returns>A visit action that continues the search for references</returns>
282
+ public override AstVisitAction VisitPropertyMember ( PropertyMemberAst propertyMemberAst )
283
+ {
284
+ if ( _symbolRef . SymbolType . Equals ( SymbolType . Property ) &&
285
+ propertyMemberAst . Name . Equals ( _symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
286
+ {
287
+ FoundReferences . Add ( new SymbolReference ( SymbolType . Property , propertyMemberAst . Extent ) ) ;
288
+ }
289
+ return AstVisitAction . Continue ;
290
+ }
291
+
292
+ /// <summary>
293
+ /// Decides if the current configuration definition is a reference of the symbol being searched for.
294
+ /// A reference of the symbol will be a of type SymbolType.Configuration and have the same name as the symbol
295
+ /// </summary>
296
+ /// <param name="configurationDefinitionAst">A ConfigurationDefinitionAst in the script's AST</param>
297
+ /// <returns>A visit action that continues the search for references</returns>
298
+ public override AstVisitAction VisitConfigurationDefinition ( ConfigurationDefinitionAst configurationDefinitionAst )
299
+ {
300
+ string configurationName = configurationDefinitionAst . InstanceName . Extent . Text ;
301
+
302
+ if ( _symbolRef . SymbolType . Equals ( SymbolType . Configuration ) &&
303
+ configurationName . Equals ( _symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
304
+ {
305
+ // Show only configuration name. Offset by StartColumn to include indentation etc.
306
+ int startColumnNumber =
307
+ configurationDefinitionAst . Extent . StartColumnNumber +
308
+ configurationDefinitionAst . Extent . Text . IndexOf ( configurationName ) ;
309
+
310
+ IScriptExtent nameExtent = new ScriptExtent ( )
311
+ {
312
+ Text = configurationName ,
313
+ StartLineNumber = configurationDefinitionAst . Extent . StartLineNumber ,
314
+ EndLineNumber = configurationDefinitionAst . Extent . StartLineNumber ,
315
+ StartColumnNumber = startColumnNumber ,
316
+ EndColumnNumber = startColumnNumber + configurationName . Length ,
317
+ File = configurationDefinitionAst . Extent . File
318
+ } ;
319
+
320
+ FoundReferences . Add ( new SymbolReference ( SymbolType . Configuration , nameExtent ) ) ;
321
+ }
322
+ return AstVisitAction . Continue ;
323
+ }
324
+
325
+ /// <summary>
326
+ /// Tests if symbol type is a type (class/enum) definition or type reference.
327
+ /// </summary>
328
+ private static bool IsTypeSymbol ( SymbolType symbolType )
329
+ => symbolType is SymbolType . Class or SymbolType . Enum or SymbolType . Type ;
171
330
}
172
331
}
0 commit comments