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
28 changes: 19 additions & 9 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.Server
{
public class LanguageServer
{
private const string CheckHelpScript = @"
[CmdletBinding()]
param (
[String]$CommandName
)
Try {
Copy link
Contributor

Choose a reason for hiding this comment

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

Below you use a lower case try (and everywhere else uses lower case keywords). Probably should tweak that here to be consistent. :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll get this corrected after the below comment is replied to so I'm not doing a bunch of extra work :)

$null = Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 for module prefixed commands!

Copy link
Contributor

Choose a reason for hiding this comment

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

If this script is only used in the HandleShowOnlineHelpRequest method, then it should be moved inside that method as a const string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems to make sense. As was said in slack: if it's a const string it won't be stack alloc'd each time the methos is invoked. https://stackoverflow.com/questions/7616448/const-string-inside-a-function

Unless @tylerl0706 @SeeminglyScience or @rjmholt have any objections I'll move the string back into the function as const...

Copy link
Member

Choose a reason for hiding this comment

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

Reasonable to me! Let's hear what 1 other has to say

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep no issues with that 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved the string into the function as const

(also creeped the scope a bit by handling both commands that could come in (although not from vscode-powershell...) and marking one as deprecated.

} 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;
Expand Down Expand Up @@ -230,20 +245,15 @@ await requestContext.SendResult(
}
});
}

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

var psCommand = new PSCommand();
psCommand.AddCommand("Get-Help");
psCommand.AddArgument(helpParams);
psCommand.AddParameter("Online");

await editorSession.PowerShellContext.ExecuteCommand<object>(psCommand);
if (helpParams == null) { helpParams = "Get-Help"; }

PSCommand checkHelpPSCommand = new PSCommand();
checkHelpPSCommand.AddScript(CheckHelpScript, useLocalScope: true).AddArgument(helpParams);
await editorSession.PowerShellContext.ExecuteCommand<PSObject>(checkHelpPSCommand, sendOutputToHost: true);
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add a newline above the first await statement

await requestContext.SendResult(null);
}

Expand Down