@@ -15,13 +15,47 @@ namespace Microsoft.PowerShell.EditorServices.Language
15
15
internal class FindReferencesVisitor : AstVisitor
16
16
{
17
17
private SymbolReference symbolRef ;
18
+ private Dictionary < String , List < String > > CmdletToAliasDictionary ;
19
+ private Dictionary < String , String > AliasToCmdletDictionary ;
20
+ private string symbolRefCommandName ;
21
+ private bool needsAliases ;
18
22
19
23
public List < SymbolReference > FoundReferences { get ; set ; }
20
24
21
- public FindReferencesVisitor ( SymbolReference symbolRef )
25
+ /// <summary>
26
+ /// Constructor used when searching for aliases is needed
27
+ /// </summary>
28
+ /// <param name="symbolReference">The found symbolReference that other symbols are being compared to</param>
29
+ /// <param name="CmdletToAliasDictionary">Dictionary maping cmdlets to aliases for finding alias references</param>
30
+ /// <param name="AliasToCmdletDictionary">Dictionary maping aliases to cmdlets for finding alias references</param>
31
+ public FindReferencesVisitor (
32
+ SymbolReference symbolReference ,
33
+ Dictionary < String , List < String > > CmdletToAliasDictionary ,
34
+ Dictionary < String , String > AliasToCmdletDictionary )
22
35
{
23
- this . symbolRef = symbolRef ;
36
+ this . symbolRef = symbolReference ;
24
37
this . FoundReferences = new List < SymbolReference > ( ) ;
38
+ this . needsAliases = true ;
39
+ this . CmdletToAliasDictionary = CmdletToAliasDictionary ;
40
+ this . AliasToCmdletDictionary = AliasToCmdletDictionary ;
41
+
42
+ // Try to get the symbolReference's command name of an alias,
43
+ // if a command name does not exists (if the symbol isn't an alias to a command)
44
+ // set symbolRefCommandName to and empty string value
45
+ AliasToCmdletDictionary . TryGetValue ( symbolReference . ScriptRegion . Text , out symbolRefCommandName ) ;
46
+ if ( symbolRefCommandName == null ) { symbolRefCommandName = string . Empty ; }
47
+
48
+ }
49
+
50
+ /// <summary>
51
+ /// Constructor used when searching for aliases is not needed
52
+ /// </summary>
53
+ /// <param name="foundSymbol">The found symbolReference that other symbols are being compared to</param>
54
+ public FindReferencesVisitor ( SymbolReference foundSymbol )
55
+ {
56
+ this . symbolRef = foundSymbol ;
57
+ this . FoundReferences = new List < SymbolReference > ( ) ;
58
+ this . needsAliases = false ;
25
59
}
26
60
27
61
/// <summary>
@@ -34,12 +68,51 @@ public FindReferencesVisitor(SymbolReference symbolRef)
34
68
public override AstVisitAction VisitCommand ( CommandAst commandAst )
35
69
{
36
70
Ast commandNameAst = commandAst . CommandElements [ 0 ] ;
37
- if ( symbolRef . SymbolType . Equals ( SymbolType . Function ) &&
38
- commandNameAst . Extent . Text . Equals ( symbolRef . ScriptRegion . Text , StringComparison . InvariantCultureIgnoreCase ) )
71
+ string commandName = commandNameAst . Extent . Text ;
72
+
73
+ if ( symbolRef . SymbolType . Equals ( SymbolType . Function ) )
39
74
{
40
- this . FoundReferences . Add ( new SymbolReference (
41
- SymbolType . Function ,
42
- commandNameAst . Extent ) ) ;
75
+ if ( needsAliases )
76
+ {
77
+ // Try to get the commandAst's name and aliases,
78
+ // if a command does not exists (if the symbol isn't an alias to a command)
79
+ // set command to and empty string value string command
80
+ // if the aliases do not exist (if the symvol isn't a command that has aliases)
81
+ // set aliases to an empty List<string>
82
+ string command ;
83
+ List < string > alaises ;
84
+ CmdletToAliasDictionary . TryGetValue ( commandName , out alaises ) ;
85
+ AliasToCmdletDictionary . TryGetValue ( commandName , out command ) ;
86
+ if ( alaises == null ) { alaises = new List < string > ( ) ; }
87
+ if ( command == null ) { command = string . Empty ; }
88
+
89
+ if ( symbolRef . SymbolType . Equals ( SymbolType . Function ) )
90
+ {
91
+ // Check if the found symbol's name is the same as the commandAst's name OR
92
+ // if the symbol's name is an alias for this commandAst's name (commandAst is a cmdlet) OR
93
+ // if the symbol's name is the same as the commandAst's cmdlet name (commandAst is a alias)
94
+ if ( commandName . Equals ( symbolRef . SymbolName , StringComparison . InvariantCultureIgnoreCase ) ||
95
+ alaises . Contains ( symbolRef . ScriptRegion . Text . ToLower ( ) ) ||
96
+ command . Equals ( symbolRef . ScriptRegion . Text , StringComparison . InvariantCultureIgnoreCase ) ||
97
+ ( ! command . Equals ( string . Empty ) && command . Equals ( symbolRefCommandName , StringComparison . InvariantCultureIgnoreCase ) ) )
98
+ {
99
+ this . FoundReferences . Add ( new SymbolReference (
100
+ SymbolType . Function ,
101
+ commandNameAst . Extent ) ) ;
102
+ }
103
+ }
104
+
105
+ }
106
+ else // search does not include aliases
107
+ {
108
+ if ( commandName . Equals ( symbolRef . SymbolName , StringComparison . InvariantCultureIgnoreCase ) )
109
+ {
110
+ this . FoundReferences . Add ( new SymbolReference (
111
+ SymbolType . Function ,
112
+ commandNameAst . Extent ) ) ;
113
+ }
114
+ }
115
+
43
116
}
44
117
return base . VisitCommand ( commandAst ) ;
45
118
}
@@ -52,6 +125,8 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
52
125
/// <returns>A visit action that continues the search for references</returns>
53
126
public override AstVisitAction VisitFunctionDefinition ( FunctionDefinitionAst functionDefinitionAst )
54
127
{
128
+ // Get the start column number of the function name,
129
+ // instead of the the start column of 'function' and create new extent for the functionName
55
130
int startColumnNumber =
56
131
functionDefinitionAst . Extent . Text . IndexOf (
57
132
functionDefinitionAst . Name ) + 1 ;
0 commit comments