Skip to content

Commit dd1fb91

Browse files
authored
Fix tab completion for env/function variables (PowerShell#25346)
1 parent f5da584 commit dd1fb91

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5331,11 +5331,11 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex
53315331
ToStringCodeMethods.Type(value.GetType(),
53325332
dropNamespaces: true), name);
53335333
}
5334-
}
53355334

5336-
if (!string.IsNullOrEmpty(variable.Description))
5337-
{
5338-
tooltip += $" - {variable.Description}";
5335+
if (!string.IsNullOrEmpty(variable.Description))
5336+
{
5337+
tooltip += $" - {variable.Description}";
5338+
}
53395339
}
53405340

53415341
var completedName = !tokenAtCursorUsedBraces && !ContainsCharactersRequiringQuotes(name)

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,46 @@ Describe "TabCompletion" -Tags CI {
150150
$res.CompletionMatches[0].ToolTip | Should -BeExactly $Expected
151151
}
152152

153+
It 'Should complete environment variable' {
154+
try {
155+
$env:PWSH_TEST_1 = 'value 1'
156+
$env:PWSH_TEST_2 = 'value 2'
157+
158+
$res = TabExpansion2 -inputScript '$env:PWSH_TEST_'
159+
$res.CompletionMatches.Count | Should -Be 2
160+
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$env:PWSH_TEST_1'
161+
$res.CompletionMatches[0].ListItemText | Should -BeExactly 'PWSH_TEST_1'
162+
$res.CompletionMatches[0].ToolTip | Should -BeExactly 'PWSH_TEST_1'
163+
$res.CompletionMatches[1].CompletionText | Should -BeExactly '$env:PWSH_TEST_2'
164+
$res.CompletionMatches[1].ListItemText | Should -BeExactly 'PWSH_TEST_2'
165+
$res.CompletionMatches[1].ToolTip | Should -BeExactly 'PWSH_TEST_2'
166+
}
167+
finally {
168+
$env:PWSH_TEST_1 = $null
169+
$env:PWSH_TEST_2 = $null
170+
}
171+
}
172+
173+
It 'Should complete function variable' {
174+
try {
175+
Function Test-PwshTest1 {}
176+
Function Test-PwshTest2 {}
177+
178+
$res = TabExpansion2 -inputScript '${function:Test-PwshTest'
179+
$res.CompletionMatches.Count | Should -Be 2
180+
$res.CompletionMatches[0].CompletionText | Should -BeExactly '${function:Test-PwshTest1}'
181+
$res.CompletionMatches[0].ListItemText | Should -BeExactly 'Test-PwshTest1'
182+
$res.CompletionMatches[0].ToolTip | Should -BeExactly 'Test-PwshTest1'
183+
$res.CompletionMatches[1].CompletionText | Should -BeExactly '${function:Test-PwshTest2}'
184+
$res.CompletionMatches[1].ListItemText | Should -BeExactly 'Test-PwshTest2'
185+
$res.CompletionMatches[1].ToolTip | Should -BeExactly 'Test-PwshTest2'
186+
}
187+
finally {
188+
Remove-Item function:Test-PwshTest1 -ErrorAction SilentlyContinue
189+
Remove-Item function:Test-PwshTest1 -ErrorAction SilentlyContinue
190+
}
191+
}
192+
153193
It 'Should complete scoped variable with description and value <Value>' -TestCases @(
154194
@{ Value = 1; Expected = '[int]$VariableWithDescription - Variable description' }
155195
@{ Value = 'string'; Expected = '[string]$VariableWithDescription - Variable description' }

0 commit comments

Comments
 (0)