Skip to content

Commit 92086a5

Browse files
MartinGC94chrisdent-de
authored andcommitted
Don't complete parameter name and class member declarations (PowerShell#21182)
1 parent 45c693d commit 92086a5

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5200,6 +5200,12 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex
52005200

52015201
var lastAst = context.RelatedAsts?[^1];
52025202
var variableAst = lastAst as VariableExpressionAst;
5203+
if (lastAst is PropertyMemberAst ||
5204+
(lastAst is not null && lastAst.Parent is ParameterAst parameter && parameter.DefaultValue != lastAst))
5205+
{
5206+
// User is adding a new parameter or a class member, variable tab completion is not useful.
5207+
return results;
5208+
}
52035209
var prefix = variableAst != null && variableAst.Splatted ? "@" : "$";
52045210
bool tokenAtCursorUsedBraces = context.TokenAtCursor is not null && context.TokenAtCursor.Text.StartsWith("${");
52055211

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ Describe "TabCompletion" -Tags CI {
7979
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$CurrentItem'
8080
}
8181

82+
It 'Should not complete parameter name' {
83+
$res = TabExpansion2 -inputScript 'param($P'
84+
$res.CompletionMatches.Count | Should -Be 0
85+
}
86+
87+
It 'Should complete variable in default value of a parameter' {
88+
$res = TabExpansion2 -inputScript 'param($PS = $P'
89+
$res.CompletionMatches.Count | Should -BeGreaterThan 0
90+
}
91+
92+
It 'Should not complete property name in class definition' {
93+
$res = TabExpansion2 -inputScript 'class X {$P'
94+
$res.CompletionMatches.Count | Should -Be 0
95+
}
96+
8297
foreach ($Operator in [System.Management.Automation.CompletionCompleters]::CompleteOperator(""))
8398
{
8499
It "Should complete $($Operator.CompletionText)" {

0 commit comments

Comments
 (0)