Prevent braces from being removed when completing variables (#17751)

This commit is contained in:
MartinGC94
2022-07-25 11:17:26 -07:00
committed by GitHub
parent 5ccbb97ae3
commit 8eb37cc44d
2 changed files with 13 additions and 6 deletions
@@ -4789,6 +4789,7 @@ namespace System.Management.Automation
var lastAst = context.RelatedAsts?.Last();
var variableAst = lastAst as VariableExpressionAst;
var prefix = variableAst != null && variableAst.Splatted ? "@" : "$";
bool tokenAtCursorUsedBraces = context.TokenAtCursor is not null && context.TokenAtCursor.Text.StartsWith("${");
// Look for variables in the input (e.g. parameters, etc.) before checking session state - these
// variables might not exist in session state yet.
@@ -4934,7 +4935,7 @@ namespace System.Management.Automation
}
}
var completedName = (name.IndexOfAny(s_charactersRequiringQuotes) == -1)
var completedName = (!tokenAtCursorUsedBraces && name.IndexOfAny(s_charactersRequiringQuotes) == -1)
? prefix + provider + name
: prefix + "{" + provider + name + "}";
AddUniqueVariable(hashedResults, results, completedName, name, tooltip);
@@ -4957,7 +4958,7 @@ namespace System.Management.Automation
if (!string.IsNullOrEmpty(name))
{
name = "env:" + name;
var completedName = (name.IndexOfAny(s_charactersRequiringQuotes) == -1)
var completedName = (!tokenAtCursorUsedBraces && name.IndexOfAny(s_charactersRequiringQuotes) == -1)
? prefix + name
: prefix + "{" + name + "}";
AddUniqueVariable(hashedResults, results, completedName, name, "[string]" + name);
@@ -4972,7 +4973,7 @@ namespace System.Management.Automation
{
if (wildcardPattern.IsMatch(specialVariable))
{
var completedName = (specialVariable.IndexOfAny(s_charactersRequiringQuotes) == -1)
var completedName = (!tokenAtCursorUsedBraces && specialVariable.IndexOfAny(s_charactersRequiringQuotes) == -1)
? prefix + specialVariable
: prefix + "{" + specialVariable + "}";
@@ -4998,7 +4999,7 @@ namespace System.Management.Automation
var name = driveInfo.Name;
if (name != null && !string.IsNullOrWhiteSpace(name) && name.Length > 1)
{
var completedName = (name.IndexOfAny(s_charactersRequiringQuotes) == -1)
var completedName = (!tokenAtCursorUsedBraces && name.IndexOfAny(s_charactersRequiringQuotes) == -1)
? prefix + name + ":"
: prefix + "{" + name + ":}";
@@ -5014,7 +5015,7 @@ namespace System.Management.Automation
{
if (scopePattern.IsMatch(scope))
{
var completedName = (scope.IndexOfAny(s_charactersRequiringQuotes) == -1)
var completedName = (!tokenAtCursorUsedBraces && scope.IndexOfAny(s_charactersRequiringQuotes) == -1)
? prefix + scope
: prefix + "{" + scope + "}";
AddUniqueVariable(hashedResults, results, completedName, scope, scope);
@@ -358,6 +358,12 @@ switch ($x)
$res.CompletionMatches[1].CompletionText | Should -BeExactly '-Functionality'
}
It 'Should not remove braces when completing variable with braces' {
$Text = '"Hello${psversiont}World"'
$res = TabExpansion2 -inputScript $Text -cursorColumn $Text.IndexOf('p')
$res.CompletionMatches[0].CompletionText | Should -BeExactly '${PSVersionTable}'
}
It 'Should work for variable assignment of enum type: <inputStr>' -TestCases @(
@{ inputStr = '$ErrorActionPreference = '; filter = ''; doubleQuotes = $false }
@{ inputStr = '$ErrorActionPreference='; filter = ''; doubleQuotes = $false }
@@ -1109,7 +1115,7 @@ ConstructorTestClass(int i, bool b)
@{ inputStr = '[System.Management.Automation.Runspaces.runspacef'; expected = 'System.Management.Automation.Runspaces.RunspaceFactory'; setup = $null }
@{ inputStr = '[specialfol'; expected = 'System.Environment+SpecialFolder'; setup = $null }
## tab completion for variable names in '{}'
@{ inputStr = '${PSDefault'; expected = '$PSDefaultParameterValues'; setup = $null }
@{ inputStr = '${PSDefault'; expected = '${PSDefaultParameterValues}'; setup = $null }
)
}