Skip to content

Change Get-Help behavior to return local help when no online help available #721

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 15 commits into from
Sep 21, 2018
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@
//

using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using System;

namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
{

[Obsolete("This class is deprecated. Use ShowHelpRequest instead.")]
public class ShowOnlineHelpRequest
{
public static readonly
RequestType<string, object, object, object> Type =
RequestType<string, object, object, object>.Create("powerShell/showOnlineHelp");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a newline here?

public class ShowHelpRequest
{
public static readonly
RequestType<string, object, object, object> Type =
RequestType<string, object, object, object>.Create("powerShell/showHelp");
}
}
43 changes: 36 additions & 7 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public void Start()
this.HandleDocumentRangeFormattingRequest);

this.messageHandlers.SetRequestHandler(ShowOnlineHelpRequest.Type, this.HandleShowOnlineHelpRequest);
this.messageHandlers.SetRequestHandler(ShowHelpRequest.Type, this.HandleShowHelpRequest);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add newlines around this line to keep the style consistent?


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

this.messageHandlers.SetRequestHandler(FindModuleRequest.Type, this.HandleFindModuleRequest);
Expand Down Expand Up @@ -231,22 +233,49 @@ await requestContext.SendResult(
});
}

protected async Task HandleShowOnlineHelpRequest(
protected async Task HandleShowHelpRequest(
string helpParams,
RequestContext<object> requestContext)
{
if (helpParams == null) { helpParams = "get-help"; }
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
}";

var psCommand = new PSCommand();
psCommand.AddCommand("Get-Help");
psCommand.AddArgument(helpParams);
psCommand.AddParameter("Online");
if (string.IsNullOrEmpty(helpParams)) { helpParams = "Get-Help"; }

await editorSession.PowerShellContext.ExecuteCommand<object>(psCommand);
PSCommand checkHelpPSCommand = new PSCommand()
.AddScript(CheckHelpScript, useLocalScope: true)
.AddArgument(helpParams);

await editorSession.PowerShellContext.ExecuteCommand<PSObject>(checkHelpPSCommand, sendOutputToHost: true);
await requestContext.SendResult(null);
}

protected async Task HandleShowOnlineHelpRequest(
string helpParams,
RequestContext<object> requestContext
)
{
PSCommand commandDeprecated = new PSCommand()
.AddCommand("Microsoft.PowerShell.Utility\\Write-Verbose")
.AddParameter("Message", "'powerShell/showOnlineHelp' has been deprecated. Use 'powerShell/showHelp' instead.");

await editorSession.PowerShellContext.ExecuteCommand<PSObject>(commandDeprecated, sendOutputToHost: true);
await this.HandleShowHelpRequest(helpParams, requestContext);
}

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