Skip to content

Commit 11f4e9b

Browse files
corbobrjmholt
authored andcommitted
Change Get-Help behavior to return local help when no online help available (#721)
* Change from Online to Full * Also output to Host. This is an expiriment on vscode-powershell issue 884. * Added checking for RelatedLinks * If someone calls showOnlineHelp we will write a warning that it's deprecated and then call showHelp.
1 parent 042f2e0 commit 11f4e9b

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

src/PowerShellEditorServices.Protocol/LanguageServer/ShowOnlineHelpRequest.cs renamed to src/PowerShellEditorServices.Protocol/LanguageServer/ShowHelpRequest.cs

+9
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44
//
55

66
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
7+
using System;
78

89
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
910
{
11+
12+
[Obsolete("This class is deprecated. Use ShowHelpRequest instead.")]
1013
public class ShowOnlineHelpRequest
1114
{
1215
public static readonly
1316
RequestType<string, object, object, object> Type =
1417
RequestType<string, object, object, object>.Create("powerShell/showOnlineHelp");
1518
}
19+
public class ShowHelpRequest
20+
{
21+
public static readonly
22+
RequestType<string, object, object, object> Type =
23+
RequestType<string, object, object, object>.Create("powerShell/showHelp");
24+
}
1625
}

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+36-7
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public void Start()
133133
this.HandleDocumentRangeFormattingRequest);
134134

135135
this.messageHandlers.SetRequestHandler(ShowOnlineHelpRequest.Type, this.HandleShowOnlineHelpRequest);
136+
this.messageHandlers.SetRequestHandler(ShowHelpRequest.Type, this.HandleShowHelpRequest);
137+
136138
this.messageHandlers.SetRequestHandler(ExpandAliasRequest.Type, this.HandleExpandAliasRequest);
137139

138140
this.messageHandlers.SetRequestHandler(FindModuleRequest.Type, this.HandleFindModuleRequest);
@@ -242,22 +244,49 @@ await requestContext.SendResult(
242244
});
243245
}
244246

245-
protected async Task HandleShowOnlineHelpRequest(
247+
protected async Task HandleShowHelpRequest(
246248
string helpParams,
247249
RequestContext<object> requestContext)
248250
{
249-
if (helpParams == null) { helpParams = "get-help"; }
251+
const string CheckHelpScript = @"
252+
[CmdletBinding()]
253+
param (
254+
[String]$CommandName
255+
)
256+
try {
257+
$null = Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop
258+
} catch [System.Management.Automation.CommandNotFoundException] {
259+
$PSCmdlet.ThrowTerminatingError($PSItem)
260+
}
261+
try {
262+
$null = Microsoft.PowerShell.Core\Get-Help $CommandName -Online
263+
} catch [System.Management.Automation.PSInvalidOperationException] {
264+
Microsoft.PowerShell.Core\Get-Help $CommandName -Full
265+
}";
250266

251-
var psCommand = new PSCommand();
252-
psCommand.AddCommand("Get-Help");
253-
psCommand.AddArgument(helpParams);
254-
psCommand.AddParameter("Online");
267+
if (string.IsNullOrEmpty(helpParams)) { helpParams = "Get-Help"; }
255268

256-
await editorSession.PowerShellContext.ExecuteCommand<object>(psCommand);
269+
PSCommand checkHelpPSCommand = new PSCommand()
270+
.AddScript(CheckHelpScript, useLocalScope: true)
271+
.AddArgument(helpParams);
257272

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

277+
protected async Task HandleShowOnlineHelpRequest(
278+
string helpParams,
279+
RequestContext<object> requestContext
280+
)
281+
{
282+
PSCommand commandDeprecated = new PSCommand()
283+
.AddCommand("Microsoft.PowerShell.Utility\\Write-Verbose")
284+
.AddParameter("Message", "'powerShell/showOnlineHelp' has been deprecated. Use 'powerShell/showHelp' instead.");
285+
286+
await editorSession.PowerShellContext.ExecuteCommand<PSObject>(commandDeprecated, sendOutputToHost: true);
287+
await this.HandleShowHelpRequest(helpParams, requestContext);
288+
}
289+
261290
private async Task HandleSetPSSARulesRequest(
262291
object param,
263292
RequestContext<object> requestContext)

0 commit comments

Comments
 (0)