From 304b728303f5e83b3a29e36e49220503676187c8 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 26 Mar 2020 10:59:07 -0700 Subject: [PATCH 1/3] refactor GetCommandSynopsisAsync method make sure cmdlets with module prefixes work --- .../Utilities/CommandHelpers.cs | 48 +++++++++---------- .../LanguageServerProtocolMessageTests.cs | 26 ++++++++++ 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Utilities/CommandHelpers.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Utilities/CommandHelpers.cs index 8e3ddc1c9..3c6e65a90 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Utilities/CommandHelpers.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Utilities/CommandHelpers.cs @@ -76,36 +76,36 @@ public static async Task GetCommandSynopsisAsync( CommandInfo commandInfo, PowerShellContextService powerShellContext) { + Validate.IsNotNull(nameof(commandInfo), commandInfo); Validate.IsNotNull(nameof(powerShellContext), powerShellContext); - string synopsisString = string.Empty; - - if (commandInfo != null && - (commandInfo.CommandType == CommandTypes.Cmdlet || - commandInfo.CommandType == CommandTypes.Function || - commandInfo.CommandType == CommandTypes.Filter)) + // A small optimization to not run Get-Help on things like DSC resources. + if (commandInfo.CommandType != CommandTypes.Cmdlet && + commandInfo.CommandType != CommandTypes.Function && + commandInfo.CommandType != CommandTypes.Filter) { - PSCommand command = new PSCommand(); - command.AddCommand(@"Microsoft.PowerShell.Core\Get-Help"); - command.AddArgument(commandInfo); - command.AddParameter("ErrorAction", "Ignore"); + return string.Empty; + } - var results = await powerShellContext.ExecuteCommandAsync(command, sendOutputToHost: false, sendErrorToHost: false).ConfigureAwait(false); - PSObject helpObject = results.FirstOrDefault(); + PSCommand command = new PSCommand() + .AddCommand(@"Microsoft.PowerShell.Core\Get-Help") + // We use .Name here instead of just passing in commandInfo because + // CommandInfo.ToString() duplicates the Prefix if one exists. + .AddArgument(commandInfo.Name) + .AddParameter("ErrorAction", "Ignore"); - if (helpObject != null) - { - // Extract the synopsis string from the object - synopsisString = - (string)helpObject.Properties["synopsis"].Value ?? - string.Empty; + var results = await powerShellContext.ExecuteCommandAsync(command, sendOutputToHost: false, sendErrorToHost: false).ConfigureAwait(false); + PSObject helpObject = results.FirstOrDefault(); - // Ignore the placeholder value for this field - if (string.Equals(synopsisString, "SHORT DESCRIPTION", System.StringComparison.CurrentCultureIgnoreCase)) - { - synopsisString = string.Empty; - } - } + // Extract the synopsis string from the object + string synopsisString = + (string)helpObject?.Properties["synopsis"].Value ?? + string.Empty; + + // Ignore the placeholder value for this field + if (string.Equals(synopsisString, "SHORT DESCRIPTION", System.StringComparison.CurrentCultureIgnoreCase)) + { + return string.Empty; } return synopsisString; diff --git a/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs b/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs index b03389234..bdce4dc8f 100644 --- a/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs +++ b/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs @@ -825,6 +825,32 @@ public async Task CanSendCompletionAndCompletionResolveRequest() Assert.Contains("Writes customized output to a host", updatedCompletionItem.Documentation.String); } + [Fact] + public async Task CanSGetHelpCompletionResolveWithPrefixRequest() + { + EvaluateResponseBody evaluateResponseBody = + await LanguageClient.SendRequest( + "evaluate", + new EvaluateRequestArguments + { + Expression = "Import-Module Microsoft.PowerShell.Archive -Prefix Slow" + }); + + string filePath = NewTestFile("Expand-SlowArch"); + + CompletionList completionItems = await LanguageClient.TextDocument.Completions( + filePath, line: 0, column: 15); + + CompletionItem completionItem = Assert.Single(completionItems, + completionItem1 => completionItem1.Label == "Expand-SlowArchive"); + + CompletionItem updatedCompletionItem = await LanguageClient.SendRequest( + "completionItem/resolve", + completionItem); + + Assert.Contains("Extracts files from a specified archive", updatedCompletionItem.Documentation.String); + } + [Fact] public async Task CanSendHoverRequest() { From 774c241c38bc5f30e13a14fed63c0189b1d96fb0 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 26 Mar 2020 11:10:49 -0700 Subject: [PATCH 2/3] codacy --- .../LanguageServerProtocolMessageTests.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs b/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs index bdce4dc8f..4640ab723 100644 --- a/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs +++ b/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs @@ -826,15 +826,14 @@ public async Task CanSendCompletionAndCompletionResolveRequest() } [Fact] - public async Task CanSGetHelpCompletionResolveWithPrefixRequest() + public async Task CanSendCompletionResolveWithModulePrefixRequest() { - EvaluateResponseBody evaluateResponseBody = - await LanguageClient.SendRequest( - "evaluate", - new EvaluateRequestArguments - { - Expression = "Import-Module Microsoft.PowerShell.Archive -Prefix Slow" - }); + await LanguageClient.SendRequest( + "evaluate", + new EvaluateRequestArguments + { + Expression = "Import-Module Microsoft.PowerShell.Archive -Prefix Slow" + }); string filePath = NewTestFile("Expand-SlowArch"); From 6b7f252fe72ba58e8e12e8b7bd86bdb276e5069c Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 26 Mar 2020 12:08:55 -0700 Subject: [PATCH 3/3] AddArgument -> AddParameter --- .../Services/PowerShellContext/Utilities/CommandHelpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Utilities/CommandHelpers.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Utilities/CommandHelpers.cs index 3c6e65a90..7e09735d6 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Utilities/CommandHelpers.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Utilities/CommandHelpers.cs @@ -91,7 +91,7 @@ public static async Task GetCommandSynopsisAsync( .AddCommand(@"Microsoft.PowerShell.Core\Get-Help") // We use .Name here instead of just passing in commandInfo because // CommandInfo.ToString() duplicates the Prefix if one exists. - .AddArgument(commandInfo.Name) + .AddParameter("Name", commandInfo.Name) .AddParameter("ErrorAction", "Ignore"); var results = await powerShellContext.ExecuteCommandAsync(command, sendOutputToHost: false, sendErrorToHost: false).ConfigureAwait(false);