Skip to content

Refactor GetCommandSynopsisAsync method make sure cmdlets with module prefixes work #1243

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,36 +76,36 @@ public static async Task<string> GetCommandSynopsisAsync(
CommandInfo commandInfo,
PowerShellContextService powerShellContext)
{
Validate.IsNotNull(nameof(commandInfo), commandInfo);
Copy link
Member Author

Choose a reason for hiding this comment

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

added this since all references checked commandInfo != null already

Validate.IsNotNull(nameof(powerShellContext), powerShellContext);

string synopsisString = string.Empty;

if (commandInfo != null &&
(commandInfo.CommandType == CommandTypes.Cmdlet ||
commandInfo.CommandType == CommandTypes.Function ||
commandInfo.CommandType == CommandTypes.Filter))
// A small optimization to not run Get-Help on things like DSC resources.
if (commandInfo.CommandType != CommandTypes.Cmdlet &&
commandInfo.CommandType != CommandTypes.Function &&
commandInfo.CommandType != CommandTypes.Filter)
{
PSCommand command = new PSCommand();
command.AddCommand(@"Microsoft.PowerShell.Core\Get-Help");
command.AddArgument(commandInfo);
command.AddParameter("ErrorAction", "Ignore");
return string.Empty;
}

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

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

// Ignore the placeholder value for this field
if (string.Equals(synopsisString, "SHORT DESCRIPTION", System.StringComparison.CurrentCultureIgnoreCase))
{
synopsisString = string.Empty;
}
}
// Extract the synopsis string from the object
string synopsisString =
(string)helpObject?.Properties["synopsis"].Value ??
string.Empty;

// Ignore the placeholder value for this field
if (string.Equals(synopsisString, "SHORT DESCRIPTION", System.StringComparison.CurrentCultureIgnoreCase))
{
return string.Empty;
}

return synopsisString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,31 @@ public async Task CanSendCompletionAndCompletionResolveRequest()
Assert.Contains("Writes customized output to a host", updatedCompletionItem.Documentation.String);
}

[Fact]
public async Task CanSendCompletionResolveWithModulePrefixRequest()
{
await LanguageClient.SendRequest<EvaluateResponseBody>(
"evaluate",
new EvaluateRequestArguments
{
Expression = "Import-Module Microsoft.PowerShell.Archive -Prefix Slow"
});

string filePath = NewTestFile("Expand-SlowArch");

CompletionList completionItems = await LanguageClient.TextDocument.Completions(
filePath, line: 0, column: 15);

CompletionItem completionItem = Assert.Single(completionItems,
completionItem1 => completionItem1.Label == "Expand-SlowArchive");

CompletionItem updatedCompletionItem = await LanguageClient.SendRequest<CompletionItem>(
"completionItem/resolve",
completionItem);

Assert.Contains("Extracts files from a specified archive", updatedCompletionItem.Documentation.String);
}

[Fact]
public async Task CanSendHoverRequest()
{
Expand Down