Skip to content
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 @@ -5375,6 +5375,20 @@ private static void AddUniqueVariable(HashSet<string> hashedResults, List<Comple
"pv"
};

private static readonly HashSet<string> s_localScopeCommandNames = new(StringComparer.OrdinalIgnoreCase)
{
"Microsoft.PowerShell.Core\\ForEach-Object",
"ForEach-Object",
"foreach",
"%",
"Microsoft.PowerShell.Core\\Where-Object",
"Where-Object",
"where",
"?",
"BeforeAll",
"BeforeEach"
};

private sealed class VariableInfo
{
internal Type LastDeclaredConstraint;
Expand Down Expand Up @@ -5605,12 +5619,31 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun

public override AstVisitAction VisitScriptBlockExpression(ScriptBlockExpressionAst scriptBlockExpressionAst)
{
return scriptBlockExpressionAst != Top ? AstVisitAction.SkipChildren : AstVisitAction.Continue;
}
if (scriptBlockExpressionAst == Top)
{
return AstVisitAction.Continue;
}

public override AstVisitAction VisitScriptBlock(ScriptBlockAst scriptBlockAst)
{
return scriptBlockAst != Top ? AstVisitAction.SkipChildren : AstVisitAction.Continue;
Ast parent = scriptBlockExpressionAst.Parent;
// This loop checks if the scriptblock is used as a command, or an argument for a command, eg: ForEach-Object -Process {$Var1 = "Hello"}, {Var2 = $true}
while (true)
{
if (parent is CommandAst cmdAst)
{
string cmdName = cmdAst.GetCommandName();
return s_localScopeCommandNames.Contains(cmdName)
|| (cmdAst.CommandElements[0] is ScriptBlockExpressionAst && cmdAst.InvocationOperator == TokenKind.Dot)
? AstVisitAction.Continue
: AstVisitAction.SkipChildren;
}

if (parent is not CommandExpressionAst and not PipelineAst and not StatementBlockAst and not ArrayExpressionAst and not ArrayLiteralAst)
{
return AstVisitAction.SkipChildren;
}

parent = parent.Parent;
}
}
}

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 @@ -269,6 +269,21 @@ switch ($x)
$completionText -join ' ' | Should -BeExactly 'Ascending Descending Expression'
}

It 'Should complete variable assigned in other scriptblock' {
$res = TabExpansion2 -inputScript 'ForEach-Object -Begin {$Test1 = "Hello"} -Process {$Test'
$res.CompletionMatches[0].CompletionText | Should -Be '$Test1'
}

It 'Should complete variable assigned in an array of scriptblocks' {
$res = TabExpansion2 -inputScript 'ForEach-Object -Process @({"Block1"},{$Test1="Hello"});$Test'
$res.CompletionMatches[0].CompletionText | Should -Be '$Test1'
}

It 'Should not complete variable assigned in an ampersand executed scriptblock' {
$res = TabExpansion2 -inputScript '& {$AmpeersandVarCompletionTest = "Hello"};$AmpeersandVarCompletionTes'
$res.CompletionMatches.Count | Should -Be 0
}

context TypeConstructionWithHashtable {
BeforeAll {
class RandomTestType {
Expand Down