diff --git a/src/PowerShellEditorServices.Protocol/LanguageServer/GetCommandRequest.cs b/src/PowerShellEditorServices.Protocol/LanguageServer/GetCommandRequest.cs new file mode 100644 index 000000000..df847dcbe --- /dev/null +++ b/src/PowerShellEditorServices.Protocol/LanguageServer/GetCommandRequest.cs @@ -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 +{ + /// + /// Describes the request to get the details for PowerShell Commands from the current session. + /// + public class GetCommandRequest + { + public static readonly + RequestType Type = + RequestType.Create("powerShell/getCommand"); + } + + /// + /// Describes the message to get the details for a single PowerShell Command + /// from the current session + /// + public class PSCommandMessage + { + public string Name { get; set; } + public string ModuleName { get; set; } + public string DefaultParameterSet { get; set; } + public Dictionary Parameters { get; set; } + public System.Collections.ObjectModel.ReadOnlyCollection ParameterSets { get; set; } + } +} diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index ab3e7a040..cb5ea17cd 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -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; @@ -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); @@ -523,6 +525,48 @@ function __Expand-Alias { await requestContext.SendResult(result.First().ToString()); } + private async Task HandleGetCommandRequestAsync( + string param, + RequestContext 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 result = await this.editorSession.PowerShellContext.ExecuteCommand(psCommand); + var commandList = new List(); + + 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 requestContext)