Skip to content

Commit dfc94c1

Browse files
committed
Clean up CommandHelpers.cs
1 parent d49d8f4 commit dfc94c1

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

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

+21-18
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;
@@ -17,8 +17,8 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility
1717
/// </summary>
1818
internal static class CommandHelpers
1919
{
20-
private static readonly HashSet<string> s_nounExclusionList = new HashSet<string>
21-
{
20+
private static readonly HashSet<string> s_nounExclusionList = new()
21+
{
2222
// PowerShellGet v2 nouns
2323
"CredsFromCredentialProvider",
2424
"DscResource",
@@ -36,8 +36,8 @@ internal static class CommandHelpers
3636
};
3737

3838
// This is used when a noun exists in multiple modules (for example, "Command" is used in Microsoft.PowerShell.Core and also PowerShellGet)
39-
private static readonly HashSet<string> s_cmdletExclusionList = new HashSet<string>
40-
{
39+
private static readonly HashSet<string> s_cmdletExclusionList = new()
40+
{
4141
// Commands in PowerShellGet with conflicting nouns
4242
"Find-Command",
4343
"Find-Module",
@@ -49,17 +49,16 @@ internal static class CommandHelpers
4949
"Update-ModuleManifest",
5050
};
5151

52-
private static readonly ConcurrentDictionary<string, CommandInfo> s_commandInfoCache =
53-
new ConcurrentDictionary<string, CommandInfo>();
52+
private static readonly ConcurrentDictionary<string, CommandInfo> s_commandInfoCache = new();
5453

55-
private static readonly ConcurrentDictionary<string, string> s_synopsisCache =
56-
new ConcurrentDictionary<string, string>();
54+
private static readonly ConcurrentDictionary<string, string> s_synopsisCache = new();
5755

5856
/// <summary>
5957
/// Gets the CommandInfo instance for a command with a particular name.
6058
/// </summary>
6159
/// <param name="commandName">The name of the command.</param>
62-
/// <param name="powerShellContext">The PowerShellContext to use for running Get-Command.</param>
60+
/// <param name="currentRunspace">The current runspace.</param>
61+
/// <param name="executionService">The execution service.</param>
6362
/// <returns>A CommandInfo object with details about the specified command.</returns>
6463
public static async Task<CommandInfo> GetCommandInfoAsync(
6564
string commandName,
@@ -97,7 +96,11 @@ public static async Task<CommandInfo> GetCommandInfoAsync(
9796
.AddArgument(commandName)
9897
.AddParameter("ErrorAction", "Ignore");
9998

100-
CommandInfo commandInfo = (await executionService.ExecutePSCommandAsync<CommandInfo>(command, CancellationToken.None).ConfigureAwait(false)).FirstOrDefault();
99+
IReadOnlyList<CommandInfo> results = await executionService
100+
.ExecutePSCommandAsync<CommandInfo>(command, CancellationToken.None)
101+
.ConfigureAwait(false);
102+
103+
CommandInfo commandInfo = results.Count > 0 ? results[0] : null;
101104

102105
// Only cache CmdletInfos since they're exposed in binaries they are likely to not change throughout the session.
103106
if (commandInfo?.CommandType == CommandTypes.Cmdlet)
@@ -112,8 +115,8 @@ public static async Task<CommandInfo> GetCommandInfoAsync(
112115
/// Gets the command's "Synopsis" documentation section.
113116
/// </summary>
114117
/// <param name="commandInfo">The CommandInfo instance for the command.</param>
115-
/// <param name="executionService">The PowerShellContext to use for getting command documentation.</param>
116-
/// <returns></returns>
118+
/// <param name="executionService">The exeuction service to use for getting command documentation.</param>
119+
/// <returns>The synopsis.</returns>
117120
public static async Task<string> GetCommandSynopsisAsync(
118121
CommandInfo commandInfo,
119122
IInternalPowerShellExecutionService executionService)
@@ -146,13 +149,13 @@ public static async Task<string> GetCommandSynopsisAsync(
146149
.AddParameter("Online", false)
147150
.AddParameter("ErrorAction", "Ignore");
148151

149-
IReadOnlyList<PSObject> results = await executionService.ExecutePSCommandAsync<PSObject>(command, CancellationToken.None).ConfigureAwait(false);
150-
PSObject helpObject = results.FirstOrDefault();
152+
IReadOnlyList<PSObject> results = await executionService
153+
.ExecutePSCommandAsync<PSObject>(command, CancellationToken.None)
154+
.ConfigureAwait(false);
151155

152156
// Extract the synopsis string from the object
153-
string synopsisString =
154-
(string)helpObject?.Properties["synopsis"].Value ??
155-
string.Empty;
157+
PSObject helpObject = results.Count > 0 ? results[0] : null;
158+
string synopsisString = (string)helpObject?.Properties["synopsis"].Value ?? string.Empty;
156159

157160
// Only cache cmdlet infos because since they're exposed in binaries, the can never change throughout the session.
158161
if (commandInfo.CommandType == CommandTypes.Cmdlet)

0 commit comments

Comments
 (0)