22// Licensed under the MIT License.
33
44using System ;
5+ using System . Collections . Concurrent ;
56using System . Collections . Generic ;
67using System . Management . Automation . Language ;
78
@@ -12,48 +13,47 @@ namespace Microsoft.PowerShell.EditorServices.Services.Symbols
1213 /// </summary>
1314 internal class FindReferencesVisitor : AstVisitor
1415 {
15- private SymbolReference symbolRef ;
16- private Dictionary < String , List < String > > CmdletToAliasDictionary ;
17- private Dictionary < String , String > AliasToCmdletDictionary ;
18- private string symbolRefCommandName ;
19- private bool needsAliases ;
16+ private readonly SymbolReference _symbolRef ;
17+ private readonly ConcurrentDictionary < string , List < string > > _cmdletToAliasDictionary ;
18+ private readonly ConcurrentDictionary < string , string > _aliasToCmdletDictionary ;
19+ private readonly string _symbolRefCommandName ;
20+ private readonly bool _needsAliases ;
2021
2122 public List < SymbolReference > FoundReferences { get ; set ; }
2223
2324 /// <summary>
2425 /// Constructor used when searching for aliases is needed
2526 /// </summary>
2627 /// <param name="symbolReference">The found symbolReference that other symbols are being compared to</param>
27- /// <param name="CmdletToAliasDictionary ">Dictionary maping cmdlets to aliases for finding alias references</param>
28- /// <param name="AliasToCmdletDictionary ">Dictionary maping aliases to cmdlets for finding alias references</param>
28+ /// <param name="cmdletToAliasDictionary ">Dictionary maping cmdlets to aliases for finding alias references</param>
29+ /// <param name="aliasToCmdletDictionary ">Dictionary maping aliases to cmdlets for finding alias references</param>
2930 public FindReferencesVisitor (
3031 SymbolReference symbolReference ,
31- Dictionary < String , List < String > > CmdletToAliasDictionary ,
32- Dictionary < String , String > AliasToCmdletDictionary )
32+ ConcurrentDictionary < string , List < string > > cmdletToAliasDictionary = default ,
33+ ConcurrentDictionary < string , string > aliasToCmdletDictionary = default )
3334 {
34- this . symbolRef = symbolReference ;
35- this . FoundReferences = new List < SymbolReference > ( ) ;
36- this . needsAliases = true ;
37- this . CmdletToAliasDictionary = CmdletToAliasDictionary ;
38- this . AliasToCmdletDictionary = AliasToCmdletDictionary ;
35+ _symbolRef = symbolReference ;
36+ FoundReferences = new List < SymbolReference > ( ) ;
37+ 38+ if ( cmdletToAliasDictionary is null || aliasToCmdletDictionary is null )
39+ {
40+ _needsAliases = false ;
41+ return ;
42+ }
43+ 44+ _needsAliases = true ;
45+ _cmdletToAliasDictionary = cmdletToAliasDictionary ;
46+ _aliasToCmdletDictionary = aliasToCmdletDictionary ;
3947
4048 // Try to get the symbolReference's command name of an alias,
4149 // if a command name does not exists (if the symbol isn't an alias to a command)
4250 // set symbolRefCommandName to and empty string value
43- AliasToCmdletDictionary . TryGetValue ( symbolReference . ScriptRegion . Text , out symbolRefCommandName ) ;
44- if ( symbolRefCommandName == null ) { symbolRefCommandName = string . Empty ; }
51+ aliasToCmdletDictionary . TryGetValue ( symbolReference . ScriptRegion . Text , out _symbolRefCommandName ) ;
4552
46- }
47- 48- /// <summary>
49- /// Constructor used when searching for aliases is not needed
50- /// </summary>
51- /// <param name="foundSymbol">The found symbolReference that other symbols are being compared to</param>
52- public FindReferencesVisitor ( SymbolReference foundSymbol )
53- {
54- this . symbolRef = foundSymbol ;
55- this . FoundReferences = new List < SymbolReference > ( ) ;
56- this . needsAliases = false ;
53+ if ( _symbolRefCommandName == null )
54+ {
55+ _symbolRefCommandName = string . Empty ;
56+ }
5757 }
5858
5959 /// <summary>
@@ -68,50 +68,44 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
6868 Ast commandNameAst = commandAst . CommandElements [ 0 ] ;
6969 string commandName = commandNameAst . Extent . Text ;
7070
71- if ( symbolRef . SymbolType . Equals ( SymbolType . Function ) )
71+ if ( _symbolRef . SymbolType . Equals ( SymbolType . Function ) )
7272 {
73- if ( needsAliases )
73+ if ( _needsAliases )
7474 {
7575 // Try to get the commandAst's name and aliases,
7676 // if a command does not exists (if the symbol isn't an alias to a command)
7777 // set command to and empty string value string command
7878 // if the aliases do not exist (if the symvol isn't a command that has aliases)
7979 // set aliases to an empty List<string>
80- string command ;
81- List < string > alaises ;
82- CmdletToAliasDictionary . TryGetValue ( commandName , out alaises ) ;
83- AliasToCmdletDictionary . TryGetValue ( commandName , out command ) ;
84- if ( alaises == null ) { alaises = new List < string > ( ) ; }
80+ _cmdletToAliasDictionary . TryGetValue ( commandName , out List < string > aliases ) ;
81+ _aliasToCmdletDictionary . TryGetValue ( commandName , out string command ) ;
82+ if ( aliases == null ) { aliases = new List < string > ( ) ; }
8583 if ( command == null ) { command = string . Empty ; }
8684
87- if ( symbolRef . SymbolType . Equals ( SymbolType . Function ) )
85+ if ( _symbolRef . SymbolType . Equals ( SymbolType . Function ) )
8886 {
8987 // Check if the found symbol's name is the same as the commandAst's name OR
9088 // if the symbol's name is an alias for this commandAst's name (commandAst is a cmdlet) OR
9189 // if the symbol's name is the same as the commandAst's cmdlet name (commandAst is a alias)
92- if ( commandName . Equals ( symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) ||
93- alaises . Contains ( symbolRef . ScriptRegion . Text . ToLower ( ) ) ||
94- command . Equals ( symbolRef . ScriptRegion . Text , StringComparison . CurrentCultureIgnoreCase ) ||
95- ( ! string . IsNullOrEmpty ( command ) && command . Equals ( symbolRefCommandName , StringComparison . CurrentCultureIgnoreCase ) ) )
90+ if ( commandName . Equals ( _symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase )
91+ || aliases . Contains ( _symbolRef . ScriptRegion . Text . ToLower ( ) )
92+ || command . Equals ( _symbolRef . ScriptRegion . Text , StringComparison . CurrentCultureIgnoreCase )
93+ || ( ! string . IsNullOrEmpty ( command )
94+ && command . Equals ( _symbolRefCommandName , StringComparison . CurrentCultureIgnoreCase ) ) )
9695 {
97- this . FoundReferences . Add ( new SymbolReference (
98- SymbolType . Function ,
99- commandNameAst . Extent ) ) ;
96+ FoundReferences . Add ( new SymbolReference ( SymbolType . Function , commandNameAst . Extent ) ) ;
10097 }
10198 }
102- 10399 }
104100 else // search does not include aliases
105101 {
106- if ( commandName . Equals ( symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
102+ if ( commandName . Equals ( _symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
107103 {
108- this . FoundReferences . Add ( new SymbolReference (
109- SymbolType . Function ,
110- commandNameAst . Extent ) ) ;
104+ FoundReferences . Add ( new SymbolReference ( SymbolType . Function , commandNameAst . Extent ) ) ;
111105 }
112106 }
113- 114107 }
108+ 115109 return base . VisitCommand ( commandAst ) ;
116110 }
117111
@@ -135,12 +129,10 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
135129 File = functionDefinitionAst . Extent . File
136130 } ;
137131
138- if ( symbolRef . SymbolType . Equals ( SymbolType . Function ) &&
139- nameExtent . Text . Equals ( symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
132+ if ( _symbolRef . SymbolType . Equals ( SymbolType . Function ) &&
133+ nameExtent . Text . Equals ( _symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
140134 {
141- this . FoundReferences . Add ( new SymbolReference (
142- SymbolType . Function ,
143- nameExtent ) ) ;
135+ FoundReferences . Add ( new SymbolReference ( SymbolType . Function , nameExtent ) ) ;
144136 }
145137 return base . VisitFunctionDefinition ( functionDefinitionAst ) ;
146138 }
@@ -153,12 +145,10 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
153145 /// <returns>A visit action that continues the search for references</returns>
154146 public override AstVisitAction VisitCommandParameter ( CommandParameterAst commandParameterAst )
155147 {
156- if ( symbolRef . SymbolType . Equals ( SymbolType . Parameter ) &&
157- commandParameterAst . Extent . Text . Equals ( symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
148+ if ( _symbolRef . SymbolType . Equals ( SymbolType . Parameter ) &&
149+ commandParameterAst . Extent . Text . Equals ( _symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
158150 {
159- this . FoundReferences . Add ( new SymbolReference (
160- SymbolType . Parameter ,
161- commandParameterAst . Extent ) ) ;
151+ FoundReferences . Add ( new SymbolReference ( SymbolType . Parameter , commandParameterAst . Extent ) ) ;
162152 }
163153 return AstVisitAction . Continue ;
164154 }
@@ -171,12 +161,10 @@ public override AstVisitAction VisitCommandParameter(CommandParameterAst command
171161 /// <returns>A visit action that continues the search for references</returns>
172162 public override AstVisitAction VisitVariableExpression ( VariableExpressionAst variableExpressionAst )
173163 {
174- if ( symbolRef . SymbolType . Equals ( SymbolType . Variable ) &&
175- variableExpressionAst . Extent . Text . Equals ( symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
164+ if ( _symbolRef . SymbolType . Equals ( SymbolType . Variable )
165+ && variableExpressionAst . Extent . Text . Equals ( _symbolRef . SymbolName , StringComparison . CurrentCultureIgnoreCase ) )
176166 {
177- this . FoundReferences . Add ( new SymbolReference (
178- SymbolType . Variable ,
179- variableExpressionAst . Extent ) ) ;
167+ FoundReferences . Add ( new SymbolReference ( SymbolType . Variable , variableExpressionAst . Extent ) ) ;
180168 }
181169 return AstVisitAction . Continue ;
182170 }
@@ -186,7 +174,7 @@ private static (int, int) GetStartColumnAndLineNumbersFromAst(FunctionDefinition
186174 {
187175 int startColumnNumber = ast . Extent . StartColumnNumber ;
188176 int startLineNumber = ast . Extent . StartLineNumber ;
189- int astOffset = 0 ;
177+ int astOffset ;
190178
191179 if ( ast . IsFilter )
192180 {
0 commit comments