From 9ba1a4e22b8d27c1ad46a40b13af625d7608c3ac Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Tue, 11 Jul 2023 00:10:31 +0200 Subject: [PATCH 1/3] Fix completion regression for filesystem paths with custom PSDrives --- .../CommandCompletion/CompletionCompleters.cs | 18 +++++++++++++++--- .../Host/TabCompletion/TabCompletion.Tests.ps1 | 7 +++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index 9dec8bcb3a5..c86e417c71e 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -4618,9 +4618,21 @@ private static List GetFileSystemProviderResults( string basePath; if (!relativePaths) { - basePath = dirInfo.FullName.EndsWith(provider.ItemSeparator) - ? providerPrefix + dirInfo.FullName - : providerPrefix + dirInfo.FullName + provider.ItemSeparator; + if (pathInfo.Drive is null) + { + basePath = pathInfo.ProviderPath; + } + else + { + int index = pathInfo.Drive.Root.EndsWith(provider.ItemSeparator) + ? pathInfo.Drive.Root.Length - 1 + : pathInfo.Drive.Root.Length; + basePath = string.Concat(pathInfo.Drive.Name, ":", pathInfo.ProviderPath.AsSpan(index)); + } + + basePath = basePath.EndsWith(provider.ItemSeparator) + ? providerPrefix + basePath + : providerPrefix + basePath + provider.ItemSeparator; basePath = RebuildPathWithVars(basePath, homePath, stringType, literalPaths, out baseQuotesNeeded); } else diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index ca159b798c3..4ae438e423d 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -1254,6 +1254,13 @@ class InheritedClassTest : System.Attribute } } + It 'Should keep custom drive names when completing file paths' { + $TempDriveName = "asdf" + $null = New-PSDrive -Name $TempDriveName -PSProvider FileSystem -Root $HOME + (TabExpansion2 -inputScript "${TempDriveName}:\").CompletionMatches[0].CompletionText | Should -BeLike "${TempDriveName}:*" + Remove-PSDrive -Name $TempDriveName + } + Context "Cmdlet name completion" { BeforeAll { $testCases = @( From b738464750413e01ad8d1122396bc1b1fcf064cc Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Tue, 11 Jul 2023 01:53:21 +0200 Subject: [PATCH 2/3] Dont add colon for drives that dont use colon. --- .../engine/CommandCompletion/CompletionCompleters.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index c86e417c71e..4e2fb46b8bb 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -4627,7 +4627,9 @@ private static List GetFileSystemProviderResults( int index = pathInfo.Drive.Root.EndsWith(provider.ItemSeparator) ? pathInfo.Drive.Root.Length - 1 : pathInfo.Drive.Root.Length; - basePath = string.Concat(pathInfo.Drive.Name, ":", pathInfo.ProviderPath.AsSpan(index)); + basePath = pathInfo.Drive.VolumeSeparatedByColon + ? string.Concat(pathInfo.Drive.Name, ":", pathInfo.ProviderPath.AsSpan(index)) + : string.Concat(pathInfo.Drive.Name, pathInfo.ProviderPath.AsSpan(index)); } basePath = basePath.EndsWith(provider.ItemSeparator) From d1bb3ba7f26e67a044b47ce659fbdd2b902299d0 Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Tue, 11 Jul 2023 11:36:57 +0200 Subject: [PATCH 3/3] Fix psdrive completion regression. --- .../CommandCompletion/CompletionCompleters.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index 4e2fb46b8bb..3de731faba4 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -4618,18 +4618,22 @@ private static List GetFileSystemProviderResults( string basePath; if (!relativePaths) { - if (pathInfo.Drive is null) + string providerName = $"{provider.ModuleName}\\{provider.Name}::"; + if (pathInfo.Path.StartsWith(providerName, StringComparison.OrdinalIgnoreCase)) { - basePath = pathInfo.ProviderPath; + basePath = pathInfo.Path.Substring(providerName.Length); } else { - int index = pathInfo.Drive.Root.EndsWith(provider.ItemSeparator) - ? pathInfo.Drive.Root.Length - 1 - : pathInfo.Drive.Root.Length; - basePath = pathInfo.Drive.VolumeSeparatedByColon - ? string.Concat(pathInfo.Drive.Name, ":", pathInfo.ProviderPath.AsSpan(index)) - : string.Concat(pathInfo.Drive.Name, pathInfo.ProviderPath.AsSpan(index)); + providerName = $"{provider.Name}::"; + if (pathInfo.Path.StartsWith(providerName, StringComparison.OrdinalIgnoreCase)) + { + basePath = pathInfo.Path.Substring(providerName.Length); + } + else + { + basePath = pathInfo.Path; + } } basePath = basePath.EndsWith(provider.ItemSeparator)