Skip to content
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

Exclude redundant parameter aliases from completion results #19382

Merged
merged 3 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -719,13 +719,18 @@ private static List<CompletionResult> GetParameterCompletionResults(string param
string tooltip = parameterType + matchedParameterName;
result.Add(new CompletionResult(completionText, matchedParameterName, CompletionResultType.ParameterName, tooltip));
}

// Process alias when there is partial input
result.AddRange(from alias in param.Parameter.Aliases
where pattern.IsMatch(alias)
select
new CompletionResult("-" + alias + colonSuffix, alias, CompletionResultType.ParameterName,
parameterType + alias));
else
{
// Process alias when there is partial input
foreach (var alias in param.Parameter.Aliases)
{
if (pattern.IsMatch(alias))
{
result.Add(new CompletionResult("-" + alias + colonSuffix, alias, CompletionResultType.ParameterName,
parameterType + alias));
}
}
}

return result;
}
Expand Down Expand Up @@ -791,15 +796,17 @@ where pattern.IsMatch(alias)
tooltip));
}
}

if (parameterName != string.Empty)
else if (parameterName != string.Empty)
{
// Process alias when there is partial input
listInUse.AddRange(from alias in param.Parameter.Aliases
where pattern.IsMatch(alias)
select
new CompletionResult("-" + alias + colonSuffix, alias, CompletionResultType.ParameterName,
foreach (var alias in param.Parameter.Aliases)
{
if (pattern.IsMatch(alias))
{
listInUse.Add(new CompletionResult("-" + alias + colonSuffix, alias, CompletionResultType.ParameterName,
type + alias));
}
}
}
}

Expand Down
20 changes: 16 additions & 4 deletions test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,18 @@ class InheritedClassTest : System.Attribute
$res.CompletionMatches.CompletionText | Should -Contain 'TypeId'
}

it 'Should not complete parameter aliases if the real parameter is in the completion results' {
$res = TabExpansion2 -inputScript 'Get-ChildItem -p'
$res.CompletionMatches.CompletionText | Should -Not -Contain '-proga'
$res.CompletionMatches.CompletionText | Should -Contain '-ProgressAction'
}

it 'Should not complete parameter aliases if the real parameter is in the completion results (Non ambiguous parameters)' {
$res = TabExpansion2 -inputScript 'Get-ChildItem -prog'
$res.CompletionMatches.CompletionText | Should -Not -Contain '-proga'
$res.CompletionMatches.CompletionText | Should -Contain '-ProgressAction'
}

Context "Script name completion" {
BeforeAll {
Setup -f 'install-powershell.ps1' -Content ""
Expand Down Expand Up @@ -1646,15 +1658,15 @@ dir -Recurse `
It "Test completion with exact match" {
$inputStr = 'get-content -wa'
$res = TabExpansion2 -inputScript $inputStr -cursorColumn $inputStr.Length
$res.CompletionMatches | Should -HaveCount 4
[string]::Join(',', ($res.CompletionMatches.completiontext | Sort-Object)) | Should -BeExactly "-wa,-Wait,-WarningAction,-WarningVariable"
$res.CompletionMatches | Should -HaveCount 3
[string]::Join(',', ($res.CompletionMatches.completiontext | Sort-Object)) | Should -BeExactly "-Wait,-WarningAction,-WarningVariable"
}

It "Test completion with splatted variable" {
$inputStr = 'Get-Content @Splat -P'
$res = TabExpansion2 -inputScript $inputStr -cursorColumn $inputStr.Length
$res.CompletionMatches | Should -HaveCount 6
[string]::Join(',', ($res.CompletionMatches.completiontext | Sort-Object)) | Should -BeExactly "-Path,-PipelineVariable,-proga,-ProgressAction,-PSPath,-pv"
$res.CompletionMatches | Should -HaveCount 4
[string]::Join(',', ($res.CompletionMatches.completiontext | Sort-Object)) | Should -BeExactly "-Path,-PipelineVariable,-ProgressAction,-PSPath"
}

It "Test completion for HttpVersion parameter name" {
Expand Down