Skip to content

Don't complete parameter name declarations #21182

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 4 commits into from
Apr 15, 2024
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 @@ -5200,6 +5200,12 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex

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

Expand Down
15 changes: 15 additions & 0 deletions test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ Describe "TabCompletion" -Tags CI {
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$CurrentItem'
}

It 'Should not complete parameter name' {
$res = TabExpansion2 -inputScript 'param($P'
$res.CompletionMatches.Count | Should -Be 0
}

It 'Should complete variable in default value of a parameter' {
$res = TabExpansion2 -inputScript 'param($PS = $P'
$res.CompletionMatches.Count | Should -BeGreaterThan 0
}

It 'Should not complete property name in class definition' {
$res = TabExpansion2 -inputScript 'class X {$P'
$res.CompletionMatches.Count | Should -Be 0
}

foreach ($Operator in [System.Management.Automation.CompletionCompleters]::CompleteOperator(""))
{
It "Should complete $($Operator.CompletionText)" {
Expand Down