Skip to content

Commit 57c563b

Browse files
committed
Add DisplayName to clean up outline
1 parent 6b093da commit 57c563b

File tree

11 files changed

+104
-102
lines changed

11 files changed

+104
-102
lines changed

src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ internal record PesterSymbolReference : SymbolReference
214214

215215
/// <summary>
216216
/// Gets the name of the test
217+
/// TODO: We could get rid of this and use DisplayName now, but first attempt didn't work great.
217218
/// </summary>
218219
public string TestName { get; }
219220

@@ -231,6 +232,7 @@ internal PesterSymbolReference(
231232
: base(
232233
SymbolType.Function,
233234
symbolName,
235+
symbolName + " { }",
234236
scriptExtent,
235237
scriptExtent,
236238
scriptFile,

src/PowerShellEditorServices/Services/Symbols/SymbolDetails.cs

Lines changed: 19 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Microsoft.PowerShell.EditorServices.Services.Symbols
1212
{
1313
/// <summary>
1414
/// Provides detailed information for a given symbol.
15+
/// TODO: Get rid of this and just use return documentation.
1516
/// </summary>
1617
[DebuggerDisplay("SymbolReference = {SymbolReference.SymbolType}/{SymbolReference.SymbolName}, DisplayString = {DisplayString}")]
1718
internal class SymbolDetails
@@ -23,11 +24,6 @@ internal class SymbolDetails
2324
/// </summary>
2425
public SymbolReference SymbolReference { get; private set; }
2526

26-
/// <summary>
27-
/// Gets the display string for this symbol.
28-
/// </summary>
29-
public string DisplayString { get; private set; }
30-
3127
/// <summary>
3228
/// Gets the documentation string for this symbol. Returns an
3329
/// empty string if the symbol has no documentation.
@@ -48,66 +44,28 @@ internal static async Task<SymbolDetails> CreateAsync(
4844
SymbolReference = symbolReference
4945
};
5046

51-
switch (symbolReference.SymbolType)
47+
if (symbolReference.SymbolType is SymbolType.Function)
5248
{
53-
case SymbolType.Function:
54-
CommandInfo commandInfo = await CommandHelpers.GetCommandInfoAsync(
55-
symbolReference.SymbolName,
56-
currentRunspace,
57-
executionService).ConfigureAwait(false);
58-
59-
if (commandInfo != null)
49+
CommandInfo commandInfo = await CommandHelpers.GetCommandInfoAsync(
50+
symbolReference.SymbolName,
51+
currentRunspace,
52+
executionService).ConfigureAwait(false);
53+
54+
if (commandInfo is not null)
55+
{
56+
symbolDetails.Documentation =
57+
await CommandHelpers.GetCommandSynopsisAsync(
58+
commandInfo,
59+
executionService).ConfigureAwait(false);
60+
61+
if (commandInfo.CommandType == CommandTypes.Application)
6062
{
61-
symbolDetails.Documentation =
62-
await CommandHelpers.GetCommandSynopsisAsync(
63-
commandInfo,
64-
executionService).ConfigureAwait(false);
65-
66-
if (commandInfo.CommandType == CommandTypes.Application)
67-
{
68-
symbolDetails.DisplayString = "(application) " + symbolReference.SymbolName;
69-
return symbolDetails;
70-
}
63+
symbolDetails.SymbolReference = symbolReference with { DisplayString = $"(application) ${symbolReference.DisplayString}" };
7164
}
72-
73-
symbolDetails.DisplayString = "function " + symbolReference.SymbolName;
74-
return symbolDetails;
75-
76-
case SymbolType.Parameter:
77-
// TODO: Get parameter help
78-
symbolDetails.DisplayString = "(parameter) " + symbolReference.SymbolName;
79-
return symbolDetails;
80-
81-
case SymbolType.Variable:
82-
symbolDetails.DisplayString = symbolReference.SymbolName;
83-
return symbolDetails;
84-
85-
case SymbolType.Class:
86-
symbolDetails.DisplayString = "class " + symbolReference.SymbolName;
87-
return symbolDetails;
88-
89-
case SymbolType.Enum:
90-
symbolDetails.DisplayString = "enum " + symbolReference.SymbolName;
91-
return symbolDetails;
92-
93-
case SymbolType.Type:
94-
symbolDetails.DisplayString = "type " + symbolReference.SymbolName;
95-
return symbolDetails;
96-
97-
case SymbolType.Constructor:
98-
case SymbolType.Method:
99-
case SymbolType.EnumMember:
100-
case SymbolType.Property:
101-
symbolDetails.DisplayString = symbolReference.SymbolName;
102-
return symbolDetails;
103-
104-
case SymbolType.Configuration:
105-
symbolDetails.DisplayString = "configuration " + symbolReference.SymbolName;
106-
return symbolDetails;
107-
108-
default:
109-
return symbolDetails;
65+
}
11066
}
67+
68+
return symbolDetails;
11169
}
11270

11371
#endregion

src/PowerShellEditorServices/Services/Symbols/SymbolReference.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ namespace Microsoft.PowerShell.EditorServices.Services.Symbols
1515
[DebuggerDisplay("SymbolType = {SymbolType}, SymbolName = {SymbolName}")]
1616
internal record SymbolReference
1717
{
18-
public SymbolType SymbolType { get; }
18+
public SymbolType SymbolType { get; init; }
1919

20-
// TODO: Have a symbol name and a separate display name, the first minimally the text so the
21-
// buckets work, the second usually a more complete signature for e.g. outline view.
22-
public string SymbolName { get; }
20+
public string SymbolName { get; init; }
2321

24-
public ScriptRegion NameRegion { get; }
22+
public string DisplayString { get; init; }
2523

26-
public ScriptRegion ScriptRegion { get; }
24+
public ScriptRegion NameRegion { get; init; }
25+
26+
public ScriptRegion ScriptRegion { get; init; }
2727

2828
public string SourceLine { get; internal set; }
2929

3030
public string FilePath { get; internal set; }
3131

32-
public bool IsDeclaration { get; }
32+
public bool IsDeclaration { get; init; }
3333

3434
/// <summary>
3535
/// Constructs and instance of a SymbolReference
@@ -43,13 +43,15 @@ internal record SymbolReference
4343
public SymbolReference(
4444
SymbolType symbolType,
4545
string symbolName,
46+
string displayString,
4647
IScriptExtent nameExtent,
4748
IScriptExtent scriptExtent,
4849
ScriptFile file,
4950
bool isDeclaration)
5051
{
5152
SymbolType = symbolType;
5253
SymbolName = symbolName;
54+
DisplayString = displayString;
5355
NameRegion = new(nameExtent);
5456
ScriptRegion = new(scriptExtent);
5557
FilePath = file.FilePath;

src/PowerShellEditorServices/Services/Symbols/SymbolType.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,5 @@ internal static SymbolKind GetSymbolKind(SymbolType symbolType)
9999
_ => SymbolKind.Variable,
100100
};
101101
}
102-
103-
internal static string GetDecoratedSymbolName(SymbolReference symbolReference)
104-
{
105-
string name = symbolReference.SymbolName;
106-
107-
// Append { } for symbols with scriptblock
108-
// Constructors and Methods have overloaded names already
109-
if (symbolReference.SymbolType is
110-
SymbolType.Function or
111-
SymbolType.Enum or
112-
SymbolType.Class or
113-
SymbolType.Constructor or
114-
SymbolType.Method or
115-
SymbolType.Configuration or
116-
SymbolType.Workflow)
117-
{
118-
name += " { }";
119-
}
120-
121-
return name;
122-
}
123102
}
124103
}

src/PowerShellEditorServices/Services/Symbols/Visitors/HashTableVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ public override AstVisitAction VisitHashtable(HashtableAst hashtableAst)
5757
};
5858

5959
SymbolReferences.Add(
60+
// TODO: Should we fill this out better?
6061
new SymbolReference(
6162
SymbolType.HashtableKey,
6263
nameExtent.Text,
64+
nameExtent.Text,
6365
nameExtent,
64-
// TODO: Should this be more?
6566
nameExtent,
6667
_file,
67-
// TODO: Should this be true?
6868
isDeclaration: false));
6969
}
7070
}

src/PowerShellEditorServices/Services/Symbols/Visitors/SymbolVisitor.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ public SymbolVisitor(ScriptFile file, Func<SymbolReference, AstVisitAction> acti
3131
public override AstVisitAction VisitCommand(CommandAst commandAst)
3232
{
3333
string? commandName = VisitorUtils.GetCommandName(commandAst);
34-
if (string.IsNullOrEmpty(commandName))
34+
if (commandName is null)
3535
{
3636
return AstVisitAction.Continue;
3737
}
3838

3939
return _action(new SymbolReference(
4040
SymbolType.Function,
4141
CommandHelpers.StripModuleQualification(commandName, out _),
42+
commandName,
4243
commandAst.CommandElements[0].Extent,
4344
commandAst.Extent,
4445
_file,
@@ -62,6 +63,7 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
6263
return _action(new SymbolReference(
6364
symbolType,
6465
functionDefinitionAst.Name,
66+
VisitorUtils.GetFunctionDisplayName(functionDefinitionAst),
6567
nameExtent,
6668
functionDefinitionAst.Extent,
6769
_file,
@@ -70,10 +72,10 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
7072

7173
public override AstVisitAction VisitParameter(ParameterAst parameterAst)
7274
{
73-
// TODO: When we add DisplayString, include the default value.
7475
return _action(new SymbolReference(
7576
SymbolType.Parameter,
7677
$"${parameterAst.Name.VariablePath.UserPath}",
78+
VisitorUtils.GetParamDisplayName(parameterAst),
7779
parameterAst.Name.Extent,
7880
parameterAst.Extent,
7981
_file,
@@ -87,6 +89,7 @@ public override AstVisitAction VisitVariableExpression(VariableExpressionAst var
8789
return _action(new SymbolReference(
8890
SymbolType.Variable,
8991
$"${variableExpressionAst.VariablePath.UserPath}",
92+
$"${variableExpressionAst.VariablePath.UserPath}",
9093
variableExpressionAst.Extent,
9194
variableExpressionAst.Extent, // TODO: Maybe parent?
9295
_file,
@@ -103,6 +106,7 @@ public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinit
103106
return _action(new SymbolReference(
104107
symbolType,
105108
typeDefinitionAst.Name,
109+
(symbolType is SymbolType.Enum ? "enum " : "class ") + typeDefinitionAst.Name + " { }",
106110
nameExtent,
107111
typeDefinitionAst.Extent,
108112
_file,
@@ -114,6 +118,7 @@ public override AstVisitAction VisitTypeExpression(TypeExpressionAst typeExpress
114118
return _action(new SymbolReference(
115119
SymbolType.Type,
116120
typeExpressionAst.TypeName.Name,
121+
typeExpressionAst.TypeName.Name,
117122
typeExpressionAst.Extent,
118123
typeExpressionAst.Extent,
119124
_file,
@@ -125,6 +130,7 @@ public override AstVisitAction VisitTypeConstraint(TypeConstraintAst typeConstra
125130
return _action(new SymbolReference(
126131
SymbolType.Type,
127132
typeConstraintAst.TypeName.Name,
133+
"[" + typeConstraintAst.TypeName.Name + "]",
128134
typeConstraintAst.Extent,
129135
typeConstraintAst.Extent,
130136
_file,
@@ -145,6 +151,7 @@ public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMem
145151
return _action(new SymbolReference(
146152
symbolType,
147153
functionMemberAst.Name, // We bucket all the overloads.
154+
VisitorUtils.GetMemberOverloadName(functionMemberAst, false, true),
148155
nameExtent,
149156
functionMemberAst.Extent,
150157
_file,
@@ -161,6 +168,7 @@ propertyMemberAst.Parent is TypeDefinitionAst typeAst && typeAst.IsEnum
161168
return _action(new SymbolReference(
162169
symbolType,
163170
nameExtent.Text,
171+
VisitorUtils.GetMemberOverloadName(propertyMemberAst, false, true),
164172
nameExtent,
165173
propertyMemberAst.Extent,
166174
_file,
@@ -180,6 +188,7 @@ public override AstVisitAction VisitMemberExpression(MemberExpressionAst memberE
180188
#pragma warning disable CS8604 // Possible null reference argument.
181189
memberName,
182190
#pragma warning restore CS8604
191+
memberExpressionAst.Member.Extent.Text,
183192
memberExpressionAst.Member.Extent,
184193
memberExpressionAst.Extent,
185194
_file,
@@ -199,6 +208,7 @@ public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressio
199208
#pragma warning disable CS8604 // Possible null reference argument.
200209
memberName,
201210
#pragma warning restore CS8604
211+
methodCallAst.Member.Extent.Text,
202212
methodCallAst.Member.Extent,
203213
methodCallAst.Extent,
204214
_file,
@@ -211,6 +221,7 @@ public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinit
211221
return _action(new SymbolReference(
212222
SymbolType.Configuration,
213223
nameExtent.Text,
224+
"Configuration " + nameExtent.Text + " { }",
214225
nameExtent,
215226
configurationDefinitionAst.Extent,
216227
_file,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public override async Task<SymbolInformationOrDocumentSymbolContainer> Handle(Do
6969
//
7070
// TODO: We should also include function invocations that are part of DSLs (like
7171
// Invoke-Build etc.).
72-
if (!r.IsDeclaration)
72+
if (!r.IsDeclaration || r.SymbolType is SymbolType.Parameter)
7373
{
7474
continue;
7575
}
@@ -93,7 +93,7 @@ public override async Task<SymbolInformationOrDocumentSymbolContainer> Handle(Do
9393
Uri = DocumentUri.From(r.FilePath),
9494
Range = r.ScriptRegion.ToRange() // The whole thing, not just the name.
9595
},
96-
Name = SymbolTypeUtils.GetDecoratedSymbolName(r)
96+
Name = r.DisplayString
9797
}));
9898
}
9999

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ await _symbolsService.FindSymbolDetailsAtLocationAsync(
5252
request.Position.Line + 1,
5353
request.Position.Character + 1).ConfigureAwait(false);
5454

55-
if (symbolDetails == null)
55+
if (symbolDetails is null)
5656
{
5757
return null;
5858
}
5959

6060
List<MarkedString> symbolInfo = new()
6161
{
62-
new MarkedString("PowerShell", symbolDetails.DisplayString)
62+
new MarkedString("PowerShell", symbolDetails.SymbolReference.DisplayString)
6363
};
6464

6565
if (!string.IsNullOrEmpty(symbolDetails.Documentation))

src/PowerShellEditorServices/Services/Workspace/Handlers/WorkspaceSymbolsHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public override async Task<Container<SymbolInformation>> Handle(WorkspaceSymbolP
8080
ContainerName = containerName,
8181
Kind = SymbolTypeUtils.GetSymbolKind(foundOccurrence.SymbolType),
8282
Location = location,
83-
Name = SymbolTypeUtils.GetDecoratedSymbolName(foundOccurrence)
83+
Name = foundOccurrence.DisplayString
8484
});
8585
}
8686
}

0 commit comments

Comments
 (0)