Skip to content
Merged
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