Fix TabExpansion2 variable leak when completing variables (#18763)

This commit is contained in:
MartinGC94
2023-06-19 15:48:59 -07:00
committed by GitHub
parent d614858550
commit 994e43b1bf
2 changed files with 16 additions and 30 deletions
@@ -513,43 +513,25 @@ namespace System.Management.Automation
List<CompletionResult> results = null;
{
/* BROKEN code commented out, fix sometime
// If we were invoked from TabExpansion2, we want to "remove" TabExpansion2 and anything it calls
// from our results. We do this by faking out the session so that TabExpansion2 isn't anywhere to be found.
MutableTuple tupleForFrameToSkipPast = null;
foreach (var stackEntry in context.Debugger.GetCallStack())
SessionStateScope scopeToRestore;
if (context.CurrentCommandProcessor.Command.CommandInfo.Name.Equals("TabExpansion2", StringComparison.OrdinalIgnoreCase)
&& context.CurrentCommandProcessor.UseLocalScope
&& context.EngineSessionState.CurrentScope.Parent is not null)
{
dynamic stackEntryAsPSObj = PSObject.AsPSObject(stackEntry);
if (stackEntryAsPSObj.Command.Equals("TabExpansion2", StringComparison.OrdinalIgnoreCase))
{
tupleForFrameToSkipPast = stackEntry.FunctionContext._localsTuple;
break;
}
}
SessionStateScope scopeToRestore = null;
if (tupleForFrameToSkipPast != null)
{
// Find this tuple in the scope stack.
scopeToRestore = context.EngineSessionState.CurrentScope;
var scope = context.EngineSessionState.CurrentScope;
while (scope != null && scope.LocalsTuple != tupleForFrameToSkipPast)
{
scope = scope.Parent;
}
if (scope != null)
{
context.EngineSessionState.CurrentScope = scope.Parent;
}
context.EngineSessionState.CurrentScope = scopeToRestore.Parent;
}
else
{
scopeToRestore = null;
}
try
{
*/
var completionAnalysis = new CompletionAnalysis(ast, tokens, positionOfCursor, options);
results = completionAnalysis.GetResults(powershell, out replacementIndex, out replacementLength);
/*
var completionAnalysis = new CompletionAnalysis(ast, tokens, positionOfCursor, options);
results = completionAnalysis.GetResults(powershell, out replacementIndex, out replacementLength);
}
finally
{
@@ -558,7 +540,6 @@ namespace System.Management.Automation
context.EngineSessionState.CurrentScope = scopeToRestore;
}
}
*/
}
var completionResults = results ?? EmptyCompletionResult;
@@ -952,6 +952,11 @@ Verb-Noun -Param1 Hello ^
$res.CompletionMatches[0].CompletionText | Should -Be "Get-ChildItem"
}
it 'Should not complete TabExpansion2 variables' {
$res = TabExpansion2 -inputScript '$' -cursorColumn 1
$res.CompletionMatches.CompletionText | Should -Not -Contain '$positionOfCursor'
}
it 'Should prefer the default parameterset when completing positional parameters' {
$ScriptInput = 'Get-ChildItem | Where-Object '
$res = TabExpansion2 -inputScript $ScriptInput -cursorColumn $ScriptInput.Length