diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommandHandler.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommandHandler.cs index 2d615360d..8ece12bc6 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommandHandler.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommandHandler.cs @@ -48,29 +48,53 @@ public async Task> Handle(GetCommandParams request, Cance PSCommand psCommand = new PSCommand(); // Executes the following: - // Get-Command -CommandType Function,Cmdlet,ExternalScript | Select-Object -Property Name,ModuleName | Sort-Object -Property Name + // Get-Command -CommandType Function,Cmdlet,ExternalScript | Sort-Object -Property Name psCommand .AddCommand("Microsoft.PowerShell.Core\\Get-Command") .AddParameter("CommandType", new[] { "Function", "Cmdlet", "ExternalScript" }) - .AddCommand("Microsoft.PowerShell.Utility\\Select-Object") - .AddParameter("Property", new[] { "Name", "ModuleName" }) .AddCommand("Microsoft.PowerShell.Utility\\Sort-Object") .AddParameter("Property", "Name"); - IEnumerable result = await _powerShellContextService.ExecuteCommandAsync(psCommand).ConfigureAwait(false); + IEnumerable result = await _powerShellContextService.ExecuteCommandAsync(psCommand).ConfigureAwait(false); var commandList = new List(); if (result != null) { - foreach (dynamic command in result) + foreach (CommandInfo command in result) { + // Some info objects have a quicker way to get the DefaultParameterSet. These + // are also the most likely to show up so win-win. + string defaultParameterSet = null; + switch (command) + { + case CmdletInfo info: + defaultParameterSet = info.DefaultParameterSet; + break; + case FunctionInfo info: + defaultParameterSet = info.DefaultParameterSet; + break; + } + + if (defaultParameterSet == null) + { + // Try to get the default ParameterSet if it isn't streamlined (ExternalScriptInfo for example) + foreach (CommandParameterSetInfo parameterSetInfo in command.ParameterSets) + { + if (parameterSetInfo.IsDefault) + { + defaultParameterSet = parameterSetInfo.Name; + break; + } + } + } + commandList.Add(new PSCommandMessage { Name = command.Name, ModuleName = command.ModuleName, Parameters = command.Parameters, ParameterSets = command.ParameterSets, - DefaultParameterSet = command.DefaultParameterSet + DefaultParameterSet = defaultParameterSet }); } } diff --git a/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs b/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs index 4640ab723..9f5e697d3 100644 --- a/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs +++ b/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs @@ -1004,8 +1004,8 @@ await LanguageClient.SendRequest( [Fact] public async Task CanSendGetCommandRequest() { - List pSCommandMessages = - await LanguageClient.SendRequest>("powerShell/getCommand", new GetCommandParams()); + List pSCommandMessages = + await LanguageClient.SendRequest>("powerShell/getCommand", new GetCommandParams()); Assert.NotEmpty(pSCommandMessages); // There should be at least 20 commands or so.