Fix completion of the foreach-statement variable (#19814)

This commit is contained in:
MartinGC94
2023-06-19 10:20:13 -07:00
committed by GitHub
parent f86bf28554
commit 8b2de76eff
2 changed files with 16 additions and 0 deletions
@@ -5592,6 +5592,17 @@ namespace System.Management.Automation
return AstVisitAction.Continue;
}
public override AstVisitAction VisitForEachStatement(ForEachStatementAst forEachStatementAst)
{
if (forEachStatementAst.Extent.StartOffset > StopSearchOffset || forEachStatementAst.Variable == CompletionVariableAst)
{
return AstVisitAction.StopVisit;
}
SaveVariableInfo(forEachStatementAst.Variable.VariablePath.UserPath, variableType: null, isConstraint: false);
return AstVisitAction.Continue;
}
public override AstVisitAction VisitAttribute(AttributeAst attributeAst)
{
// Attributes can't assign values to variables so they aren't interesting.
@@ -74,6 +74,11 @@ Describe "TabCompletion" -Tags CI {
$res.CompletionMatches[0].CompletionText | Should -BeExactly 'pscustomobject'
}
It 'Should complete foreach variable' {
$res = TabExpansion2 -inputScript 'foreach ($CurrentItem in 1..10){$CurrentIt'
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$CurrentItem'
}
foreach ($Operator in [System.Management.Automation.CompletionCompleters]::CompleteOperator(""))
{
It "Should complete $($Operator.CompletionText)" {