Skip to content

Commit

Permalink
Exclude redundant parameter aliases from completion results (#19382)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinGC94 committed Mar 22, 2023
1 parent 190c99a commit 18f0c5a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -719,13 +719,21 @@ 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 +799,20 @@ 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,
type + alias));
foreach (var alias in param.Parameter.Aliases)
{
if (pattern.IsMatch(alias))
{
listInUse.Add(new CompletionResult(
$"-{alias}{colonSuffix}",
alias,
CompletionResultType.ParameterName,
type + alias));
}
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions test/powershell/Host/TabCompletion/BugFix.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ Describe "Tab completion bug fix" -Tags "CI" {
It "Issue#1345 - 'Import-Module -n<tab>' should work" {
$cmd = "Import-Module -n"
$result = TabExpansion2 -inputScript $cmd -cursorColumn $cmd.Length
$result.CompletionMatches | Should -HaveCount 3
$result.CompletionMatches | Should -HaveCount 2
$result.CompletionMatches[0].CompletionText | Should -BeExactly "-Name"
$result.CompletionMatches[1].CompletionText | Should -BeExactly "-NoClobber"
$result.CompletionMatches[2].CompletionText | Should -BeExactly "-NoOverwrite"
}

It "Issue#11227 - [CompletionCompleters]::CompleteVariable and [CompletionCompleters]::CompleteType should work" {
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

0 comments on commit 18f0c5a

Please sign in to comment.