Skip to content

Commit b5f5e83

Browse files
refactor GetCommandSynopsisAsync method make sure cmdlets with module prefixes work
1 parent abf2496 commit b5f5e83

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/Utilities/CommandHelpers.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,36 +76,36 @@ public static async Task<string> GetCommandSynopsisAsync(
7676
CommandInfo commandInfo,
7777
PowerShellContextService powerShellContext)
7878
{
79+
Validate.IsNotNull(nameof(commandInfo), commandInfo);
7980
Validate.IsNotNull(nameof(powerShellContext), powerShellContext);
8081

81-
string synopsisString = string.Empty;
82-
83-
if (commandInfo != null &&
84-
(commandInfo.CommandType == CommandTypes.Cmdlet ||
85-
commandInfo.CommandType == CommandTypes.Function ||
86-
commandInfo.CommandType == CommandTypes.Filter))
82+
// A small optimization to not run Get-Help on things like DSC resources.
83+
if (commandInfo.CommandType != CommandTypes.Cmdlet &&
84+
commandInfo.CommandType != CommandTypes.Function &&
85+
commandInfo.CommandType != CommandTypes.Filter)
8786
{
88-
PSCommand command = new PSCommand();
89-
command.AddCommand(@"Microsoft.PowerShell.Core\Get-Help");
90-
command.AddArgument(commandInfo);
91-
command.AddParameter("ErrorAction", "Ignore");
87+
return string.Empty;
88+
}
9289

93-
var results = await powerShellContext.ExecuteCommandAsync<PSObject>(command, sendOutputToHost: false, sendErrorToHost: false).ConfigureAwait(false);
94-
PSObject helpObject = results.FirstOrDefault();
90+
PSCommand command = new PSCommand()
91+
.AddCommand(@"Microsoft.PowerShell.Core\Get-Help")
92+
// We use .Name here instead of just passing in commandInfo because
93+
// CommandInfo.ToString() duplicates the Prefix if one exists.
94+
.AddArgument(commandInfo.Name)
95+
.AddParameter("ErrorAction", "Ignore");
9596

96-
if (helpObject != null)
97-
{
98-
// Extract the synopsis string from the object
99-
synopsisString =
100-
(string)helpObject.Properties["synopsis"].Value ??
101-
string.Empty;
97+
var results = await powerShellContext.ExecuteCommandAsync<PSObject>(command, sendOutputToHost: false, sendErrorToHost: false).ConfigureAwait(false);
98+
PSObject helpObject = results.FirstOrDefault();
10299

103-
// Ignore the placeholder value for this field
104-
if (string.Equals(synopsisString, "SHORT DESCRIPTION", System.StringComparison.CurrentCultureIgnoreCase))
105-
{
106-
synopsisString = string.Empty;
107-
}
108-
}
100+
// Extract the synopsis string from the object
101+
string synopsisString =
102+
(string)helpObject?.Properties["synopsis"].Value ??
103+
string.Empty;
104+
105+
// Ignore the placeholder value for this field
106+
if (string.Equals(synopsisString, "SHORT DESCRIPTION", System.StringComparison.CurrentCultureIgnoreCase))
107+
{
108+
return string.Empty;
109109
}
110110

111111
return synopsisString;

test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,32 @@ public async Task CanSendCompletionAndCompletionResolveRequest()
816816
Assert.Contains("Writes customized output to a host", updatedCompletionItem.Documentation.String);
817817
}
818818

819+
[Fact]
820+
public async Task CanSGetHelpCompletionResolveWithPrefixRequest()
821+
{
822+
EvaluateResponseBody evaluateResponseBody =
823+
await LanguageClient.SendRequest<EvaluateResponseBody>(
824+
"evaluate",
825+
new EvaluateRequestArguments
826+
{
827+
Expression = "Import-Module Microsoft.PowerShell.Archive -Prefix Slow"
828+
});
829+
830+
string filePath = NewTestFile("Expand-SlowArch");
831+
832+
CompletionList completionItems = await LanguageClient.TextDocument.Completions(
833+
filePath, line: 0, column: 15);
834+
835+
CompletionItem completionItem = Assert.Single(completionItems,
836+
completionItem1 => completionItem1.Label == "Expand-SlowArchive");
837+
838+
CompletionItem updatedCompletionItem = await LanguageClient.SendRequest<CompletionItem>(
839+
"completionItem/resolve",
840+
completionItem);
841+
842+
Assert.Contains("Extracts files from a specified archive", updatedCompletionItem.Documentation.String);
843+
}
844+
819845
[Fact]
820846
public async Task CanSendHoverRequest()
821847
{

0 commit comments

Comments
 (0)