Skip to content

Commit 3012586

Browse files
fflatenandyleejordan
authored andcommitted
add EnumMember, signature and fix detection
1 parent f587eed commit 3012586

File tree

10 files changed

+222
-86
lines changed

10 files changed

+222
-86
lines changed

src/PowerShellEditorServices/Services/Symbols/SymbolDetails.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,10 @@ await CommandHelpers.GetCommandSynopsisAsync(
9595
return symbolDetails;
9696

9797
case SymbolType.Constructor:
98-
// TODO: constructor Class(parameters)
99-
symbolDetails.DisplayString = "constructor " + symbolReference.SymbolName;
100-
return symbolDetails;
101-
10298
case SymbolType.Method:
103-
// TODO: method ReturnType Class.MethodName(parameters)
104-
symbolDetails.DisplayString = "method " + symbolReference.SymbolName;
105-
return symbolDetails;
106-
99+
case SymbolType.EnumMember:
107100
case SymbolType.Property:
108-
symbolDetails.DisplayString = "(property) " + symbolReference.SymbolName;
101+
symbolDetails.DisplayString = symbolReference.SymbolName;
109102
return symbolDetails;
110103

111104
case SymbolType.Configuration:

src/PowerShellEditorServices/Services/Symbols/SymbolType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ internal enum SymbolType
5353
/// </summary>
5454
Enum,
5555

56+
/// <summary>
57+
/// The symbol is a enum member/value
58+
/// </summary>
59+
EnumMember,
60+
5661
/// <summary>
5762
/// The symbol is a class property
5863
/// </summary>

src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ public Task<SymbolDetails> FindSymbolDetailsAtLocationAsync(
332332
AstOperations.FindSymbolAtPosition(
333333
scriptFile.ScriptAst,
334334
lineNumber,
335-
columnNumber);
335+
columnNumber,
336+
returnMemberSignature: true);
336337

337338
if (symbolReference == null)
338339
{

src/PowerShellEditorServices/Services/Symbols/Vistors/AstOperations.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,21 @@ await executionService.ExecuteDelegateAsync(
148148
/// <param name="lineNumber">The line number of the cursor for the given script</param>
149149
/// <param name="columnNumber">The column number of the cursor for the given script</param>
150150
/// <param name="includeDefinitions">Includes full symbol definition ranges in the search.</param>
151+
/// <param name="returnMemberSignature">Includes return type and class in symbol name.</param>
151152
/// <returns>SymbolReference of found symbol</returns>
152153
public static SymbolReference FindSymbolAtPosition(
153154
Ast scriptAst,
154155
int lineNumber,
155156
int columnNumber,
156-
bool includeDefinitions = false)
157+
bool includeDefinitions = false,
158+
bool returnMemberSignature = false)
157159
{
158160
FindSymbolVisitor symbolVisitor =
159161
new(
160162
lineNumber,
161163
columnNumber,
162-
includeDefinitions);
164+
includeDefinitions,
165+
returnMemberSignature);
163166

164167
scriptAst.Visit(symbolVisitor);
165168

src/PowerShellEditorServices/Services/Symbols/Vistors/FindDeclarationVisitor.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMem
110110
SymbolType.Constructor : SymbolType.Method;
111111

112112
if (symbolRef.SymbolType.Equals(symbolType) &&
113-
functionMemberAst.Name.Equals(symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase))
113+
VisitorUtils.GetMemberOverloadName(functionMemberAst).Equals(symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase))
114114
{
115115
// We only want the method/ctor name. Get start-location for name
116116
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(functionMemberAst);
@@ -129,15 +129,19 @@ public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMem
129129
/// <summary>
130130
/// Decides if the current property member is the right definition
131131
/// for the symbol being searched for. The definition of the symbol will be a of type
132-
/// SymbolType.Property and have the same name as the symbol
132+
/// SymbolType.Property or SymbolType.EnumMember and have the same name as the symbol
133133
/// </summary>
134134
/// <param name="propertyMemberAst">A PropertyMemberAst in the script's AST</param>
135135
/// <returns>A decision to stop searching if the right PropertyMemberAst was found,
136136
/// or a decision to continue if it wasn't found</returns>
137137
public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMemberAst)
138138
{
139-
if (symbolRef.SymbolType.Equals(SymbolType.Property) &&
140-
propertyMemberAst.Name.Equals(symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase))
139+
SymbolType symbolType =
140+
propertyMemberAst.Parent is TypeDefinitionAst typeAst && typeAst.IsEnum ?
141+
SymbolType.EnumMember : SymbolType.Property;
142+
143+
if (symbolRef.SymbolType.Equals(symbolType) &&
144+
VisitorUtils.GetMemberOverloadName(propertyMemberAst).Equals(symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase))
141145
{
142146
// We only want the property name. Get start-location for name
143147
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(propertyMemberAst);

src/PowerShellEditorServices/Services/Symbols/Vistors/FindReferencesVisitor.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMem
235235
SymbolType.Constructor : SymbolType.Method;
236236

237237
if (_symbolRef.SymbolType.Equals(symbolType) &&
238-
functionMemberAst.Name.Equals(_symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase))
238+
VisitorUtils.GetMemberOverloadName(functionMemberAst).Equals(_symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase))
239239
{
240240
// We only want the method/ctor name. Get start-location for name
241241
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(functionMemberAst);
@@ -246,14 +246,19 @@ public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMem
246246

247247
/// <summary>
248248
/// Decides if the current property member is a reference of the symbol being searched for.
249-
/// A reference of the symbol will be a of type SymbolType.Property and have the same name as the symbol
249+
/// A reference of the symbol will be a of type SymbolType.Property or SymbolType.EnumMember
250+
/// and have the same name as the symbol.
250251
/// </summary>
251252
/// <param name="propertyMemberAst">A PropertyMemberAst in the script's AST</param>
252253
/// <returns>A visit action that continues the search for references</returns>
253254
public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMemberAst)
254255
{
255-
if (_symbolRef.SymbolType.Equals(SymbolType.Property) &&
256-
propertyMemberAst.Name.Equals(_symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase))
256+
SymbolType symbolType =
257+
propertyMemberAst.Parent is TypeDefinitionAst typeAst && typeAst.IsEnum ?
258+
SymbolType.EnumMember : SymbolType.Property;
259+
260+
if (_symbolRef.SymbolType.Equals(symbolType) &&
261+
VisitorUtils.GetMemberOverloadName(propertyMemberAst).Equals(_symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase))
257262
{
258263
// We only want the property name. Get start-location for name
259264
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(propertyMemberAst);

src/PowerShellEditorServices/Services/Symbols/Vistors/FindSymbolVisitor.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

44
using System.Management.Automation.Language;
@@ -14,17 +14,20 @@ internal class FindSymbolVisitor : AstVisitor2
1414
private readonly int lineNumber;
1515
private readonly int columnNumber;
1616
private readonly bool includeDefinitions;
17+
private readonly bool returnMemberSignature;
1718

1819
public SymbolReference FoundSymbolReference { get; private set; }
1920

2021
public FindSymbolVisitor(
2122
int lineNumber,
2223
int columnNumber,
23-
bool includeDefinitions)
24+
bool includeDefinitions,
25+
bool returnMemberSignature)
2426
{
2527
this.lineNumber = lineNumber;
2628
this.columnNumber = columnNumber;
2729
this.includeDefinitions = includeDefinitions;
30+
this.returnMemberSignature = returnMemberSignature;
2831
}
2932

3033
/// <summary>
@@ -161,7 +164,7 @@ private bool IsPositionInExtent(IScriptExtent extent)
161164
public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMemberAst)
162165
{
163166
// We only want the method/ctor name. Get start-location for name
164-
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(functionMemberAst);
167+
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(functionMemberAst, returnMemberSignature);
165168

166169
if (IsPositionInExtent(nameExtent))
167170
{
@@ -265,8 +268,11 @@ public override AstVisitAction VisitTypeExpression(TypeExpressionAst typeExpress
265268
/// or a decision to continue if it wasn't found</returns>
266269
public override AstVisitAction VisitTypeConstraint(TypeConstraintAst typeConstraintAst)
267270
{
268-
// Show only type name (skip leading '['). Offset by StartColumn to include indentation etc.
269-
int startColumnNumber = typeConstraintAst.Extent.StartColumnNumber + 1;
271+
// Show only type name (skip leading '[' if present). It's not present for inherited types
272+
// Offset by StartColumn to include indentation etc.
273+
int startColumnNumber =
274+
typeConstraintAst.Extent.Text[0] == '[' ?
275+
typeConstraintAst.Extent.StartColumnNumber + 1 : typeConstraintAst.Extent.StartColumnNumber;
270276

271277
IScriptExtent nameExtent = new ScriptExtent()
272278
{
@@ -322,13 +328,17 @@ public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinit
322328
public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMemberAst)
323329
{
324330
// We only want the property name. Get start-location for name
325-
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(propertyMemberAst);
331+
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(propertyMemberAst, returnMemberSignature);
326332

327333
if (IsPositionInExtent(nameExtent))
328334
{
335+
SymbolType symbolType =
336+
propertyMemberAst.Parent is TypeDefinitionAst typeAst && typeAst.IsEnum ?
337+
SymbolType.EnumMember : SymbolType.Property;
338+
329339
FoundSymbolReference =
330340
new SymbolReference(
331-
SymbolType.Property,
341+
symbolType,
332342
nameExtent);
333343

334344
return AstVisitAction.StopVisit;

src/PowerShellEditorServices/Services/Symbols/Vistors/FindSymbolsVisitor.cs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using Microsoft.PowerShell.EditorServices.Utility;
45
using System.Collections.Generic;
56
using System.Management.Automation.Language;
67

@@ -29,7 +30,8 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
2930
return AstVisitAction.Continue;
3031
}
3132

32-
IScriptExtent nameExtent = GetNewExtent(functionDefinitionAst, functionDefinitionAst.Name);
33+
(int startColumn, int startLine) = VisitorUtils.GetNameStartColumnAndLineFromAst(functionDefinitionAst);
34+
IScriptExtent nameExtent = GetNewExtent(functionDefinitionAst, functionDefinitionAst.Name, startLine, startColumn);
3335

3436
SymbolType symbolType =
3537
functionDefinitionAst.IsWorkflow ?
@@ -82,7 +84,8 @@ private static bool IsAssignedAtScriptScope(VariableExpressionAst variableExpres
8284
/// <returns>A visit action that continues the search for references</returns>
8385
public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinitionAst)
8486
{
85-
IScriptExtent nameExtent = GetNewExtent(typeDefinitionAst, typeDefinitionAst.Name);
87+
(int startColumn, int startLine) = VisitorUtils.GetNameStartColumnAndLineFromAst(typeDefinitionAst);
88+
IScriptExtent nameExtent = GetNewExtent(typeDefinitionAst, typeDefinitionAst.Name, startLine, startColumn);
8689

8790
SymbolType symbolType =
8891
typeDefinitionAst.IsEnum ?
@@ -103,7 +106,8 @@ public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinit
103106
/// <returns>A visit action that continues the search for references</returns>
104107
public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMemberAst)
105108
{
106-
IScriptExtent nameExtent = GetNewExtent(functionMemberAst, GetMethodOverloadName(functionMemberAst));
109+
(int startColumn, int startLine) = VisitorUtils.GetNameStartColumnAndLineFromAst(functionMemberAst);
110+
IScriptExtent nameExtent = GetNewExtent(functionMemberAst, VisitorUtils.GetMemberOverloadName(functionMemberAst), startLine, startColumn);
107111

108112
SymbolType symbolType =
109113
functionMemberAst.IsConstructor ?
@@ -117,42 +121,24 @@ public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMem
117121
return AstVisitAction.Continue;
118122
}
119123

120-
/// <summary>
121-
/// Gets the method or constructor name with parameters for current overload.
122-
/// </summary>
123-
/// <param name="functionMemberAst">A FunctionMemberAst object in the script's AST</param>
124-
/// <returns>Function member name with parameter types and names</returns>
125-
private static string GetMethodOverloadName(FunctionMemberAst functionMemberAst)
126-
{
127-
if (functionMemberAst.Parameters.Count > 0)
128-
{
129-
List<string> parameters = new(functionMemberAst.Parameters.Count);
130-
foreach (ParameterAst param in functionMemberAst.Parameters)
131-
{
132-
parameters.Add(param.Extent.Text);
133-
}
134-
135-
string paramString = string.Join(", ", parameters);
136-
return string.Concat(functionMemberAst.Name, "(", paramString, ")");
137-
}
138-
else
139-
{
140-
return string.Concat(functionMemberAst.Name, "()");
141-
}
142-
}
143-
144124
/// <summary>
145125
/// Adds class property AST to symbol reference list
146126
/// </summary>
147127
/// <param name="propertyMemberAst">A PropertyMemberAst in the script's AST</param>
148128
/// <returns>A visit action that continues the search for references</returns>
149129
public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMemberAst)
150130
{
151-
IScriptExtent nameExtent = GetNewExtent(propertyMemberAst, propertyMemberAst.Name);
131+
SymbolType symbolType =
132+
propertyMemberAst.Parent is TypeDefinitionAst typeAst && typeAst.IsEnum ?
133+
SymbolType.EnumMember : SymbolType.Property;
134+
135+
bool isEnumMember = symbolType.Equals(SymbolType.EnumMember);
136+
(int startColumn, int startLine) = VisitorUtils.GetNameStartColumnAndLineFromAst(propertyMemberAst, isEnumMember);
137+
IScriptExtent nameExtent = GetNewExtent(propertyMemberAst, propertyMemberAst.Name, startLine, startColumn);
152138

153139
SymbolReferences.Add(
154140
new SymbolReference(
155-
SymbolType.Property,
141+
symbolType,
156142
nameExtent));
157143

158144
return AstVisitAction.Continue;
@@ -165,7 +151,8 @@ public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMem
165151
/// <returns>A visit action that continues the search for references</returns>
166152
public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinitionAst configurationDefinitionAst)
167153
{
168-
IScriptExtent nameExtent = GetNewExtent(configurationDefinitionAst, configurationDefinitionAst.InstanceName.Extent.Text);
154+
(int startColumn, int startLine) = VisitorUtils.GetNameStartColumnAndLineFromAst(configurationDefinitionAst);
155+
IScriptExtent nameExtent = GetNewExtent(configurationDefinitionAst, configurationDefinitionAst.InstanceName.Extent.Text, startLine, startColumn);
169156

170157
SymbolReferences.Add(
171158
new SymbolReference(
@@ -178,14 +165,14 @@ public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinit
178165
/// <summary>
179166
/// Gets a new ScriptExtent for a given Ast with same range but modified Text
180167
/// </summary>
181-
private static ScriptExtent GetNewExtent(Ast ast, string text)
168+
private static ScriptExtent GetNewExtent(Ast ast, string text, int startLine, int startColumn)
182169
{
183170
return new ScriptExtent()
184171
{
185172
Text = text,
186-
StartLineNumber = ast.Extent.StartLineNumber,
173+
StartLineNumber = startLine,
187174
EndLineNumber = ast.Extent.EndLineNumber,
188-
StartColumnNumber = ast.Extent.StartColumnNumber,
175+
StartColumnNumber = startColumn,
189176
EndColumnNumber = ast.Extent.EndColumnNumber,
190177
File = ast.Extent.File
191178
};

src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ private static SymbolKind GetSymbolKind(SymbolType symbolType)
134134
SymbolType.Constructor => SymbolKind.Constructor,
135135
SymbolType.Method => SymbolKind.Method,
136136
SymbolType.Property => SymbolKind.Property,
137+
SymbolType.EnumMember => SymbolKind.EnumMember,
137138
_ => SymbolKind.Variable,
138139
};
139140
}

0 commit comments

Comments
 (0)