Skip to content

Commit c68ca39

Browse files
corbobrjmholt
authored andcommitted
Add functionality to allow a Show-Command like panel in VS Code (#697)
* Return PowerShell Commands Return all commands but aliases. We're returning the same as show-command
1 parent bbe259d commit c68ca39

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
7+
using System.Collections.Generic;
8+
using System.Management.Automation;
9+
10+
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
11+
{
12+
/// <summary>
13+
/// Describes the request to get the details for PowerShell Commands from the current session.
14+
/// </summary>
15+
public class GetCommandRequest
16+
{
17+
public static readonly
18+
RequestType<string, object, object, object> Type =
19+
RequestType<string, object, object, object>.Create("powerShell/getCommand");
20+
}
21+
22+
/// <summary>
23+
/// Describes the message to get the details for a single PowerShell Command
24+
/// from the current session
25+
/// </summary>
26+
public class PSCommandMessage
27+
{
28+
public string Name { get; set; }
29+
public string ModuleName { get; set; }
30+
public string DefaultParameterSet { get; set; }
31+
public Dictionary<string, ParameterMetadata> Parameters { get; set; }
32+
public System.Collections.ObjectModel.ReadOnlyCollection<CommandParameterSetInfo> ParameterSets { get; set; }
33+
}
34+
}

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+44
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Newtonsoft.Json.Linq;
1414
using System;
1515
using System.Collections.Generic;
16+
using System.Diagnostics;
1617
using System.IO;
1718
using System.Linq;
1819
using System.Management.Automation;
@@ -138,6 +139,7 @@ public void Start()
138139
this.messageHandlers.SetRequestHandler(ShowHelpRequest.Type, this.HandleShowHelpRequest);
139140

140141
this.messageHandlers.SetRequestHandler(ExpandAliasRequest.Type, this.HandleExpandAliasRequest);
142+
this.messageHandlers.SetRequestHandler(GetCommandRequest.Type, this.HandleGetCommandRequestAsync);
141143

142144
this.messageHandlers.SetRequestHandler(FindModuleRequest.Type, this.HandleFindModuleRequest);
143145
this.messageHandlers.SetRequestHandler(InstallModuleRequest.Type, this.HandleInstallModuleRequest);
@@ -525,6 +527,48 @@ function __Expand-Alias {
525527
await requestContext.SendResult(result.First().ToString());
526528
}
527529

530+
private async Task HandleGetCommandRequestAsync(
531+
string param,
532+
RequestContext<object> requestContext)
533+
{
534+
PSCommand psCommand = new PSCommand();
535+
if (!string.IsNullOrEmpty(param))
536+
{
537+
psCommand.AddCommand("Microsoft.PowerShell.Core\\Get-Command").AddArgument(param);
538+
}
539+
else
540+
{
541+
// Executes the following:
542+
// Get-Command -CommandType Function,Cmdlet,ExternalScript | Select-Object -Property Name,ModuleName | Sort-Object -Property Name
543+
psCommand
544+
.AddCommand("Microsoft.PowerShell.Core\\Get-Command")
545+
.AddParameter("CommandType", new[]{"Function", "Cmdlet", "ExternalScript"})
546+
.AddCommand("Microsoft.PowerShell.Utility\\Select-Object")
547+
.AddParameter("Property", new[]{"Name", "ModuleName"})
548+
.AddCommand("Microsoft.PowerShell.Utility\\Sort-Object")
549+
.AddParameter("Property", "Name");
550+
}
551+
IEnumerable<PSObject> result = await this.editorSession.PowerShellContext.ExecuteCommand<PSObject>(psCommand);
552+
var commandList = new List<PSCommandMessage>();
553+
554+
if (result != null)
555+
{
556+
foreach (dynamic command in result)
557+
{
558+
commandList.Add(new PSCommandMessage
559+
{
560+
Name = command.Name,
561+
ModuleName = command.ModuleName,
562+
Parameters = command.Parameters,
563+
ParameterSets = command.ParameterSets,
564+
DefaultParameterSet = command.DefaultParameterSet
565+
});
566+
}
567+
}
568+
569+
await requestContext.SendResult(commandList);
570+
}
571+
528572
private async Task HandleFindModuleRequest(
529573
object param,
530574
RequestContext<object> requestContext)

0 commit comments

Comments
 (0)