diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index e86e87e137..990771e6b7 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -5384,10 +5384,15 @@ namespace System.Management.Automation var toolTip = value is null ? key : StringUtil.Format("[{0}]${1}", ToStringCodeMethods.Type(value.GetType(), dropNamespaces: true), key); + if (!string.IsNullOrEmpty(variable.Description)) + { + toolTip += $" - {variable.Description}"; + } + var completionText = !tokenAtCursorUsedBraces && !ContainsCharactersRequiringQuotes(name) ? prefix + name : prefix + "{" + name + "}"; - AddUniqueVariable(hashedResults, tempResults, completionText, key, key); + AddUniqueVariable(hashedResults, tempResults, completionText, key, toolTip); } } @@ -5436,6 +5441,11 @@ namespace System.Management.Automation } } + if (!string.IsNullOrEmpty(variable.Description)) + { + tooltip += $" - {variable.Description}"; + } + var completedName = !tokenAtCursorUsedBraces && !ContainsCharactersRequiringQuotes(name) ? prefix + scopePrefix + name : prefix + "{" + scopePrefix + name + "}"; diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index d35c404548..76055965ce 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -104,6 +104,34 @@ Describe "TabCompletion" -Tags CI { $res = TabExpansion2 -inputScript 'param($PS = $P' $res.CompletionMatches.Count | Should -BeGreaterThan 0 } + + It 'Should complete variable with description and value ' -TestCases @( + @{ Value = 1; Expected = '[int]$VariableWithDescription - Variable description' } + @{ Value = 'string'; Expected = '[string]$VariableWithDescription - Variable description' } + @{ Value = $null; Expected = 'VariableWithDescription - Variable description' } + ) { + param ($Value, $Expected) + + New-Variable -Name VariableWithDescription -Value $Value -Description 'Variable description' -Force + $res = TabExpansion2 -inputScript '$VariableWithDescription' + $res.CompletionMatches.Count | Should -Be 1 + $res.CompletionMatches[0].CompletionText | Should -BeExactly '$VariableWithDescription' + $res.CompletionMatches[0].ToolTip | Should -BeExactly $Expected + } + + It 'Should complete scoped variable with description and value ' -TestCases @( + @{ Value = 1; Expected = '[int]$VariableWithDescription - Variable description' } + @{ Value = 'string'; Expected = '[string]$VariableWithDescription - Variable description' } + @{ Value = $null; Expected = 'VariableWithDescription - Variable description' } + ) { + param ($Value, $Expected) + + New-Variable -Name VariableWithDescription -Value $Value -Description 'Variable description' -Force + $res = TabExpansion2 -inputScript '$local:VariableWithDescription' + $res.CompletionMatches.Count | Should -Be 1 + $res.CompletionMatches[0].CompletionText | Should -BeExactly '$local:VariableWithDescription' + $res.CompletionMatches[0].ToolTip | Should -BeExactly $Expected + } It 'Should not complete property name in class definition' { $res = TabExpansion2 -inputScript 'class X {$P'