7
7
namespace Microsoft . PowerShell . EditorServices . Services . Symbols
8
8
{
9
9
/// <summary>
10
- /// The visitor used to find all the symbols (function and class defs) in the AST.
10
+ /// The visitor used to find all the symbols (variables, functions and class defs etc ) in the AST.
11
11
/// </summary>
12
- /// <remarks>
13
- /// Requires PowerShell v3 or higher
14
- /// </remarks>
15
- internal class FindSymbolsVisitor : AstVisitor
12
+ internal class FindSymbolsVisitor : AstVisitor2
16
13
{
17
14
public List < SymbolReference > SymbolReferences { get ; }
18
15
@@ -26,6 +23,12 @@ internal class FindSymbolsVisitor : AstVisitor
26
23
/// or a decision to continue if it wasn't found</returns>
27
24
public override AstVisitAction VisitFunctionDefinition ( FunctionDefinitionAst functionDefinitionAst )
28
25
{
26
+ // Extent for constructors and method trigger both this and VisitFunctionMember(). Covered in the latter.
27
+ // This will not exclude nested functions as they have ScriptBlockAst as parent
28
+ if ( functionDefinitionAst . Parent is FunctionMemberAst ) {
29
+ return AstVisitAction . Continue ;
30
+ }
31
+
29
32
IScriptExtent nameExtent = new ScriptExtent ( )
30
33
{
31
34
Text = functionDefinitionAst . Name ,
@@ -49,7 +52,7 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
49
52
}
50
53
51
54
/// <summary>
52
- /// Checks to see if this variable expression is the symbol we are looking for.
55
+ /// Checks to see if this variable expression is the symbol we are looking for.
53
56
/// </summary>
54
57
/// <param name="variableExpressionAst">A VariableExpressionAst object in the script's AST</param>
55
58
/// <returns>A decision to stop searching if the right symbol was found,
@@ -80,6 +83,132 @@ private static bool IsAssignedAtScriptScope(VariableExpressionAst variableExpres
80
83
parent = parent . Parent ;
81
84
return parent is null || parent . Parent is null || parent . Parent . Parent is null ;
82
85
}
86
+
87
+ /// <summary>
88
+ /// Adds class, enum and interface AST to symbol reference list
89
+ /// </summary>
90
+ public override AstVisitAction VisitTypeDefinition ( TypeDefinitionAst typeDefinitionAst )
91
+ {
92
+ IScriptExtent nameExtent = new ScriptExtent ( )
93
+ {
94
+ Text = typeDefinitionAst . Name ,
95
+ StartLineNumber = typeDefinitionAst . Extent . StartLineNumber ,
96
+ EndLineNumber = typeDefinitionAst . Extent . EndLineNumber ,
97
+ StartColumnNumber = typeDefinitionAst . Extent . StartColumnNumber ,
98
+ EndColumnNumber = typeDefinitionAst . Extent . EndColumnNumber ,
99
+ File = typeDefinitionAst . Extent . File
100
+ } ;
101
+
102
+ SymbolType symbolType = typeDefinitionAst switch
103
+ {
104
+ { IsEnum : true } => SymbolType . Enum ,
105
+ { IsInterface : true } => SymbolType . Interface ,
106
+ _ => SymbolType . Class ,
107
+ } ;
108
+
109
+ SymbolReferences . Add (
110
+ new SymbolReference (
111
+ symbolType ,
112
+ nameExtent ) ) ;
113
+
114
+ return AstVisitAction . Continue ;
115
+ }
116
+
117
+ /// <summary>
118
+ /// Adds class method and constructor AST to symbol reference list
119
+ /// </summary>
120
+ public override AstVisitAction VisitFunctionMember ( FunctionMemberAst functionMemberAst )
121
+ {
122
+ IScriptExtent nameExtent = new ScriptExtent ( )
123
+ {
124
+ Text = GetMethodOverloadName ( functionMemberAst ) ,
125
+ StartLineNumber = functionMemberAst . Extent . StartLineNumber ,
126
+ EndLineNumber = functionMemberAst . Extent . EndLineNumber ,
127
+ StartColumnNumber = functionMemberAst . Extent . StartColumnNumber ,
128
+ EndColumnNumber = functionMemberAst . Extent . EndColumnNumber ,
129
+ File = functionMemberAst . Extent . File
130
+ } ;
131
+
132
+ SymbolType symbolType =
133
+ functionMemberAst . IsConstructor ?
134
+ SymbolType . Constructor : SymbolType . Method ;
135
+
136
+ SymbolReferences . Add (
137
+ new SymbolReference (
138
+ symbolType ,
139
+ nameExtent ) ) ;
140
+
141
+ return AstVisitAction . Continue ;
142
+ }
143
+
144
+ /// <summary>
145
+ /// Gets the method or constructor name with parameters for current overload.
146
+ /// </summary>
147
+ /// <param name="functionMemberAst">A FunctionMemberAst object in the script's AST</param>
148
+ /// <returns>Function member name with parameter types and names</returns>
149
+ private static string GetMethodOverloadName ( FunctionMemberAst functionMemberAst ) {
150
+ if ( functionMemberAst . Parameters . Count > 0 )
151
+ {
152
+ List < string > parameters = new ( functionMemberAst . Parameters . Count ) ;
153
+ foreach ( ParameterAst param in functionMemberAst . Parameters )
154
+ {
155
+ parameters . Add ( param . Extent . Text ) ;
156
+ }
157
+
158
+ string paramString = string . Join ( ", " , parameters ) ;
159
+ return string . Concat ( functionMemberAst . Name , "(" , paramString , ")" ) ;
160
+ }
161
+ else
162
+ {
163
+ return string . Concat ( functionMemberAst . Name , "()" ) ;
164
+ }
165
+ }
166
+
167
+ /// <summary>
168
+ /// Adds class property AST to symbol reference list
169
+ /// </summary>
170
+ public override AstVisitAction VisitPropertyMember ( PropertyMemberAst propertyMemberAst )
171
+ {
172
+ IScriptExtent nameExtent = new ScriptExtent ( )
173
+ {
174
+ Text = propertyMemberAst . Name ,
175
+ StartLineNumber = propertyMemberAst . Extent . StartLineNumber ,
176
+ EndLineNumber = propertyMemberAst . Extent . EndLineNumber ,
177
+ StartColumnNumber = propertyMemberAst . Extent . StartColumnNumber ,
178
+ EndColumnNumber = propertyMemberAst . Extent . EndColumnNumber ,
179
+ File = propertyMemberAst . Extent . File
180
+ } ;
181
+
182
+ SymbolReferences . Add (
183
+ new SymbolReference (
184
+ SymbolType . Property ,
185
+ nameExtent ) ) ;
186
+
187
+ return AstVisitAction . Continue ;
188
+ }
189
+
190
+ /// <summary>
191
+ /// Adds DSC configuration AST to symbol reference list
192
+ /// </summary>
193
+ public override AstVisitAction VisitConfigurationDefinition ( ConfigurationDefinitionAst configurationDefinitionAst )
194
+ {
195
+ IScriptExtent nameExtent = new ScriptExtent ( )
196
+ {
197
+ Text = configurationDefinitionAst . InstanceName . Extent . Text ,
198
+ StartLineNumber = configurationDefinitionAst . Extent . StartLineNumber ,
199
+ EndLineNumber = configurationDefinitionAst . Extent . EndLineNumber ,
200
+ StartColumnNumber = configurationDefinitionAst . Extent . StartColumnNumber ,
201
+ EndColumnNumber = configurationDefinitionAst . Extent . EndColumnNumber ,
202
+ File = configurationDefinitionAst . Extent . File
203
+ } ;
204
+
205
+ SymbolReferences . Add (
206
+ new SymbolReference (
207
+ SymbolType . Configuration ,
208
+ nameExtent ) ) ;
209
+
210
+ return AstVisitAction . Continue ;
211
+ }
83
212
}
84
213
85
214
/// <summary>
0 commit comments