8000 Add completion for variables assigned in ArrayLiterals and ParenExpre… · PowerShell/PowerShell@957eb1b · GitHub
[go: up one dir, main page]

Skip to content

Commit 957eb1b

Browse files
authored
Add completion for variables assigned in ArrayLiterals and ParenExpressions (#25303)
1 parent 1b786d2 commit 957eb1b

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

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

+27-8
Original file line numberDiff line numberDiff line change
@@ -5573,7 +5573,13 @@ public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst a
55735573
: AstVisitAction.StopVisit;
55745574
}
55755575

5576-
if (assignmentStatementAst.Left is AttributedExpressionAst attributedExpression)
5576+
ProcessAssignmentLeftSide(assignmentStatementAst.Left, assignmentStatementAst.Right);
5577+
return AstVisitAction.Continue;
5578+
}
5579+
5580+
private void ProcessAssignmentLeftSide(ExpressionAst left, StatementAst right)
5581+
{
5582+
if (left is AttributedExpressionAst attributedExpression)
55775583
{
55785584
var firstConvertExpression = attributedExpression as ConvertExpressionAst;
55795585
ExpressionAst child = attributedExpression.Child;
@@ -5593,7 +5599,7 @@ public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst a
55935599
{
55945600
if (variableExpression == CompletionVariableAst || s_specialVariablesCache.Value.Contains(variableExpression.VariablePath.UserPath))
55955601
{
5596-
return AstVisitAction.Continue;
5602+
return;
55975603
}
55985604

55995605
if (firstConvertExpression is not null)
@@ -5602,22 +5608,22 @@ public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst a
56025608
}
56035609
else
56045610
{
5605-
PSTypeName lastAssignedType = assignmentStatementAst.Right is CommandExpressionAst commandExpression
5611+
PSTypeName lastAssignedType = right is CommandExpressionAst commandExpression
56065612
? GetInferredVarTypeFromAst(commandExpression.Expression)
56075613
: null;
56085614
SaveVariableInfo(variableExpression.VariablePath.UnqualifiedPath, lastAssignedType, isConstraint: false);
56095615
}
56105616
}
56115617
}
5612-
else if (assignmentStatementAst.Left is VariableExpressionAst variableExpression)
5618+
else if (left is VariableExpressionAst variableExpression)
56135619
{
56145620
if (variableExpression == CompletionVariableAst || s_specialVariablesCache.Value.Contains(variableExpression.VariablePath.UserPath))
56155621
{
5616-
return AstVisitAction.Continue;
5622+
return;
56175623
}
56185624

56195625
PSTypeName lastAssignedType;
5620-
if (assignmentStatementAst.Right is CommandExpressionAst commandExpression)
5626+
if (right is CommandExpressionAst commandExpression)
56215627
{
56225628
lastAssignedType = GetInferredVarTypeFromAst(commandExpression.Expression);
56235629
}
@@ -5628,8 +5634,21 @@ public override AstVisitAction VisitAssignmentStatement(AssignmentStatementAst a
56285634

56295635
SaveVariableInfo(variableExpression.VariablePath.UnqualifiedPath, lastAssignedType, isConstraint: false);
56305636
}
5631-
5632-
return AstVisitAction.Continue;
5637+
else if (left is ArrayLiteralAst array)
5638+
{
5639+
foreach (ExpressionAst expression in array.Elements)
5640+
{
5641+
ProcessAssignmentLeftSide(expression, right);
5642+
}
5643+
}
5644+
else if (left is ParenExpressionAst parenExpression)
5645+
{
5646+
ExpressionAst pureExpression = parenExpression.Pipeline.GetPureExpression();
5647+
if (pureExpression is not null)
5648+
{
5649+
ProcessAssignmentLeftSide(pureExpression, right);
5650+
}
5651+
}
56335652
}
56345653

56355654
public override AstVisitAction VisitCommand(CommandAst commandAst)

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

+11
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,17 @@ param([ValidatePattern(
10841084
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$TestVar1'
10851085
}
10861086

1087+
It 'Should complete variable assigned in ParenExpression' {
1088+
$res = TabExpansion2 -inputScript '($ParenVar) = 1; $ParenVa'
1089+
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$ParenVar'
1090+
}
1091+
1092+
It 'Should complete variable assigned in ArrayLiteral' {
1093+
$res = TabExpansion2 -inputScript '$DemoVar1, $DemoVar2 = 1..10; $DemoVar'
1094+
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$DemoVar1'
1095+
$res.CompletionMatches[1].CompletionText | Should -BeExactly '$DemoVar2'
1096+
}
1097+
10871098
Context 'Start-Process -Verb parameter completion' {
10881099
BeforeAll {
10891100
function GetProcessInfoVerbs([string]$path, [switch]$singleQuote, [switch]$doubleQuote) {

0 commit comments

Comments
 (0)
0