Skip to content

Commit fc1a95a

Browse files
Refactor GetCommandHandler to not use dynamic (#1268)
* Refactor GetCommandHandler to not use dynamic * streamline a couple CommandType's Co-authored-by: Tyler Leonhardt (POWERSHELL) <tyleonha@microsoft.com>
1 parent 6567d5c commit fc1a95a

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommandHandler.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,53 @@ public async Task<List<PSCommandMessage>> Handle(GetCommandParams request, Cance
4848
PSCommand psCommand = new PSCommand();
4949

5050
// Executes the following:
51-
// Get-Command -CommandType Function,Cmdlet,ExternalScript | Select-Object -Property Name,ModuleName | Sort-Object -Property Name
51+
// Get-Command -CommandType Function,Cmdlet,ExternalScript | Sort-Object -Property Name
5252
psCommand
5353
.AddCommand("Microsoft.PowerShell.Core\\Get-Command")
5454
.AddParameter("CommandType", new[] { "Function", "Cmdlet", "ExternalScript" })
55-
.AddCommand("Microsoft.PowerShell.Utility\\Select-Object")
56-
.AddParameter("Property", new[] { "Name", "ModuleName" })
5755
.AddCommand("Microsoft.PowerShell.Utility\\Sort-Object")
5856
.AddParameter("Property", "Name");
5957

60-
IEnumerable<PSObject> result = await _powerShellContextService.ExecuteCommandAsync<PSObject>(psCommand).ConfigureAwait(false);
58+
IEnumerable<CommandInfo> result = await _powerShellContextService.ExecuteCommandAsync<CommandInfo>(psCommand).ConfigureAwait(false);
6159

6260
var commandList = new List<PSCommandMessage>();
6361
if (result != null)
6462
{
65-
foreach (dynamic command in result)
63+
foreach (CommandInfo command in result)
6664
{
65+
// Some info objects have a quicker way to get the DefaultParameterSet. These
66+
// are also the most likely to show up so win-win.
67+
string defaultParameterSet = null;
68+
switch (command)
69+
{
70+
case CmdletInfo info:
71+
defaultParameterSet = info.DefaultParameterSet;
72+
break;
73+
case FunctionInfo info:
74+
defaultParameterSet = info.DefaultParameterSet;
75+
break;
76+
}
77+
78+
if (defaultParameterSet == null)
79+
{
80+
// Try to get the default ParameterSet if it isn't streamlined (ExternalScriptInfo for example)
81+
foreach (CommandParameterSetInfo parameterSetInfo in command.ParameterSets)
82+
{
83+
if (parameterSetInfo.IsDefault)
84+
{
85+
defaultParameterSet = parameterSetInfo.Name;
86+
break;
87+
}
88+
}
89+
}
90+
6791
commandList.Add(new PSCommandMessage
6892
{
6993
Name = command.Name,
7094
ModuleName = command.ModuleName,
7195
Parameters = command.Parameters,
7296
ParameterSets = command.ParameterSets,
73-
DefaultParameterSet = command.DefaultParameterSet
97+
DefaultParameterSet = defaultParameterSet
7498
});
7599
}
76100
}

test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,8 +1004,8 @@ await LanguageClient.SendRequest<EvaluateResponseBody>(
10041004
[Fact]
10051005
public async Task CanSendGetCommandRequest()
10061006
{
1007-
List<PSCommandMessage> pSCommandMessages =
1008-
await LanguageClient.SendRequest<List<PSCommandMessage>>("powerShell/getCommand", new GetCommandParams());
1007+
List<object> pSCommandMessages =
1008+
await LanguageClient.SendRequest<List<object>>("powerShell/getCommand", new GetCommandParams());
10091009

10101010
Assert.NotEmpty(pSCommandMessages);
10111011
// There should be at least 20 commands or so.

0 commit comments

Comments
 (0)