Add completion for variables assigned by command redirection (#25104)

This commit is contained in:
MartinGC94
2025-02-28 18:13:38 +05:00
committed by GitHub
parent 6fbd8ba1dd
commit 2c769d65e3
2 changed files with 55 additions and 0 deletions
@@ -5797,6 +5797,46 @@ namespace System.Management.Automation
}
}
foreach (RedirectionAst redirection in commandAst.Redirections)
{
if (redirection is FileRedirectionAst fileRedirection
&& fileRedirection.Location is StringConstantExpressionAst redirectTarget
&& redirectTarget.Value.StartsWith("variable:", StringComparison.OrdinalIgnoreCase)
&& redirectTarget.Value.Length > "variable:".Length)
{
string varName = redirectTarget.Value.Substring("variable:".Length);
PSTypeName varType;
switch (fileRedirection.FromStream)
{
case RedirectionStream.Error:
varType = new PSTypeName(typeof(ErrorRecord));
break;
case RedirectionStream.Warning:
varType = new PSTypeName(typeof(WarningRecord));
break;
case RedirectionStream.Verbose:
varType = new PSTypeName(typeof(VerboseRecord));
break;
case RedirectionStream.Debug:
varType = new PSTypeName(typeof(DebugRecord));
break;
case RedirectionStream.Information:
varType = new PSTypeName(typeof(InformationRecord));
break;
default:
varType = null;
break;
}
SaveVariableInfo(varName, varType, isConstraint: false);
}
}
return AstVisitAction.Continue;
}
@@ -436,6 +436,21 @@ switch ($x)
$res.CompletionMatches.Count | Should -Be 0
}
It 'Should complete variable assigned in command redirection to variable' {
$res = TabExpansion2 -inputScript 'New-Guid 1>variable:Redir1 2>variable:Redir2 3>variable:Redir3 4>variable:Redir4 5>variable:Redir5 6>variable:Redir6; $Redir'
$res.CompletionMatches[0].CompletionText | Should -Be '$Redir1'
$res.CompletionMatches[1].CompletionText | Should -Be '$Redir2'
$res.CompletionMatches[1].ToolTip | Should -Be '[ErrorRecord]$Redir2'
$res.CompletionMatches[2].CompletionText | Should -Be '$Redir3'
$res.CompletionMatches[2].ToolTip | Should -Be '[WarningRecord]$Redir3'
$res.CompletionMatches[3].CompletionText | Should -Be '$Redir4'
$res.CompletionMatches[3].ToolTip | Should -Be '[VerboseRecord]$Redir4'
$res.CompletionMatches[4].CompletionText | Should -Be '$Redir5'
$res.CompletionMatches[4].ToolTip | Should -Be '[DebugRecord]$Redir5'
$res.CompletionMatches[5].CompletionText | Should -Be '$Redir6'
$res.CompletionMatches[5].ToolTip | Should -Be '[InformationRecord]$Redir6'
}
context TypeConstructionWithHashtable {
BeforeAll {
class RandomTestType {