Skip to content

Commit 8eb37cc

Browse files
authored
Prevent braces from being removed when completing variables (PowerShell#17751)
1 parent 5ccbb97 commit 8eb37cc

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4789,6 +4789,7 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex
47894789
var lastAst = context.RelatedAsts?.Last();
47904790
var variableAst = lastAst as VariableExpressionAst;
47914791
var prefix = variableAst != null && variableAst.Splatted ? "@" : "$";
4792+
bool tokenAtCursorUsedBraces = context.TokenAtCursor is not null && context.TokenAtCursor.Text.StartsWith("${");
47924793

47934794
// Look for variables in the input (e.g. parameters, etc.) before checking session state - these
47944795
// variables might not exist in session state yet.
@@ -4934,7 +4935,7 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex
49344935
}
49354936
}
49364937

4937-
var completedName = (name.IndexOfAny(s_charactersRequiringQuotes) == -1)
4938+
var completedName = (!tokenAtCursorUsedBraces && name.IndexOfAny(s_charactersRequiringQuotes) == -1)
49384939
? prefix + provider + name
49394940
: prefix + "{" + provider + name + "}";
49404941
AddUniqueVariable(hashedResults, results, completedName, name, tooltip);
@@ -4957,7 +4958,7 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex
49574958
if (!string.IsNullOrEmpty(name))
49584959
{
49594960
name = "env:" + name;
4960-
var completedName = (name.IndexOfAny(s_charactersRequiringQuotes) == -1)
4961+
var completedName = (!tokenAtCursorUsedBraces && name.IndexOfAny(s_charactersRequiringQuotes) == -1)
49614962
? prefix + name
49624963
: prefix + "{" + name + "}";
49634964
AddUniqueVariable(hashedResults, results, completedName, name, "[string]" + name);
@@ -4972,7 +4973,7 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex
49724973
{
49734974
if (wildcardPattern.IsMatch(specialVariable))
49744975
{
4975-
var completedName = (specialVariable.IndexOfAny(s_charactersRequiringQuotes) == -1)
4976+
var completedName = (!tokenAtCursorUsedBraces && specialVariable.IndexOfAny(s_charactersRequiringQuotes) == -1)
49764977
? prefix + specialVariable
49774978
: prefix + "{" + specialVariable + "}";
49784979

@@ -4998,7 +4999,7 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex
49984999
var name = driveInfo.Name;
49995000
if (name != null && !string.IsNullOrWhiteSpace(name) && name.Length > 1)
50005001
{
5001-
var completedName = (name.IndexOfAny(s_charactersRequiringQuotes) == -1)
5002+
var completedName = (!tokenAtCursorUsedBraces && name.IndexOfAny(s_charactersRequiringQuotes) == -1)
50025003
? prefix + name + ":"
50035004
: prefix + "{" + name + ":}";
50045005

@@ -5014,7 +5015,7 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex
50145015
{
50155016
if (scopePattern.IsMatch(scope))
50165017
{
5017-
var completedName = (scope.IndexOfAny(s_charactersRequiringQuotes) == -1)
5018+
var completedName = (!tokenAtCursorUsedBraces && scope.IndexOfAny(s_charactersRequiringQuotes) == -1)
50185019
? prefix + scope
50195020
: prefix + "{" + scope + "}";
50205021
AddUniqueVariable(hashedResults, results, completedName, scope, scope);

test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,12 @@ switch ($x)
358358
$res.CompletionMatches[1].CompletionText | Should -BeExactly '-Functionality'
359359
}
360360

361+
It 'Should not remove braces when completing variable with braces' {
362+
$Text = '"Hello${psversiont}World"'
363+
$res = TabExpansion2 -inputScript $Text -cursorColumn $Text.IndexOf('p')
364+
$res.CompletionMatches[0].CompletionText | Should -BeExactly '${PSVersionTable}'
365+
}
366+
361367
It 'Should work for variable assignment of enum type: <inputStr>' -TestCases @(
362368
@{ inputStr = '$ErrorActionPreference = '; filter = ''; doubleQuotes = $false }
363369
@{ inputStr = '$ErrorActionPreference='; filter = ''; doubleQuotes = $false }
@@ -1109,7 +1115,7 @@ ConstructorTestClass(int i, bool b)
11091115
@{ inputStr = '[System.Management.Automation.Runspaces.runspacef'; expected = 'System.Management.Automation.Runspaces.RunspaceFactory'; setup = $null }
11101116
@{ inputStr = '[specialfol'; expected = 'System.Environment+SpecialFolder'; setup = $null }
11111117
## tab completion for variable names in '{}'
1112-
@{ inputStr = '${PSDefault'; expected = '$PSDefaultParameterValues'; setup = $null }
1118+
@{ inputStr = '${PSDefault'; expected = '${PSDefaultParameterValues}'; setup = $null }
11131119
)
11141120
}
11151121

0 commit comments

Comments
 (0)