Add completion for variables assigned in ArrayLiterals and ParenExpressions (#25303)

This commit is contained in:
MartinGC94
2025-05-08 18:17:40 +05:00
committed by GitHub
parent 1b786d2d89
commit 957eb1b279
2 changed files with 38 additions and 8 deletions
@@ -5573,7 +5573,13 @@ namespace System.Management.Automation
: AstVisitAction.StopVisit;
}
if (assignmentStatementAst.Left is AttributedExpressionAst attributedExpression)
ProcessAssignmentLeftSide(assignmentStatementAst.Left, assignmentStatementAst.Right);
return AstVisitAction.Continue;
}
private void ProcessAssignmentLeftSide(ExpressionAst left, StatementAst right)
{
if (left is AttributedExpressionAst attributedExpression)
{
var firstConvertExpression = attributedExpression as ConvertExpressionAst;
ExpressionAst child = attributedExpression.Child;
@@ -5593,7 +5599,7 @@ namespace System.Management.Automation
{
if (variableExpression == CompletionVariableAst || s_specialVariablesCache.Value.Contains(variableExpression.VariablePath.UserPath))
{
return AstVisitAction.Continue;
return;
}
if (firstConvertExpression is not null)
@@ -5602,22 +5608,22 @@ namespace System.Management.Automation
}
else
{
PSTypeName lastAssignedType = assignmentStatementAst.Right is CommandExpressionAst commandExpression
PSTypeName lastAssignedType = right is CommandExpressionAst commandExpression
? GetInferredVarTypeFromAst(commandExpression.Expression)
: null;
SaveVariableInfo(variableExpression.VariablePath.UnqualifiedPath, lastAssignedType, isConstraint: false);
}
}
}
else if (assignmentStatementAst.Left is VariableExpressionAst variableExpression)
else if (left is VariableExpressionAst variableExpression)
{
if (variableExpression == CompletionVariableAst || s_specialVariablesCache.Value.Contains(variableExpression.VariablePath.UserPath))
{
return AstVisitAction.Continue;
return;
}
PSTypeName lastAssignedType;
if (assignmentStatementAst.Right is CommandExpressionAst commandExpression)
if (right is CommandExpressionAst commandExpression)
{
lastAssignedType = GetInferredVarTypeFromAst(commandExpression.Expression);
}
@@ -5628,8 +5634,21 @@ namespace System.Management.Automation
SaveVariableInfo(variableExpression.VariablePath.UnqualifiedPath, lastAssignedType, isConstraint: false);
}
return AstVisitAction.Continue;
else if (left is ArrayLiteralAst array)
{
foreach (ExpressionAst expression in array.Elements)
{
ProcessAssignmentLeftSide(expression, right);
}
}
else if (left is ParenExpressionAst parenExpression)
{
ExpressionAst pureExpression = parenExpression.Pipeline.GetPureExpression();
if (pureExpression is not null)
{
ProcessAssignmentLeftSide(pureExpression, right);
}
}
}
public override AstVisitAction VisitCommand(CommandAst commandAst)
@@ -1084,6 +1084,17 @@ param([ValidatePattern(
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$TestVar1'
}
It 'Should complete variable assigned in ParenExpression' {
$res = TabExpansion2 -inputScript '($ParenVar) = 1; $ParenVa'
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$ParenVar'
}
It 'Should complete variable assigned in ArrayLiteral' {
$res = TabExpansion2 -inputScript '$DemoVar1, $DemoVar2 = 1..10; $DemoVar'
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$DemoVar1'
$res.CompletionMatches[1].CompletionText | Should -BeExactly '$DemoVar2'
}
Context 'Start-Process -Verb parameter completion' {
BeforeAll {
function GetProcessInfoVerbs([string]$path, [switch]$singleQuote, [switch]$doubleQuote) {