8000 Exclude redundant parameter aliases from completion results (#19382) · PowerShell/PowerShell@18f0c5a · GitHub
[go: up one dir, main page]

Skip to content

Commit 18f0c5a

Browse files
authored
Exclude redundant parameter aliases from completion results (#19382)
1 parent 190c99a commit 18f0c5a

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -719,13 +719,21 @@ private static List<CompletionResult> GetParameterCompletionResults(string param
719719
string tooltip = parameterType + matchedParameterName;
720720
result.Add(new CompletionResult(completionText, matchedParameterName, CompletionResultType.ParameterName, tooltip));
721721
}
722-
723-
// Process alias when there is partial input
724-
result.AddRange(from alias in param.Parameter.Aliases
725-
where pattern.IsMatch(alias)
726-
select
727-
new CompletionResult("-" + alias + colonSuffix, alias, CompletionResultType.ParameterName,
728-
parameterType + alias));
722+
else
723+
{
724+
// Process alias when there is partial input
725+
foreach (var alias in param.Parameter.Aliases)
726+
{
727+
if (pattern.IsMatch(alias))
728+
{
729+
result.Add(new CompletionResult(
730+
$"-{alias}{colonSuffix}",
731+
alias,
732+
CompletionResultType.ParameterName,
733+
parameterType + alias));
734+
}
735+
}
736+
}
729737

730738
return result;
731739
}
@@ -791,15 +799,20 @@ private static List<CompletionResult> GetParameterCompletionResults(
791799
tooltip));
792800
}
793801
}
794-
795-
if (parameterName != string.Empty)
802+
else if (parameterName != string.Empty)
796803
{
797804
// Process alias when there is partial input
798-
listInUse.AddRange(from alias in param.Parameter.Aliases
799-
where pattern.IsMatch(alias)
800-
select
801-
new CompletionResult("-" + alias + colonSuffix, alias, CompletionResultType.ParameterName,
802-
type + alias));
805+
foreach (var alias in param.Parameter.Aliases)
806+
{
807+
if (pattern.IsMatch(alias))
808+
{
809+
listInUse.Add(new CompletionResult(
810+
$"-{alias}{colonSuffix}",
811+
alias,
812+
CompletionResultType.ParameterName,
813+
type + alias));
814+
}
815+
}
803816
}
804817
}
805818

test/powershell/Host/TabCompletion/BugFix.Tests.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ Describe "Tab completion bug fix" -Tags "CI" {
2828
It "Issue#1345 - 'Import-Module -n<tab>' should work" {
2929
$cmd = "Import-Module -n"
3030
$result = TabExpansion2 -inputScript $cmd -cursorColumn $cmd.Length
31-
$result.CompletionMatches | Should -HaveCount 3
31+
$result.CompletionMatches | Should -HaveCount 2
3232
$result.CompletionMatches[0].CompletionText | Should -BeExactly "-Name"
3333
$result.CompletionMatches[1].CompletionText | Should -BeExactly "-NoClobber"
34-
$result.CompletionMatches[2].CompletionText | Should -BeExactly "-NoOverwrite"
3534
}
3635

3736
It "Issue#11227 - [CompletionCompleters]::CompleteVariable and [CompletionCompleters]::CompleteType should work" {

test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,18 @@ class InheritedClassTest : System.Attribute
871871
$res.CompletionMatches.CompletionText | Should -Contain 'TypeId'
872872
}
873873

874+
it 'Should not complete parameter aliases if the real parameter is in the completion results' {
875+
$res = TabExpansion2 -inputScript 'Get-ChildItem -p'
876+
$res.CompletionMatches.CompletionText | Should -Not -Contain '-proga'
877+
$res.CompletionMatches.CompletionText | Should -Contain '-ProgressAction'
878+
}
879+
880+
it 'Should not complete parameter aliases if the real parameter is in the completion results (Non ambiguous parameters)' {
881+
$res = TabExpansion2 -inputScript 'Get-ChildItem -prog'
882+
$res.CompletionMatches.CompletionText | Should -Not -Contain '-proga'
883+
$res.CompletionMatches.CompletionText | Should -Contain '-ProgressAction'
884+
}
885+
874886
Context "Script name completion" {
875887
BeforeAll {
876888
Setup -f 'install-powershell.ps1' -Content ""
@@ -1646,15 +1658,15 @@ dir -Recurse `
16461658
It "Test completion with exact match" {
16471659
$inputStr = 'get-content -wa'
16481660
$res = TabExpansion2 -inputScript $inputStr -cursorColumn $inputStr.Length
1649-
$res.CompletionMatches | Should -HaveCount 4
1650-
[string]::Join(',', ($res.CompletionMatches.completiontext | Sort-Object)) | Should -BeExactly "-wa,-Wait,-WarningAction,-WarningVariable"
1661+
$res.CompletionMatches | Should -HaveCount 3
1662+
[string]::Join(',', ($res.CompletionMatches.completiontext | Sort-Object)) | Should -BeExactly "-Wait,-WarningAction,-WarningVariable"
16511663
}
16521664

16531665
It "Test completion with splatted variable" {
16541666
$inputStr = 'Get-Content @Splat -P'
16551667
$res = TabExpansion2 -inputScript $inputStr -cursorColumn $inputStr.Length
1656-
$res.CompletionMatches | Should -HaveCount 6
1657-
[string]::Join(',', ($res.CompletionMatches.completiontext | Sort-Object)) | Should -BeExactly "-Path,-PipelineVariab 5532 le,-proga,-ProgressAction,-PSPath,-pv"
1668+
$res.CompletionMatches | Should -HaveCount 4
1669+
[string]::Join(',', ($res.CompletionMatches.completiontext | Sort-Object)) | Should -BeExactly "-Path,-PipelineVariable,-ProgressAction,-PSPath"
16581670
}
16591671

16601672
It "Test completion for HttpVersion parameter name" {

0 commit comments

Comments
 (0)
0