Skip to content

Commit

Permalink
Expand ~ to $home on Windows with tab completion (#21529)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveL-MSFT committed Apr 24, 2024
1 parent 0c64fe1 commit 5efd627
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Expand Up @@ -4925,12 +4925,15 @@ private static string GetChildNameFromPsObject(dynamic psObject, char separator)

for (int i = 0; i < path.Length; i++)
{
// on Windows, we need to preserve the expanded home path as native commands don't understand it
#if UNIX
if (i == homeIndex)
{
_ = sb.Append('~');
i += homePath.Length - 1;
continue;
}
#endif

EscapeCharIfNeeded(sb, path, i, stringType, literalPath, useSingleQuoteEscapeRules, ref quotesAreNeeded);
_ = sb.Append(path[i]);
Expand Down
8 changes: 6 additions & 2 deletions test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Expand Up @@ -1409,12 +1409,16 @@ class InheritedClassTest : System.Attribute
$res.CompletionMatches[0].CompletionText | Should -Be "`"$expectedPath`""
}

It "Should keep '~' in completiontext when it's used to refer to home in input" {
It "Should handle '~' in completiontext when it's used to refer to home in input" {
$res = TabExpansion2 -inputScript "~$separator"
# select the first answer which does not have a space in the completion (those completions look like & '3D Objects')
$observedResult = $res.CompletionMatches.Where({$_.CompletionText.IndexOf("&") -eq -1})[0].CompletionText
$completedText = $res.CompletionMatches.CompletionText -join ","
$observedResult | Should -BeLike "~$separator*" -Because "$completedText"
if ($IsWindows) {
$observedResult | Should -BeLike "$home$separator*" -Because "$completedText"
} else {
$observedResult | Should -BeLike "~$separator*" -Because "$completedText"
}
}

It "Should use '~' as relative filter text when not followed by separator" {
Expand Down

0 comments on commit 5efd627

Please sign in to comment.