Skip to content

Commit 05214da

Browse files
authored
Fix tooltip for variable expansion and include desc (#25112)
1 parent 33ed509 commit 05214da

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5384,10 +5384,15 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex
53845384
var toolTip = value is null
53855385
? key
53865386
: StringUtil.Format("[{0}]${1}", ToStringCodeMethods.Type(value.GetType(), dropNamespaces: true), key);
5387+
if (!string.IsNullOrEmpty(variable.Description))
5388+
{
5389+
toolTip += $" - {variable.Description}";
5390+
}
5391+
53875392
var completionText = !tokenAtCursorUsedBraces && !ContainsCharactersRequiringQuotes(name)
53885393
? prefix + name
53895394
: prefix + "{" + name + "}";
5390-
AddUniqueVariable(hashedResults, tempResults, completionText, key, key);
5395+
AddUniqueVariable(hashedResults, tempResults, completionText, key, toolTip);
53915396
}
53925397
}
53935398

@@ -5436,6 +5441,11 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex
54365441
}
54375442
}
54385443

5444+
if (!string.IsNullOrEmpty(variable.Description))
5445+
{
5446+
tooltip += $" - {variable.Description}";
5447+
}
5448+
54395449
var completedName = !tokenAtCursorUsedBraces && !ContainsCharactersRequiringQuotes(name)
54405450
? prefix + scopePrefix + name
54415451
: prefix + "{" + scopePrefix + name + "}";

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,34 @@ Describe "TabCompletion" -Tags CI {
104104
$res = TabExpansion2 -inputScript 'param($PS = $P'
105105
$res.CompletionMatches.Count | Should -BeGreaterThan 0
106106
}
107+
108+
It 'Should complete variable with description and value <Value>' -TestCases @(
109+
@{ Value = 1; Expected = '[int]$VariableWithDescription - Variable description' }
110+
@{ Value = 'string'; Expected = '[string]$VariableWithDescription - Variable description' }
111+
@{ Value = $null; Expected = 'VariableWithDescription - Variable description' }
112+
) {
113+
param ($Value, $Expected)
114+
115+
New-Variable -Name VariableWithDescription -Value $Value -Description 'Variable description' -Force
116+
$res = TabExpansion2 -inputScript '$VariableWithDescription'
117+
$res.CompletionMatches.Count | Should -Be 1
118+
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$VariableWithDescription'
119+
$res.CompletionMatches[0].ToolTip | Should -BeExactly $Expected
120+
}
121+
122+
It 'Should complete scoped variable with description and value <Value>' -TestCases @(
123+
@{ Value = 1; Expected = '[int]$VariableWithDescription - Variable description' }
124+
@{ Value = 'string'; Expected = '[string]$VariableWithDescription - Variable description' }
125+
@{ Value = $null; Expected = 'VariableWithDescription - Variable description' }
126+
) {
127+
param ($Value, $Expected)
128+
129+
New-Variable -Name VariableWithDescription -Value $Value -Description 'Variable description' -Force
130+
$res = TabExpansion2 -inputScript '$local:VariableWithDescription'
131+
$res.CompletionMatches.Count | Should -Be 1
132+
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$local:VariableWithDescription'
133+
$res.CompletionMatches[0].ToolTip | Should -BeExactly $Expected
134+
}
107135

108136
It 'Should not complete property name in class definition' {
109137
$res = TabExpansion2 -inputScript 'class X {$P'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy