Skip to content

Add functionality to allow a Show-Command like panel in VS Code #697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Nov 28, 2018
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using System.Collections.Generic;
using System.Management.Automation;

namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
{
/// <summary>
/// Describes the request to get the details for PowerShell Commands from the current session.
/// </summary>
public class GetCommandRequest
{
public static readonly
RequestType<string, object, object, object> Type =
RequestType<string, object, object, object>.Create("powerShell/getCommand");
}

/// <summary>
/// Describes the message to get the details for a single PowerShell Command
/// from the current session
/// </summary>
public class PSCommandMessage
{
public string Name { get; set; }
public string ModuleName { get; set; }
public string DefaultParameterSet { get; set; }
public Dictionary<string, ParameterMetadata> Parameters { get; set; }
public System.Collections.ObjectModel.ReadOnlyCollection<CommandParameterSetInfo> ParameterSets { get; set; }
}
}
44 changes: 44 additions & 0 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation;
Expand Down Expand Up @@ -136,6 +137,7 @@ public void Start()
this.messageHandlers.SetRequestHandler(ShowHelpRequest.Type, this.HandleShowHelpRequest);

this.messageHandlers.SetRequestHandler(ExpandAliasRequest.Type, this.HandleExpandAliasRequest);
this.messageHandlers.SetRequestHandler(GetCommandRequest.Type, this.HandleGetCommandRequestAsync);

this.messageHandlers.SetRequestHandler(FindModuleRequest.Type, this.HandleFindModuleRequest);
this.messageHandlers.SetRequestHandler(InstallModuleRequest.Type, this.HandleInstallModuleRequest);
Expand Down Expand Up @@ -523,6 +525,48 @@ function __Expand-Alias {
await requestContext.SendResult(result.First().ToString());
}

private async Task HandleGetCommandRequestAsync(
string param,
RequestContext<object> requestContext)
{
PSCommand psCommand = new PSCommand();
if (!string.IsNullOrEmpty(param))
{
psCommand.AddCommand("Microsoft.PowerShell.Core\\Get-Command").AddArgument(param);
}
else
{
// Executes the following:
// Get-Command -CommandType Function,Cmdlet,ExternalScript | Select-Object -Property Name,ModuleName | 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<PSObject> result = await this.editorSession.PowerShellContext.ExecuteCommand<PSObject>(psCommand);
var commandList = new List<PSCommandMessage>();

if (result != null)
{
foreach (dynamic command in result)
{
commandList.Add(new PSCommandMessage
{
Name = command.Name,
ModuleName = command.ModuleName,
Parameters = command.Parameters,
ParameterSets = command.ParameterSets,
DefaultParameterSet = command.DefaultParameterSet
});
}
}

await requestContext.SendResult(commandList);
}

private async Task HandleFindModuleRequest(
object param,
RequestContext<object> requestContext)
Expand Down