|
3 | 3 |
|
4 | 4 | #nullable enable |
5 | 5 |
|
6 | | -using Microsoft.PowerShell.EditorServices.Utility; |
7 | 6 | using System.Collections.Generic; |
8 | 7 | using System.Management.Automation.Language; |
9 | 8 |
|
10 | 9 | namespace Microsoft.PowerShell.EditorServices.Services.Symbols |
11 | 10 | { |
12 | | - /// <summary> |
13 | | - /// The visitor used to find all the symbols (variables, functions and class defs etc) in the AST. |
14 | | - /// </summary> |
15 | | - internal class FindSymbolsVisitor : AstVisitor2 |
16 | | - { |
17 | | - public List<SymbolReference> SymbolReferences { get; } |
18 | | - |
19 | | - public FindSymbolsVisitor() => SymbolReferences = new List<SymbolReference>(); |
20 | | - |
21 | | - /// <summary> |
22 | | - /// Adds each function definition to symbol reference list |
23 | | - /// </summary> |
24 | | - /// <param name="functionDefinitionAst">A FunctionDefinitionAst in the script's AST</param> |
25 | | - /// <returns>A visit action that continues the search for references</returns> |
26 | | - public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst) |
27 | | - { |
28 | | - // Extent for constructors and method trigger both this and VisitFunctionMember(). Covered in the latter. |
29 | | - // This will not exclude nested functions as they have ScriptBlockAst as parent |
30 | | - if (functionDefinitionAst.Parent is FunctionMemberAst) |
31 | | - { |
32 | | - return AstVisitAction.Continue; |
33 | | - } |
34 | | - |
35 | | - (int startColumn, int startLine) = VisitorUtils.GetNameStartColumnAndLineFromAst(functionDefinitionAst); |
36 | | - IScriptExtent nameExtent = GetNewExtent(functionDefinitionAst, functionDefinitionAst.Name, startLine, startColumn); |
37 | | - |
38 | | - SymbolType symbolType = |
39 | | - functionDefinitionAst.IsWorkflow ? |
40 | | - SymbolType.Workflow : SymbolType.Function; |
41 | | - |
42 | | - SymbolReferences.Add( |
43 | | - new SymbolReference( |
44 | | - symbolType, |
45 | | - nameExtent)); |
46 | | - |
47 | | - return AstVisitAction.Continue; |
48 | | - } |
49 | | - |
50 | | - /// <summary> |
51 | | - /// Adds each script scoped variable assignment to symbol reference list |
52 | | - /// </summary> |
53 | | - /// <param name="variableExpressionAst">A VariableExpressionAst in the script's AST</param> |
54 | | - /// <returns>A visit action that continues the search for references</returns> |
55 | | - public override AstVisitAction VisitVariableExpression(VariableExpressionAst variableExpressionAst) |
56 | | - { |
57 | | - if (!IsAssignedAtScriptScope(variableExpressionAst)) |
58 | | - { |
59 | | - return AstVisitAction.Continue; |
60 | | - } |
61 | | - |
62 | | - SymbolReferences.Add( |
63 | | - new SymbolReference( |
64 | | - SymbolType.Variable, |
65 | | - variableExpressionAst.Extent)); |
66 | | - |
67 | | - return AstVisitAction.Continue; |
68 | | - } |
69 | | - |
70 | | - private static bool IsAssignedAtScriptScope(VariableExpressionAst variableExpressionAst) |
71 | | - { |
72 | | - Ast parent = variableExpressionAst.Parent; |
73 | | - if (parent is not AssignmentStatementAst) |
74 | | - { |
75 | | - return false; |
76 | | - } |
77 | | - |
78 | | - parent = parent.Parent; |
79 | | - return parent is null || parent.Parent is null || parent.Parent.Parent is null; |
80 | | - } |
81 | | - |
82 | | - /// <summary> |
83 | | - /// Adds class and enum AST to symbol reference list |
84 | | - /// </summary> |
85 | | - /// <param name="typeDefinitionAst">A TypeDefinitionAst in the script's AST</param> |
86 | | - /// <returns>A visit action that continues the search for references</returns> |
87 | | - public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinitionAst) |
88 | | - { |
89 | | - (int startColumn, int startLine) = VisitorUtils.GetNameStartColumnAndLineFromAst(typeDefinitionAst); |
90 | | - IScriptExtent nameExtent = GetNewExtent(typeDefinitionAst, typeDefinitionAst.Name, startLine, startColumn); |
91 | | - |
92 | | - SymbolType symbolType = |
93 | | - typeDefinitionAst.IsEnum ? |
94 | | - SymbolType.Enum : SymbolType.Class; |
95 | | - |
96 | | - SymbolReferences.Add( |
97 | | - new SymbolReference( |
98 | | - symbolType, |
99 | | - nameExtent)); |
100 | | - |
101 | | - return AstVisitAction.Continue; |
102 | | - } |
103 | | - |
104 | | - /// <summary> |
105 | | - /// Adds class method and constructor AST to symbol reference list |
106 | | - /// </summary> |
107 | | - /// <param name="functionMemberAst">A FunctionMemberAst in the script's AST</param> |
108 | | - /// <returns>A visit action that continues the search for references</returns> |
109 | | - public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMemberAst) |
110 | | - { |
111 | | - (int startColumn, int startLine) = VisitorUtils.GetNameStartColumnAndLineFromAst(functionMemberAst); |
112 | | - IScriptExtent nameExtent = GetNewExtent(functionMemberAst, VisitorUtils.GetMemberOverloadName(functionMemberAst, false, false), startLine, startColumn); |
113 | | - |
114 | | - SymbolType symbolType = |
115 | | - functionMemberAst.IsConstructor ? |
116 | | - SymbolType.Constructor : SymbolType.Method; |
117 | | - |
118 | | - SymbolReferences.Add( |
119 | | - new SymbolReference( |
120 | | - symbolType, |
121 | | - nameExtent)); |
122 | | - |
123 | | - return AstVisitAction.Continue; |
124 | | - } |
125 | | - |
126 | | - /// <summary> |
127 | | - /// Adds class property AST to symbol reference list |
128 | | - /// </summary> |
129 | | - /// <param name="propertyMemberAst">A PropertyMemberAst in the script's AST</param> |
130 | | - /// <returns>A visit action that continues the search for references</returns> |
131 | | - public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMemberAst) |
132 | | - { |
133 | | - SymbolType symbolType = |
134 | | - propertyMemberAst.Parent is TypeDefinitionAst typeAst && typeAst.IsEnum ? |
135 | | - SymbolType.EnumMember : SymbolType.Property; |
136 | | - |
137 | | - bool isEnumMember = symbolType.Equals(SymbolType.EnumMember); |
138 | | - (int startColumn, int startLine) = VisitorUtils.GetNameStartColumnAndLineFromAst(propertyMemberAst, isEnumMember); |
139 | | - IScriptExtent nameExtent = GetNewExtent(propertyMemberAst, propertyMemberAst.Name, startLine, startColumn); |
140 | | - |
141 | | - SymbolReferences.Add( |
142 | | - new SymbolReference( |
143 | | - symbolType, |
144 | | - nameExtent)); |
145 | | - |
146 | | - return AstVisitAction.Continue; |
147 | | - } |
148 | | - |
149 | | - /// <summary> |
150 | | - /// Adds DSC configuration AST to symbol reference list |
151 | | - /// </summary> |
152 | | - /// <param name="configurationDefinitionAst">A ConfigurationDefinitionAst in the script's AST</param> |
153 | | - /// <returns>A visit action that continues the search for references</returns> |
154 | | - public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinitionAst configurationDefinitionAst) |
155 | | - { |
156 | | - (int startColumn, int startLine) = VisitorUtils.GetNameStartColumnAndLineFromAst(configurationDefinitionAst); |
157 | | - IScriptExtent nameExtent = GetNewExtent(configurationDefinitionAst, configurationDefinitionAst.InstanceName.Extent.Text, startLine, startColumn); |
158 | | - |
159 | | - SymbolReferences.Add( |
160 | | - new SymbolReference( |
161 | | - SymbolType.Configuration, |
162 | | - nameExtent)); |
163 | | - |
164 | | - return AstVisitAction.Continue; |
165 | | - } |
166 | | - |
167 | | - /// <summary> |
168 | | - /// Gets a new ScriptExtent for a given Ast with same range but modified Text |
169 | | - /// </summary> |
170 | | - private static ScriptExtent GetNewExtent(Ast ast, string text, int startLine, int startColumn) |
171 | | - { |
172 | | - return new ScriptExtent() |
173 | | - { |
174 | | - Text = text, |
175 | | - StartLineNumber = startLine, |
176 | | - EndLineNumber = ast.Extent.EndLineNumber, |
177 | | - StartColumnNumber = startColumn, |
178 | | - EndColumnNumber = ast.Extent.EndColumnNumber, |
179 | | - File = ast.Extent.File |
180 | | - }; |
181 | | - } |
182 | | - } |
183 | | - |
184 | 11 | /// <summary> |
185 | 12 | /// Visitor to find all the keys in Hashtable AST |
186 | 13 | /// </summary> |
|
0 commit comments