Skip to content

Commit 8c2c9c4

Browse files
committed
Clean up SymbolReference constructors
1 parent e5cf5bd commit 8c2c9c4

File tree

5 files changed

+32
-49
lines changed

5 files changed

+32
-49
lines changed

src/PowerShellEditorServices/Services/CodeLens/PesterCodeLensProvider.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public PesterCodeLensProvider(ConfigurationService configurationService)
4646
private static CodeLens[] GetPesterLens(PesterSymbolReference pesterSymbol, ScriptFile scriptFile)
4747
{
4848
string word = pesterSymbol.Command == PesterCommandType.It ? "test" : "tests";
49-
CodeLens[] codeLensResults = new CodeLens[]
49+
return new CodeLens[]
5050
{
5151
new CodeLens()
5252
{
@@ -91,8 +91,6 @@ private static CodeLens[] GetPesterLens(PesterSymbolReference pesterSymbol, Scri
9191
}
9292
}
9393
};
94-
95-
return codeLensResults;
9694
}
9795

9896
/// <summary>

src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ private static bool IsPesterCommand(CommandAst commandAst)
7575
return true;
7676
}
7777

78+
private static readonly char[] DefinitionTrimChars = new char[] { ' ', '{' };
79+
7880
/// <summary>
7981
/// Convert a CommandAst known to represent a Pester command and a reference to the scriptfile
8082
/// it is in into symbol representing a Pester call for code lens
@@ -84,7 +86,11 @@ private static bool IsPesterCommand(CommandAst commandAst)
8486
/// <returns>a symbol representing the Pester call containing metadata for CodeLens to use</returns>
8587
private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFile scriptFile, CommandAst pesterCommandAst)
8688
{
87-
string testLine = scriptFile.GetLine(pesterCommandAst.Extent.StartLineNumber);
89+
string symbolName = scriptFile
90+
.GetLine(pesterCommandAst.Extent.StartLineNumber)
91+
.TrimStart()
92+
.TrimEnd(DefinitionTrimChars);
93+
8894
PesterCommandType? commandName = PesterSymbolReference.GetCommandType(pesterCommandAst.GetCommandName());
8995
if (commandName == null)
9096
{
@@ -126,7 +132,7 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil
126132
return new PesterSymbolReference(
127133
scriptFile,
128134
commandName.Value,
129-
testLine,
135+
symbolName,
130136
testName,
131137
pesterCommandAst.Extent
132138
);
@@ -206,8 +212,6 @@ internal record PesterSymbolReference : SymbolReference
206212
.Cast<PesterCommandType>()
207213
.ToDictionary(pct => pct.ToString(), pct => pct, StringComparer.OrdinalIgnoreCase);
208214

209-
private static readonly char[] DefinitionTrimChars = new char[] { ' ', '{' };
210-
211215
/// <summary>
212216
/// Gets the name of the test
213217
/// </summary>
@@ -221,15 +225,15 @@ internal record PesterSymbolReference : SymbolReference
221225
internal PesterSymbolReference(
222226
ScriptFile scriptFile,
223227
PesterCommandType commandType,
224-
string testLine,
228+
string symbolName,
225229
string testName,
226230
IScriptExtent scriptExtent)
227231
: base(
228232
SymbolType.Function,
229-
testLine.TrimStart().TrimEnd(DefinitionTrimChars),
233+
symbolName,
234+
scriptExtent,
230235
scriptExtent,
231-
scriptFile.FilePath,
232-
testLine,
236+
scriptFile,
233237
isDeclaration: true)
234238
{
235239
Command = commandType;

src/PowerShellEditorServices/Services/Symbols/PsdDocumentSymbolProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ IEnumerable<SymbolReference> IDocumentSymbolProvider.ProvideDocumentSymbols(
2323
if ((scriptFile.FilePath?.EndsWith(".psd1", StringComparison.OrdinalIgnoreCase) == true) ||
2424
IsPowerShellDataFileAst(scriptFile.ScriptAst))
2525
{
26-
FindHashtableSymbolsVisitor findHashtableSymbolsVisitor = new();
26+
FindHashtableSymbolsVisitor findHashtableSymbolsVisitor = new(scriptFile);
2727
scriptFile.ScriptAst.Visit(findHashtableSymbolsVisitor);
2828
return findHashtableSymbolsVisitor.SymbolReferences;
2929
}

src/PowerShellEditorServices/Services/Symbols/SymbolReference.cs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,13 @@ internal record SymbolReference
3333

3434
/// <summary>
3535
/// Constructs and instance of a SymbolReference
36-
/// TODO: Remove this when it becomes unused.
3736
/// </summary>
3837
/// <param name="symbolType">The higher level type of the symbol</param>
3938
/// <param name="symbolName">The name of the symbol</param>
39+
/// <param name="nameExtent">The extent of the symbol's name</param>
4040
/// <param name="scriptExtent">The script extent of the symbol</param>
41-
/// <param name="filePath">The file path of the symbol</param>
42-
/// <param name="sourceLine">The line contents of the given symbol (defaults to empty string)</param>
41+
/// <param name="file">The script file that has the symbol</param>
4342
/// <param name="isDeclaration">True if this reference is the definition of the symbol</param>
44-
public SymbolReference(
45-
SymbolType symbolType,
46-
string symbolName,
47-
IScriptExtent scriptExtent,
48-
string filePath = "",
49-
string sourceLine = "",
50-
bool isDeclaration = false)
51-
{
52-
// TODO: Verify params
53-
SymbolType = symbolType;
54-
SymbolName = symbolName;
55-
ScriptRegion = new(scriptExtent);
56-
NameRegion = ScriptRegion;
57-
FilePath = string.IsNullOrEmpty(filePath) ? scriptExtent.File : filePath;
58-
SourceLine = sourceLine;
59-
IsDeclaration = isDeclaration;
60-
}
61-
6243
public SymbolReference(
6344
SymbolType symbolType,
6445
string symbolName,
@@ -82,16 +63,5 @@ public SymbolReference(
8263
}
8364
IsDeclaration = isDeclaration;
8465
}
85-
86-
/// <summary>
87-
/// Constructs an instance of a SymbolReference.
88-
/// TODO: Remove this when it becomes unused.
89-
/// </summary>
90-
/// <param name="symbolType">The higher level type of the symbol</param>
91-
/// <param name="scriptExtent">The script extent of the symbol</param>
92-
public SymbolReference(SymbolType symbolType, IScriptExtent scriptExtent)
93-
: this(symbolType, scriptExtent.Text, scriptExtent, scriptExtent.File)
94-
{
95-
}
9666
}
9767
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System.Collections.Generic;
77
using System.Management.Automation.Language;
8+
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
89

910
namespace Microsoft.PowerShell.EditorServices.Services.Symbols
1011
{
@@ -13,6 +14,8 @@ namespace Microsoft.PowerShell.EditorServices.Services.Symbols
1314
/// </summary>
1415
internal class FindHashtableSymbolsVisitor : AstVisitor
1516
{
17+
private readonly ScriptFile _file;
18+
1619
/// <summary>
1720
/// List of symbols (keys) found in the hashtable
1821
/// </summary>
@@ -21,7 +24,11 @@ internal class FindHashtableSymbolsVisitor : AstVisitor
2124
/// <summary>
2225
/// Initializes a new instance of FindHashtableSymbolsVisitor class
2326
/// </summary>
24-
public FindHashtableSymbolsVisitor() => SymbolReferences = new List<SymbolReference>();
27+
public FindHashtableSymbolsVisitor(ScriptFile file)
28+
{
29+
SymbolReferences = new List<SymbolReference>();
30+
_file = file;
31+
}
2532

2633
/// <summary>
2734
/// Adds keys in the input hashtable to the symbol reference
@@ -49,12 +56,16 @@ public override AstVisitAction VisitHashtable(HashtableAst hashtableAst)
4956
File = hashtableAst.Extent.File
5057
};
5158

52-
const SymbolType symbolType = SymbolType.HashtableKey;
53-
5459
SymbolReferences.Add(
5560
new SymbolReference(
56-
symbolType,
57-
nameExtent));
61+
SymbolType.HashtableKey,
62+
nameExtent.Text,
63+
nameExtent,
64+
// TODO: Should this be more?
65+
nameExtent,
66+
_file,
67+
// TODO: Should this be true?
68+
isDeclaration: false));
5869
}
5970
}
6071

0 commit comments

Comments
 (0)