Skip to content

Commit 4157a91

Browse files
Refactor GetCommandSynopsisAsync method make sure cmdlets with module prefixes work (#1243)
* refactor GetCommandSynopsisAsync method make sure cmdlets with module prefixes work * codacy * AddArgument -> AddParameter
1 parent 41bf246 commit 4157a91

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

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

+24-24
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+
.AddParameter("Name", 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

+25
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,31 @@ public async Task CanSendCompletionAndCompletionResolveRequest()
825825
Assert.Contains("Writes customized output to a host", updatedCompletionItem.Documentation.String);
826826
}
827827

828+
[Fact]
829+
public async Task CanSendCompletionResolveWithModulePrefixRequest()
830+
{
831+
await LanguageClient.SendRequest<EvaluateResponseBody>(
832+
"evaluate",
833+
new EvaluateRequestArguments
834+
{
835+
Expression = "Import-Module Microsoft.PowerShell.Archive -Prefix Slow"
836+
});
837+
838+
string filePath = NewTestFile("Expand-SlowArch");
839+
840+
CompletionList completionItems = await LanguageClient.TextDocument.Completions(
841+
filePath, line: 0, column: 15);
842+
843+
CompletionItem completionItem = Assert.Single(completionItems,
844+
completionItem1 => completionItem1.Label == "Expand-SlowArchive");
845+
846+
CompletionItem updatedCompletionItem = await LanguageClient.SendRequest<CompletionItem>(
847+
"completionItem/resolve",
848+
completionItem);
849+
850+
Assert.Contains("Extracts files from a specified archive", updatedCompletionItem.Documentation.String);
851+
}
852+
828853
[Fact]
829854
public async Task CanSendHoverRequest()
830855
{

0 commit comments

Comments
 (0)