Skip to content

Commit

Permalink
Don't complete parameter name and class member declarations (PowerShe…
Browse files Browse the repository at this point in the history
MartinGC94 authored and chrisdent-de committed Sep 12, 2024
1 parent 45c693d commit 92086a5
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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("${");

15 changes: 15 additions & 0 deletions test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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)" {

0 comments on commit 92086a5

Please sign in to comment.