From c35d13ed0d7269e2a59f1f0e4d503eed24ca9668 Mon Sep 17 00:00:00 2001 From: corbob Date: Thu, 12 Jul 2018 06:57:18 -0700 Subject: [PATCH 01/15] Change from Online to Full Also output to Host. This is an expiriment on vscode-powershell issue 884. --- .../Server/LanguageServer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index 7f8f223b4..afb501bb5 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -240,9 +240,9 @@ protected async Task HandleShowOnlineHelpRequest( var psCommand = new PSCommand(); psCommand.AddCommand("Get-Help"); psCommand.AddArgument(helpParams); - psCommand.AddParameter("Online"); + psCommand.AddParameter("Full"); - await editorSession.PowerShellContext.ExecuteCommand(psCommand); + await editorSession.PowerShellContext.ExecuteCommand(psCommand, true); await requestContext.SendResult(null); } From 75f8afb50cf61057be6a9058dcb93b9a2a8eb27f Mon Sep 17 00:00:00 2001 From: corbob Date: Fri, 10 Aug 2018 06:22:14 -0700 Subject: [PATCH 02/15] Added checking for RelatedLinks --- .../Server/LanguageServer.cs | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index afb501bb5..9dae13d87 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -240,9 +240,25 @@ protected async Task HandleShowOnlineHelpRequest( var psCommand = new PSCommand(); psCommand.AddCommand("Get-Help"); psCommand.AddArgument(helpParams); - psCommand.AddParameter("Full"); + psCommand.AddCommand("Select-Object").AddArgument("RelatedLinks"); + var relatedLinks = await editorSession.PowerShellContext.ExecuteCommand(psCommand); - await editorSession.PowerShellContext.ExecuteCommand(psCommand, true); + psCommand = new PSCommand(); + psCommand.AddCommand("Get-Help"); + psCommand.AddArgument(helpParams); + bool sendOutput = false; + // Check if the first returned item converted to string is an empty object. + // From experience this appears to be the return when there are no RelatedLinks. + if (relatedLinks.First().ToString() != "@{RelatedLinks=}") + { + psCommand.AddParameter("Online"); + } else + { + psCommand.AddParameter("Full"); + sendOutput = true; + } + + await editorSession.PowerShellContext.ExecuteCommand(psCommand, sendOutput); await requestContext.SendResult(null); } From 55936d185c2ef02e9f60ac101c50e232269af33b Mon Sep 17 00:00:00 2001 From: corbob Date: Fri, 10 Aug 2018 07:09:58 -0700 Subject: [PATCH 03/15] Check if command first If not a command, Get-Help takes 4-5 seconds to return, and we will crash PSES if we don't account for the return of a 0 count. --- .../Server/LanguageServer.cs | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index 9dae13d87..0046f26c2 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -238,29 +238,38 @@ protected async Task HandleShowOnlineHelpRequest( if (helpParams == null) { helpParams = "get-help"; } var psCommand = new PSCommand(); - psCommand.AddCommand("Get-Help"); - psCommand.AddArgument(helpParams); - psCommand.AddCommand("Select-Object").AddArgument("RelatedLinks"); - var relatedLinks = await editorSession.PowerShellContext.ExecuteCommand(psCommand); + // Check if it's an actual command first. This returns near instant. Get-Help takes a good 5 seconds to return when not a command. + psCommand.AddCommand("Get-Command").AddArgument(helpParams); + var isCommand = await editorSession.PowerShellContext.ExecuteCommand(psCommand); + if (isCommand.Count() > 0){ + psCommand = new PSCommand(); + psCommand.AddCommand("Get-Help"); + psCommand.AddArgument(helpParams); + psCommand.AddCommand("Select-Object").AddArgument("RelatedLinks"); + var relatedLinks = await editorSession.PowerShellContext.ExecuteCommand(psCommand); + psCommand = new PSCommand(); + psCommand.AddCommand("Get-Help"); + psCommand.AddArgument(helpParams); + bool sendOutput = false; + // Check if the first returned item converted to string is an empty object. + // From experience this is the return when there are no RelatedLinks. + if (relatedLinks.First().ToString() != "@{RelatedLinks=}") + { + psCommand.AddParameter("Online"); + } + else + { + psCommand.AddParameter("Full"); + sendOutput = true; + } + await editorSession.PowerShellContext.ExecuteCommand(psCommand, sendOutput); - psCommand = new PSCommand(); - psCommand.AddCommand("Get-Help"); - psCommand.AddArgument(helpParams); - bool sendOutput = false; - // Check if the first returned item converted to string is an empty object. - // From experience this appears to be the return when there are no RelatedLinks. - if (relatedLinks.First().ToString() != "@{RelatedLinks=}") - { - psCommand.AddParameter("Online"); - } else + await requestContext.SendResult(null); + } + else { - psCommand.AddParameter("Full"); - sendOutput = true; + // TODO: write error or something... } - - await editorSession.PowerShellContext.ExecuteCommand(psCommand, sendOutput); - - await requestContext.SendResult(null); } private async Task HandleSetPSSARulesRequest( From 6ad4e91ae80d747c380de381d66b742f0834dfd7 Mon Sep 17 00:00:00 2001 From: corbob Date: Sat, 18 Aug 2018 11:23:47 -0700 Subject: [PATCH 04/15] Cleanup types. Output when no commands found. --- .../Server/LanguageServer.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index 0046f26c2..0d6974a6d 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -237,23 +237,23 @@ protected async Task HandleShowOnlineHelpRequest( { if (helpParams == null) { helpParams = "get-help"; } - var psCommand = new PSCommand(); + PSCommand psCommand = new PSCommand(); // Check if it's an actual command first. This returns near instant. Get-Help takes a good 5 seconds to return when not a command. psCommand.AddCommand("Get-Command").AddArgument(helpParams); - var isCommand = await editorSession.PowerShellContext.ExecuteCommand(psCommand); - if (isCommand.Count() > 0){ + IEnumerable isCommand = await editorSession.PowerShellContext.ExecuteCommand(psCommand); + if (isCommand != null && isCommand.Count() > 0){ psCommand = new PSCommand(); psCommand.AddCommand("Get-Help"); psCommand.AddArgument(helpParams); psCommand.AddCommand("Select-Object").AddArgument("RelatedLinks"); - var relatedLinks = await editorSession.PowerShellContext.ExecuteCommand(psCommand); + IEnumerable relatedLinks = await editorSession.PowerShellContext.ExecuteCommand(psCommand); psCommand = new PSCommand(); psCommand.AddCommand("Get-Help"); psCommand.AddArgument(helpParams); bool sendOutput = false; // Check if the first returned item converted to string is an empty object. // From experience this is the return when there are no RelatedLinks. - if (relatedLinks.First().ToString() != "@{RelatedLinks=}") + if (relatedLinks != null && relatedLinks.First().ToString() != "@{RelatedLinks=}") { psCommand.AddParameter("Online"); } @@ -268,7 +268,10 @@ protected async Task HandleShowOnlineHelpRequest( } else { - // TODO: write error or something... + psCommand = new PSCommand().AddScript($"write-host -ForegroundColor red 'Command not found: {helpParams}'"); + await editorSession.PowerShellContext.ExecuteCommand(psCommand, true); + await requestContext.SendResult(null); + // TODO: write better error output or something... } } From 51a2ae8d536cc59b872eb6d4e8b85576e41014b5 Mon Sep 17 00:00:00 2001 From: corbob Date: Mon, 3 Sep 2018 06:27:25 -0700 Subject: [PATCH 05/15] Refactor to a PowerShell Function --- .../Server/LanguageServer.cs | 62 ++++++++----------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index 0d6974a6d..047b97d44 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -236,44 +236,34 @@ protected async Task HandleShowOnlineHelpRequest( RequestContext requestContext) { if (helpParams == null) { helpParams = "get-help"; } - - PSCommand psCommand = new PSCommand(); - // Check if it's an actual command first. This returns near instant. Get-Help takes a good 5 seconds to return when not a command. - psCommand.AddCommand("Get-Command").AddArgument(helpParams); - IEnumerable isCommand = await editorSession.PowerShellContext.ExecuteCommand(psCommand); - if (isCommand != null && isCommand.Count() > 0){ - psCommand = new PSCommand(); - psCommand.AddCommand("Get-Help"); - psCommand.AddArgument(helpParams); - psCommand.AddCommand("Select-Object").AddArgument("RelatedLinks"); - IEnumerable relatedLinks = await editorSession.PowerShellContext.ExecuteCommand(psCommand); - psCommand = new PSCommand(); - psCommand.AddCommand("Get-Help"); - psCommand.AddArgument(helpParams); - bool sendOutput = false; - // Check if the first returned item converted to string is an empty object. - // From experience this is the return when there are no RelatedLinks. - if (relatedLinks != null && relatedLinks.First().ToString() != "@{RelatedLinks=}") - { - psCommand.AddParameter("Online"); - } - else - { - psCommand.AddParameter("Full"); - sendOutput = true; - } - await editorSession.PowerShellContext.ExecuteCommand(psCommand, sendOutput); - - await requestContext.SendResult(null); - } - else - { - psCommand = new PSCommand().AddScript($"write-host -ForegroundColor red 'Command not found: {helpParams}'"); - await editorSession.PowerShellContext.ExecuteCommand(psCommand, true); - await requestContext.SendResult(null); - // TODO: write better error output or something... + string script = @" +function __Check-Help { + param ( + $Command + ) + $CommandExists = $false + Try { + Get-Command $Command -ErrorAction Stop + $CommandExists = $true + } catch [System.Management.Automation.CommandNotFoundException] { + Write-Warning ""$Command does not exist."" + } + if ($CommandExists) { + try { + Get-Help $Command -Online + } catch [System.Management.Automation.PSInvalidOperationException] { + Get-Help $Command -Full } } +}"; + PSCommand checkHelpScript = new PSCommand(); + checkHelpScript.AddScript(script); + await this.editorSession.PowerShellContext.ExecuteCommand(checkHelpScript); + PSCommand getHelpCommand = new PSCommand(); + getHelpCommand.AddCommand("__Check-Help").AddArgument(helpParams); + await editorSession.PowerShellContext.ExecuteCommand(getHelpCommand, true); + await requestContext.SendResult(null); + } private async Task HandleSetPSSARulesRequest( object param, From 0f65dc66048837ce7a5aee4462e73167d294a958 Mon Sep 17 00:00:00 2001 From: corbob Date: Tue, 4 Sep 2018 21:14:08 -0700 Subject: [PATCH 06/15] Remove extraneous function declaration. --- .../Server/LanguageServer.cs | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index 047b97d44..907049d99 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -235,33 +235,25 @@ protected async Task HandleShowOnlineHelpRequest( string helpParams, RequestContext requestContext) { - if (helpParams == null) { helpParams = "get-help"; } + if (helpParams == null) { helpParams = "Get-Help"; } string script = @" -function __Check-Help { - param ( - $Command - ) - $CommandExists = $false - Try { - Get-Command $Command -ErrorAction Stop - $CommandExists = $true - } catch [System.Management.Automation.CommandNotFoundException] { - Write-Warning ""$Command does not exist."" - } - if ($CommandExists) { - try { - Get-Help $Command -Online - } catch [System.Management.Automation.PSInvalidOperationException] { - Get-Help $Command -Full - } - } +[CmdletBinding()] +param ( + [String]$CommandName +) +Try { + Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop | Out-Null +} catch [System.Management.Automation.CommandNotFoundException] { + $PSCmdlet.ThrowTerminatingError($PSItem) +} +try { + Microsoft.PowerShell.Core\Get-Help $CommandName -Online | Out-Null +} catch [System.Management.Automation.PSInvalidOperationException] { + Microsoft.PowerShell.Core\Get-Help $CommandName -Full }"; PSCommand checkHelpScript = new PSCommand(); - checkHelpScript.AddScript(script); - await this.editorSession.PowerShellContext.ExecuteCommand(checkHelpScript); - PSCommand getHelpCommand = new PSCommand(); - getHelpCommand.AddCommand("__Check-Help").AddArgument(helpParams); - await editorSession.PowerShellContext.ExecuteCommand(getHelpCommand, true); + checkHelpScript.AddScript(script,true).AddArgument(helpParams); + await editorSession.PowerShellContext.ExecuteCommand(checkHelpScript, true); await requestContext.SendResult(null); } From 74f04ff9fda99035da8de9c742806696afdbc5f6 Mon Sep 17 00:00:00 2001 From: corbob Date: Tue, 4 Sep 2018 22:24:46 -0700 Subject: [PATCH 07/15] Move Script to private static variable --- .../Server/LanguageServer.cs | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index 907049d99..d65fba9b5 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -42,6 +42,21 @@ public class LanguageServer private TaskCompletionSource serverCompletedTask; + private static string checkHelpScript = @" +[CmdletBinding()] +param ( + [String]$CommandName +) +Try { + Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop | Out-Null +} catch [System.Management.Automation.CommandNotFoundException] { + $PSCmdlet.ThrowTerminatingError($PSItem) +} +try { + Microsoft.PowerShell.Core\Get-Help $CommandName -Online | Out-Null +} catch [System.Management.Automation.PSInvalidOperationException] { + Microsoft.PowerShell.Core\Get-Help $CommandName -Full +}"; public IEditorOperations EditorOperations { get { return this.editorOperations; } @@ -230,30 +245,15 @@ await requestContext.SendResult( } }); } - protected async Task HandleShowOnlineHelpRequest( string helpParams, RequestContext requestContext) { if (helpParams == null) { helpParams = "Get-Help"; } - string script = @" -[CmdletBinding()] -param ( - [String]$CommandName -) -Try { - Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop | Out-Null -} catch [System.Management.Automation.CommandNotFoundException] { - $PSCmdlet.ThrowTerminatingError($PSItem) -} -try { - Microsoft.PowerShell.Core\Get-Help $CommandName -Online | Out-Null -} catch [System.Management.Automation.PSInvalidOperationException] { - Microsoft.PowerShell.Core\Get-Help $CommandName -Full -}"; - PSCommand checkHelpScript = new PSCommand(); - checkHelpScript.AddScript(script,true).AddArgument(helpParams); - await editorSession.PowerShellContext.ExecuteCommand(checkHelpScript, true); + + PSCommand checkHelpPSCommand = new PSCommand(); + checkHelpPSCommand.AddScript(checkHelpScript,true).AddArgument(helpParams); + await editorSession.PowerShellContext.ExecuteCommand(checkHelpPSCommand, true); await requestContext.SendResult(null); } From c772aa0a55b8cdca41fd7d419c6330e5128b3270 Mon Sep 17 00:00:00 2001 From: corbob Date: Wed, 5 Sep 2018 19:46:51 -0700 Subject: [PATCH 08/15] Be explicit with bool parameters Also: * make CheckHelpScript constant * assign output of Get-Command to $null * Move CheckHelpScript to top of class. --- .../Server/LanguageServer.cs | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index d65fba9b5..bd12edb70 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -26,6 +26,21 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.Server { public class LanguageServer { + private const string CheckHelpScript = @" +[CmdletBinding()] +param ( + [String]$CommandName +) +Try { + $null = Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop +} catch [System.Management.Automation.CommandNotFoundException] { + $PSCmdlet.ThrowTerminatingError($PSItem) +} +try { + $null = Microsoft.PowerShell.Core\Get-Help $CommandName -Online +} catch [System.Management.Automation.PSInvalidOperationException] { + Microsoft.PowerShell.Core\Get-Help $CommandName -Full +}"; private static CancellationTokenSource existingRequestCancellation; private ILogger Logger; @@ -42,21 +57,6 @@ public class LanguageServer private TaskCompletionSource serverCompletedTask; - private static string checkHelpScript = @" -[CmdletBinding()] -param ( - [String]$CommandName -) -Try { - Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop | Out-Null -} catch [System.Management.Automation.CommandNotFoundException] { - $PSCmdlet.ThrowTerminatingError($PSItem) -} -try { - Microsoft.PowerShell.Core\Get-Help $CommandName -Online | Out-Null -} catch [System.Management.Automation.PSInvalidOperationException] { - Microsoft.PowerShell.Core\Get-Help $CommandName -Full -}"; public IEditorOperations EditorOperations { get { return this.editorOperations; } @@ -252,8 +252,8 @@ protected async Task HandleShowOnlineHelpRequest( if (helpParams == null) { helpParams = "Get-Help"; } PSCommand checkHelpPSCommand = new PSCommand(); - checkHelpPSCommand.AddScript(checkHelpScript,true).AddArgument(helpParams); - await editorSession.PowerShellContext.ExecuteCommand(checkHelpPSCommand, true); + checkHelpPSCommand.AddScript(CheckHelpScript, useLocalScope: true).AddArgument(helpParams); + await editorSession.PowerShellContext.ExecuteCommand(checkHelpPSCommand, sendOutputToHost: true); await requestContext.SendResult(null); } From 4090e8e6b2210f672762c2a39e662027d258f184 Mon Sep 17 00:00:00 2001 From: corbob Date: Wed, 5 Sep 2018 20:59:46 -0700 Subject: [PATCH 09/15] Chain all the things (checkHelpPSCommand variable) --- .../Server/LanguageServer.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index bd12edb70..2f1cca44e 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -251,8 +251,9 @@ protected async Task HandleShowOnlineHelpRequest( { if (helpParams == null) { helpParams = "Get-Help"; } - PSCommand checkHelpPSCommand = new PSCommand(); - checkHelpPSCommand.AddScript(CheckHelpScript, useLocalScope: true).AddArgument(helpParams); + PSCommand checkHelpPSCommand = new PSCommand() + .AddScript(CheckHelpScript, useLocalScope: true) + .AddArgument(helpParams); await editorSession.PowerShellContext.ExecuteCommand(checkHelpPSCommand, sendOutputToHost: true); await requestContext.SendResult(null); } From c481d02087a713ce02166f4602814d1069dc4716 Mon Sep 17 00:00:00 2001 From: corbob Date: Wed, 5 Sep 2018 22:33:29 -0700 Subject: [PATCH 10/15] minor bug fix helpParams doesn't actually come over null... It comes over as empty string. Fix this to prevent a likely rare bug condition because we can. --- src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index 2f1cca44e..751eb60d5 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -249,7 +249,7 @@ protected async Task HandleShowOnlineHelpRequest( string helpParams, RequestContext requestContext) { - if (helpParams == null) { helpParams = "Get-Help"; } + if (string.IsNullOrEmpty(helpParams)) { helpParams = "Get-Help"; } PSCommand checkHelpPSCommand = new PSCommand() .AddScript(CheckHelpScript, useLocalScope: true) From d125d665eaa3580ca016a7652b7e8e4c5cbca280 Mon Sep 17 00:00:00 2001 From: corbob Date: Sat, 15 Sep 2018 19:42:24 -0700 Subject: [PATCH 11/15] Handle both powerShell/showOnlineHelp and powerShell/showHelp If someone calls showOnlineHelp we will write a warning that it's deprecated and then call showHelp. This should allow us to more easily update to remove the references to Online. Also: Move CheckHelpScript into the function and make it constant... --- .../LanguageServer/ShowOnlineHelpRequest.cs | 8 ++++ .../Server/LanguageServer.cs | 45 ++++++++++++------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/LanguageServer/ShowOnlineHelpRequest.cs b/src/PowerShellEditorServices.Protocol/LanguageServer/ShowOnlineHelpRequest.cs index 1e65df93d..b8d1772ef 100644 --- a/src/PowerShellEditorServices.Protocol/LanguageServer/ShowOnlineHelpRequest.cs +++ b/src/PowerShellEditorServices.Protocol/LanguageServer/ShowOnlineHelpRequest.cs @@ -7,10 +7,18 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer { + // We don't expect ShowOnlineHelpRequest to come from vscode anymore, but it could come from another editor. + // TODO: Note that it's deprecated if it's called??? public class ShowOnlineHelpRequest { public static readonly RequestType Type = RequestType.Create("powerShell/showOnlineHelp"); } + public class ShowHelpRequest + { + public static readonly + RequestType Type = + RequestType.Create("powerShell/showHelp"); + } } diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index 751eb60d5..d315a6bfa 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -26,21 +26,6 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.Server { public class LanguageServer { - private const string CheckHelpScript = @" -[CmdletBinding()] -param ( - [String]$CommandName -) -Try { - $null = Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop -} catch [System.Management.Automation.CommandNotFoundException] { - $PSCmdlet.ThrowTerminatingError($PSItem) -} -try { - $null = Microsoft.PowerShell.Core\Get-Help $CommandName -Online -} catch [System.Management.Automation.PSInvalidOperationException] { - Microsoft.PowerShell.Core\Get-Help $CommandName -Full -}"; private static CancellationTokenSource existingRequestCancellation; private ILogger Logger; @@ -137,6 +122,7 @@ public void Start() this.HandleDocumentRangeFormattingRequest); this.messageHandlers.SetRequestHandler(ShowOnlineHelpRequest.Type, this.HandleShowOnlineHelpRequest); + this.messageHandlers.SetRequestHandler(ShowHelpRequest.Type, this.HandleShowHelpRequest); this.messageHandlers.SetRequestHandler(ExpandAliasRequest.Type, this.HandleExpandAliasRequest); this.messageHandlers.SetRequestHandler(FindModuleRequest.Type, this.HandleFindModuleRequest); @@ -245,10 +231,25 @@ await requestContext.SendResult( } }); } - protected async Task HandleShowOnlineHelpRequest( + protected async Task HandleShowHelpRequest( string helpParams, RequestContext requestContext) { + const string CheckHelpScript = @" + [CmdletBinding()] + param ( + [String]$CommandName + ) + Try { + $null = Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop + } catch [System.Management.Automation.CommandNotFoundException] { + $PSCmdlet.ThrowTerminatingError($PSItem) + } + try { + $null = Microsoft.PowerShell.Core\Get-Help $CommandName -Online + } catch [System.Management.Automation.PSInvalidOperationException] { + Microsoft.PowerShell.Core\Get-Help $CommandName -Full + }"; if (string.IsNullOrEmpty(helpParams)) { helpParams = "Get-Help"; } PSCommand checkHelpPSCommand = new PSCommand() @@ -257,6 +258,18 @@ protected async Task HandleShowOnlineHelpRequest( await editorSession.PowerShellContext.ExecuteCommand(checkHelpPSCommand, sendOutputToHost: true); await requestContext.SendResult(null); } + protected async Task HandleShowOnlineHelpRequest( + string helpParams, + RequestContext requestContext + ) + { + const string deprecatedWarning = @" + Write-Warning ""'powerShell/showOnlineHelp' has been deprecated. Use 'powerShell/showHelp' instead."""; + PSCommand commandDeprecated = new PSCommand() + .AddScript(deprecatedWarning); + await editorSession.PowerShellContext.ExecuteCommand(commandDeprecated, sendOutputToHost: true); + await this.HandleShowHelpRequest(helpParams, requestContext); + } private async Task HandleSetPSSARulesRequest( object param, From f100131574f9a046d331ba4436f82b1597449a30 Mon Sep 17 00:00:00 2001 From: corbob Date: Sat, 15 Sep 2018 19:54:30 -0700 Subject: [PATCH 12/15] Try -> try --- src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index d315a6bfa..c1aa374cc 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -240,7 +240,7 @@ protected async Task HandleShowHelpRequest( param ( [String]$CommandName ) - Try { + try { $null = Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop } catch [System.Management.Automation.CommandNotFoundException] { $PSCmdlet.ThrowTerminatingError($PSItem) From c2ba5ada0372dc8346a77f4398c6bfec6155d7a0 Mon Sep 17 00:00:00 2001 From: corbob Date: Tue, 18 Sep 2018 20:06:30 -0700 Subject: [PATCH 13/15] Remove Online from filename --- .../{ShowOnlineHelpRequest.cs => ShowHelpRequest.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/PowerShellEditorServices.Protocol/LanguageServer/{ShowOnlineHelpRequest.cs => ShowHelpRequest.cs} (100%) diff --git a/src/PowerShellEditorServices.Protocol/LanguageServer/ShowOnlineHelpRequest.cs b/src/PowerShellEditorServices.Protocol/LanguageServer/ShowHelpRequest.cs similarity index 100% rename from src/PowerShellEditorServices.Protocol/LanguageServer/ShowOnlineHelpRequest.cs rename to src/PowerShellEditorServices.Protocol/LanguageServer/ShowHelpRequest.cs From 504a5a830e64d5b67cd547171a1249a394373412 Mon Sep 17 00:00:00 2001 From: corbob Date: Tue, 18 Sep 2018 20:18:59 -0700 Subject: [PATCH 14/15] Tidy up. Resolve comments from PR. --- .../LanguageServer/ShowHelpRequest.cs | 6 +++--- .../Server/LanguageServer.cs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/LanguageServer/ShowHelpRequest.cs b/src/PowerShellEditorServices.Protocol/LanguageServer/ShowHelpRequest.cs index b8d1772ef..ef63bd2b7 100644 --- a/src/PowerShellEditorServices.Protocol/LanguageServer/ShowHelpRequest.cs +++ b/src/PowerShellEditorServices.Protocol/LanguageServer/ShowHelpRequest.cs @@ -4,18 +4,18 @@ // using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol; +using System; namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer { - // We don't expect ShowOnlineHelpRequest to come from vscode anymore, but it could come from another editor. - // TODO: Note that it's deprecated if it's called??? + [Obsolete("This class is deprecated. Use ShowHelpRequest instead.")] public class ShowOnlineHelpRequest { public static readonly RequestType Type = RequestType.Create("powerShell/showOnlineHelp"); } - public class ShowHelpRequest + public class ShowHelpRequest { public static readonly RequestType Type = diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index c1aa374cc..4e14b1c23 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -231,6 +231,7 @@ await requestContext.SendResult( } }); } + protected async Task HandleShowHelpRequest( string helpParams, RequestContext requestContext) @@ -263,10 +264,9 @@ protected async Task HandleShowOnlineHelpRequest( RequestContext requestContext ) { - const string deprecatedWarning = @" - Write-Warning ""'powerShell/showOnlineHelp' has been deprecated. Use 'powerShell/showHelp' instead."""; PSCommand commandDeprecated = new PSCommand() - .AddScript(deprecatedWarning); + .AddCommand("Microsoft.PowerShell.Utility\\Write-Verbose") + .AddParameter("Message", ";powerShell/showOnlineHelp' has been deprecated. Use 'powerShell/showHelp' instead."); await editorSession.PowerShellContext.ExecuteCommand(commandDeprecated, sendOutputToHost: true); await this.HandleShowHelpRequest(helpParams, requestContext); } From 004d53d7a6595e84a23dc0da1e5470c357304537 Mon Sep 17 00:00:00 2001 From: corbob Date: Thu, 20 Sep 2018 19:48:31 -0700 Subject: [PATCH 15/15] Fix Typo. Adjust whitespace. --- .../LanguageServer/ShowHelpRequest.cs | 1 + .../Server/LanguageServer.cs | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices.Protocol/LanguageServer/ShowHelpRequest.cs b/src/PowerShellEditorServices.Protocol/LanguageServer/ShowHelpRequest.cs index ef63bd2b7..bce097aa6 100644 --- a/src/PowerShellEditorServices.Protocol/LanguageServer/ShowHelpRequest.cs +++ b/src/PowerShellEditorServices.Protocol/LanguageServer/ShowHelpRequest.cs @@ -8,6 +8,7 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer { + [Obsolete("This class is deprecated. Use ShowHelpRequest instead.")] public class ShowOnlineHelpRequest { diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index 4e14b1c23..0ac51dd6b 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -123,6 +123,7 @@ public void Start() this.messageHandlers.SetRequestHandler(ShowOnlineHelpRequest.Type, this.HandleShowOnlineHelpRequest); this.messageHandlers.SetRequestHandler(ShowHelpRequest.Type, this.HandleShowHelpRequest); + this.messageHandlers.SetRequestHandler(ExpandAliasRequest.Type, this.HandleExpandAliasRequest); this.messageHandlers.SetRequestHandler(FindModuleRequest.Type, this.HandleFindModuleRequest); @@ -251,14 +252,17 @@ protected async Task HandleShowHelpRequest( } catch [System.Management.Automation.PSInvalidOperationException] { Microsoft.PowerShell.Core\Get-Help $CommandName -Full }"; + if (string.IsNullOrEmpty(helpParams)) { helpParams = "Get-Help"; } PSCommand checkHelpPSCommand = new PSCommand() .AddScript(CheckHelpScript, useLocalScope: true) .AddArgument(helpParams); + await editorSession.PowerShellContext.ExecuteCommand(checkHelpPSCommand, sendOutputToHost: true); await requestContext.SendResult(null); } + protected async Task HandleShowOnlineHelpRequest( string helpParams, RequestContext requestContext @@ -266,7 +270,8 @@ RequestContext requestContext { PSCommand commandDeprecated = new PSCommand() .AddCommand("Microsoft.PowerShell.Utility\\Write-Verbose") - .AddParameter("Message", ";powerShell/showOnlineHelp' has been deprecated. Use 'powerShell/showHelp' instead."); + .AddParameter("Message", "'powerShell/showOnlineHelp' has been deprecated. Use 'powerShell/showHelp' instead."); + await editorSession.PowerShellContext.ExecuteCommand(commandDeprecated, sendOutputToHost: true); await this.HandleShowHelpRequest(helpParams, requestContext); }