Skip to content

Commit 3f07453

Browse files
committed
Move GetAliases into CommandHelpers
1 parent dfc94c1 commit 3f07453

File tree

2 files changed

+46
-40
lines changed

2 files changed

+46
-40
lines changed

src/PowerShellEditorServices/Services/PowerShell/Utility/CommandHelpers.cs

+42-2
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.Collections.Concurrent;
@@ -50,8 +50,9 @@ internal static class CommandHelpers
5050
};
5151

5252
private static readonly ConcurrentDictionary<string, CommandInfo> s_commandInfoCache = new();
53-
5453
private static readonly ConcurrentDictionary<string, string> s_synopsisCache = new();
54+
private static readonly ConcurrentDictionary<string, List<string>> s_cmdletToAliasCache = new(System.StringComparer.OrdinalIgnoreCase);
55+
private static readonly ConcurrentDictionary<string, string> s_aliasToCmdletCache = new(System.StringComparer.OrdinalIgnoreCase);
5556

5657
/// <summary>
5758
/// Gets the CommandInfo instance for a command with a particular name.
@@ -171,5 +172,44 @@ public static async Task<string> GetCommandSynopsisAsync(
171172

172173
return synopsisString;
173174
}
175+
176+
/// <summary>
177+
/// Gets all aliases found in the runspace
178+
/// </summary>
179+
/// <param name="executionService"></param>
180+
public static async Task<(ConcurrentDictionary<string, List<string>>, ConcurrentDictionary<string, string>)> GetAliasesAsync(IInternalPowerShellExecutionService executionService)
181+
{
182+
Validate.IsNotNull(nameof(executionService), executionService);
183+
184+
// TODO: Should we return the caches if they're not empty, or always update?
185+
// if (!s_cmdletToAliasCache.IsEmpty || !s_aliasToCmdletCache.IsEmpty)
186+
// {
187+
// return (s_cmdletToAliasCache, s_aliasToCmdletCache);
188+
// }
189+
190+
IEnumerable<CommandInfo> aliases = await executionService.ExecuteDelegateAsync<IEnumerable<CommandInfo>>(
191+
nameof(GetAliasesAsync),
192+
Execution.ExecutionOptions.Default,
193+
(pwsh, _) =>
194+
{
195+
CommandInvocationIntrinsics invokeCommand = pwsh.Runspace.SessionStateProxy.InvokeCommand;
196+
return invokeCommand.GetCommands("*", CommandTypes.Alias, nameIsPattern: true);
197+
},
198+
CancellationToken.None).ConfigureAwait(false);
199+
200+
foreach (AliasInfo aliasInfo in aliases)
201+
{
202+
List<string> aliasList = new() { aliasInfo.Name };
203+
204+
s_cmdletToAliasCache.AddOrUpdate(
205+
aliasInfo.Definition,
206+
aliasList,
207+
(_, v) => v.Concat(aliasList).ToList());
208+
209+
s_aliasToCmdletCache.TryAdd(aliasInfo.Name, aliasInfo.Definition);
210+
}
211+
212+
return (s_cmdletToAliasCache, s_aliasToCmdletCache);
213+
}
174214
}
175215
}

src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs

+4-38
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ internal class SymbolsService
3939

4040
private readonly ConcurrentDictionary<string, ICodeLensProvider> _codeLensProviders;
4141
private readonly ConcurrentDictionary<string, IDocumentSymbolProvider> _documentSymbolProviders;
42-
private readonly Dictionary<String, List<String>> _cmdletToAliasDictionary;
43-
private readonly Dictionary<String, String> _aliasToCmdletDictionary;
4442
#endregion
4543

4644
#region Constructors
@@ -85,10 +83,6 @@ public SymbolsService(
8583
{
8684
_documentSymbolProviders.TryAdd(documentSymbolProvider.ProviderId, documentSymbolProvider);
8785
}
88-
89-
_cmdletToAliasDictionary = new Dictionary<String, List<String>>(StringComparer.OrdinalIgnoreCase);
90-
_aliasToCmdletDictionary = new Dictionary<String, String>(StringComparer.OrdinalIgnoreCase);
91-
GetAliases();
9286
}
9387

9488
#endregion
@@ -191,6 +185,8 @@ public List<SymbolReference> FindReferencesOfSymbol(
191185
return null;
192186
}
193187

188+
(ConcurrentDictionary<string, List<string>> cmdletToAliases, ConcurrentDictionary<string, string> aliasToCmdlets) = await CommandHelpers.GetAliasesAsync(_executionService).ConfigureAwait(false);
189+
194190
// We want to look for references first in referenced files, hence we use ordered dictionary
195191
// TODO: File system case-sensitivity is based on filesystem not OS, but OS is a much cheaper heuristic
196192
var fileMap = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
@@ -224,8 +220,8 @@ public List<SymbolReference> FindReferencesOfSymbol(
224220
IEnumerable<SymbolReference> references = AstOperations.FindReferencesOfSymbol(
225221
file.ScriptAst,
226222
foundSymbol,
227-
_cmdletToAliasDictionary,
228-
_aliasToCmdletDictionary);
223+
cmdletToAliases,
224+
aliasToCmdlets);
229225

230226
foreach (SymbolReference reference in references)
231227
{
@@ -494,36 +490,6 @@ await CommandHelpers.GetCommandInfoAsync(
494490
return foundDefinition;
495491
}
496492

497-
/// <summary>
498-
/// Gets all aliases found in the runspace
499-
/// </summary>
500-
private async void GetAliases()
501-
{
502-
IEnumerable<CommandInfo> aliases = await _executionService.ExecuteDelegateAsync<IEnumerable<CommandInfo>>(
503-
nameof(GetAliases),
504-
PowerShell.Execution.ExecutionOptions.Default,
505-
(pwsh, _) =>
506-
{
507-
CommandInvocationIntrinsics invokeCommand = pwsh.Runspace.SessionStateProxy.InvokeCommand;
508-
return invokeCommand.GetCommands("*", CommandTypes.Alias, nameIsPattern: true);
509-
},
510-
System.Threading.CancellationToken.None).ConfigureAwait(false);
511-
512-
foreach (AliasInfo aliasInfo in aliases)
513-
{
514-
if (!_cmdletToAliasDictionary.ContainsKey(aliasInfo.Definition))
515-
{
516-
_cmdletToAliasDictionary.Add(aliasInfo.Definition, new List<String>() { aliasInfo.Name });
517-
}
518-
else
519-
{
520-
_cmdletToAliasDictionary[aliasInfo.Definition].Add(aliasInfo.Name);
521-
}
522-
523-
_aliasToCmdletDictionary.Add(aliasInfo.Name, aliasInfo.Definition);
524-
}
525-
}
526-
527493
/// <summary>
528494
/// Gets a path from a dot-source symbol.
529495
/// </summary>

0 commit comments

Comments
 (0)