Skip to content

Commit 98fbfbd

Browse files
committed
Minor clean-ups
1 parent da2ad17 commit 98fbfbd

File tree

7 files changed

+33
-42
lines changed

7 files changed

+33
-42
lines changed

src/PowerShellEditorServices/Services/CodeLens/ReferencesCodeLensProvider.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public async Task<CodeLens> ResolveCodeLens(
107107
List<SymbolReference> referencesResult = await _symbolsService.FindReferencesOfSymbol(
108108
foundSymbol,
109109
references,
110-
_workspaceService,
111110
cancellationToken).ConfigureAwait(false);
112111

113112
Location[] referenceLocations;

src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ internal sealed class ReferenceTable
2626
public ReferenceTable(ScriptFile parent) => _parent = parent;
2727

2828
/// <summary>
29-
/// Clears the reference table causing it to rescan the source AST when queried.
29+
/// Clears the reference table causing it to re-scan the source AST when queried.
3030
/// </summary>
3131
public void TagAsChanged()
3232
{
3333
_symbolReferences.Clear();
3434
_isInited = false;
3535
}
3636

37-
// Prefer checking if the dictionary has contents to determine if initialized. The field
38-
// `_isInited` is to guard against rescanning files with no command references, but will
39-
// generally be less reliable of a check.
37+
/// <summary>
38+
/// Prefer checking if the dictionary has contents to determine if initialized. The field
39+
/// `_isInited` is to guard against re-scanning files with no command references, but will
40+
/// generally be less reliable of a check.
41+
/// </summary>
4042
private bool IsInitialized => !_symbolReferences.IsEmpty || _isInited;
4143

4244
internal bool TryGetReferences(string command, out ConcurrentBag<IScriptExtent>? references)
@@ -73,6 +75,22 @@ private sealed class ReferenceVisitor : AstVisitor
7375

7476
public ReferenceVisitor(ReferenceTable references) => _references = references;
7577

78+
private static string? GetCommandName(CommandAst commandAst)
79+
{
80+
string commandName = commandAst.GetCommandName();
81+
if (!string.IsNullOrEmpty(commandName))
82+
{
83+
return commandName;
84+
}
85+
86+
if (commandAst.CommandElements[0] is not ExpandableStringExpressionAst expandableStringExpressionAst)
87+
{
88+
return null;
89+
}
90+
91+
return AstOperations.TryGetInferredValue(expandableStringExpressionAst, out string value) ? value : null;
92+
}
93+
7694
public override AstVisitAction VisitCommand(CommandAst commandAst)
7795
{
7896
string? commandName = GetCommandName(commandAst);
@@ -86,22 +104,6 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
86104
commandAst.CommandElements[0].Extent);
87105

88106
return AstVisitAction.Continue;
89-
90-
static string? GetCommandName(CommandAst commandAst)
91-
{
92-
string commandName = commandAst.GetCommandName();
93-
if (!string.IsNullOrEmpty(commandName))
94-
{
95-
return commandName;
96-
}
97-
98-
if (commandAst.CommandElements[0] is not ExpandableStringExpressionAst expandableStringExpressionAst)
99-
{
100-
return null;
101-
}
102-
103-
return AstOperations.TryGetInferredValue(expandableStringExpressionAst, out string value) ? value : null;
104-
}
105107
}
106108

107109
public override AstVisitAction VisitVariableExpression(VariableExpressionAst variableExpressionAst)

src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,11 @@ public static SymbolReference FindSymbolAtLocation(
161161
/// </summary>
162162
/// <param name="foundSymbol">The symbol to find all references for</param>
163163
/// <param name="referencedFiles">An array of scriptFiles too search for references in</param>
164-
/// <param name="workspace">The workspace that will be searched for symbols</param>
165164
/// <param name="cancellationToken"></param>
166165
/// <returns>FindReferencesResult</returns>
167166
public async Task<List<SymbolReference>> FindReferencesOfSymbol(
168167
SymbolReference foundSymbol,
169168
ScriptFile[] referencedFiles,
170-
WorkspaceService workspace,
171169
CancellationToken cancellationToken = default)
172170
{
173171
if (foundSymbol == null)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ public static SymbolReference FindCommandAtPosition(Ast scriptAst, int lineNumbe
189189
/// </summary>
190190
/// <param name="scriptAst">The abstract syntax tree of the given script</param>
191191
/// <param name="symbolReference">The symbol that we are looking for references of</param>
192-
/// <param name="cmdletToAliasDictionary">Dictionary maping cmdlets to aliases for finding alias references</param>
193-
/// <param name="aliasToCmdletDictionary">Dictionary maping aliases to cmdlets for finding alias references</param>
194-
/// <returns></returns>
192+
/// <param name="cmdletToAliasDictionary">Dictionary mapping cmdlets to aliases for finding alias references</param>
193+
/// <param name="aliasToCmdletDictionary">Dictionary mapping aliases to cmdlets for finding alias references</param>
194+
/// <returns>The found symbol references as an enumerable</returns>
195195
public static IEnumerable<SymbolReference> FindReferencesOfSymbol(
196196
Ast scriptAst,
197197
SymbolReference symbolReference,

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

Lines changed: 6 additions & 10 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;
@@ -25,8 +25,8 @@ internal class FindReferencesVisitor : AstVisitor2
2525
/// Constructor used when searching for aliases is needed
2626
/// </summary>
2727
/// <param name="symbolReference">The found symbolReference that other symbols are being compared to</param>
28-
/// <param name="cmdletToAliasDictionary">Dictionary maping cmdlets to aliases for finding alias references</param>
29-
/// <param name="aliasToCmdletDictionary">Dictionary maping aliases to cmdlets for finding alias references</param>
28+
/// <param name="cmdletToAliasDictionary">Dictionary mapping cmdlets to aliases for finding alias references</param>
29+
/// <param name="aliasToCmdletDictionary">Dictionary mapping aliases to cmdlets for finding alias references</param>
3030
public FindReferencesVisitor(
3131
SymbolReference symbolReference,
3232
IDictionary<string, List<string>> cmdletToAliasDictionary = default,
@@ -49,11 +49,7 @@ public FindReferencesVisitor(
4949
// exists (if the symbol isn't an alias to a command) set symbolRefCommandName to an
5050
// empty string.
5151
aliasToCmdletDictionary.TryGetValue(symbolReference.ScriptRegion.Text, out _symbolRefCommandName);
52-
53-
if (_symbolRefCommandName == null)
54-
{
55-
_symbolRefCommandName = string.Empty;
56-
}
52+
_symbolRefCommandName ??= string.Empty;
5753
}
5854

5955
/// <summary>
@@ -81,8 +77,8 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
8177
// set aliases to an empty List<string>
8278
_cmdletToAliasDictionary.TryGetValue(commandName, out List<string> aliases);
8379
_aliasToCmdletDictionary.TryGetValue(commandName, out string command);
84-
if (aliases == null) { aliases = new List<string>(); }
85-
if (command == null) { command = string.Empty; }
80+
aliases ??= new List<string>();
81+
command ??= string.Empty;
8682

8783
// Check if the found symbol's name is the same as the commandAst's name OR
8884
// if the symbol's name is an alias for this commandAst's name (commandAst is a cmdlet) OR

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public override async Task<LocationContainer> Handle(ReferenceParams request, Ca
4545
await _symbolsService.FindReferencesOfSymbol(
4646
foundSymbol,
4747
_workspaceService.ExpandScriptReferences(scriptFile),
48-
_workspaceService,
4948
cancellationToken).ConfigureAwait(false);
5049

5150
List<Location> locations = new();

test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ private Task<List<SymbolReference>> GetReferences(ScriptRegion scriptRegion)
9494

9595
return symbolsService.FindReferencesOfSymbol(
9696
symbolReference,
97-
workspace.ExpandScriptReferences(scriptFile),
98-
workspace);
97+
workspace.ExpandScriptReferences(scriptFile));
9998
}
10099

101100
private IReadOnlyList<SymbolReference> GetOccurrences(ScriptRegion scriptRegion)
@@ -257,9 +256,7 @@ public async Task FindsReferencesOnCommandWithAlias()
257256
{
258257
List<SymbolReference> referencesResult = await GetReferences(FindsReferencesOnBuiltInCommandWithAliasData.SourceDetails).ConfigureAwait(true);
259258
Assert.Equal(4, referencesResult.Count);
260-
Assert.Equal("Get-ChildItem", referencesResult[1].SymbolName);
261-
Assert.Equal("Get-ChildItem", referencesResult[2].SymbolName);
262-
Assert.Equal("Get-ChildItem", referencesResult[referencesResult.Count - 1].SymbolName);
259+
Assert.All(referencesResult, (i) => Assert.Equal("Get-ChildItem", i.SymbolName));
263260
}
264261

265262
[Fact]

0 commit comments

Comments
 (0)