From 5d8336adf5f261aba00152cd9d2b15be7630e1c1 Mon Sep 17 00:00:00 2001 From: MartinGC94 <42123497+MartinGC94@users.noreply.github.com> Date: Wed, 5 May 2021 20:24:40 +0200 Subject: [PATCH] Fix tab completion for un-localized about topics (#15265) --- .../CommandCompletion/CompletionCompleters.cs | 70 ++++++------------- .../TabCompletion/TabCompletion.Tests.ps1 | 17 +++++ 2 files changed, 39 insertions(+), 48 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index 0b572ed9ebd..10f2bab1bce 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -6270,35 +6270,36 @@ private static string GetNamespaceToRemove(CompletionContext context, TypeComple internal static List CompleteHelpTopics(CompletionContext context) { var results = new List(); - var searchPaths = new List(); - var currentCulture = CultureInfo.CurrentCulture.Name; - - // Add the user scope path first, since it is searched in order. - var userHelpRoot = Path.Combine(HelpUtils.GetUserHomeHelpSearchPath(), currentCulture); - - if (Directory.Exists(userHelpRoot)) - { - searchPaths.Add(userHelpRoot); - } - - var dirPath = Path.Combine(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID), currentCulture); - searchPaths.Add(dirPath); - - var wordToComplete = context.WordToComplete + "*"; - var topicPattern = WildcardPattern.Get("about_*.help.txt", WildcardOptions.IgnoreCase); - List files = new List(); - + string userHelpDir = HelpUtils.GetUserHomeHelpSearchPath(); + string appHelpDir = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID); + string currentCulture = CultureInfo.CurrentCulture.Name; + + //search for help files for the current culture + en-US as fallback + var searchPaths = new string[] + { + Path.Combine(userHelpDir, currentCulture), + Path.Combine(appHelpDir, currentCulture), + Path.Combine(userHelpDir, "en-US"), + Path.Combine(appHelpDir, "en-US") + }.Distinct(); + + string wordToComplete = context.WordToComplete + "*"; try { var wildcardPattern = WildcardPattern.Get(wordToComplete, WildcardOptions.IgnoreCase); foreach (var dir in searchPaths) { - foreach (var file in Directory.EnumerateFiles(dir)) + var currentDir = new DirectoryInfo(dir); + if (currentDir.Exists) { - if (wildcardPattern.IsMatch(Path.GetFileName(file))) + foreach (var file in currentDir.EnumerateFiles("about_*.help.txt")) { - files.Add(file); + if (wildcardPattern.IsMatch(file.Name)) + { + string topicName = file.Name.Substring(0, file.Name.LastIndexOf(".help.txt")); + results.Add(new CompletionResult(topicName)); + } } } } @@ -6306,33 +6307,6 @@ internal static List CompleteHelpTopics(CompletionContext cont catch (Exception) { } - - if (files != null) - { - foreach (string file in files) - { - if (file == null) - { - continue; - } - - try - { - var fileName = Path.GetFileName(file); - if (fileName == null || !topicPattern.IsMatch(fileName)) - continue; - - // All topic files are ending with ".help.txt" - var completionText = fileName.Substring(0, fileName.Length - 9); - results.Add(new CompletionResult(completionText)); - } - catch (Exception) - { - continue; - } - } - } - return results; } diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index 5e18aa7d095..be18209101a 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -1371,6 +1371,23 @@ dir -Recurse ` $res.CompletionMatches | Should -HaveCount $expectedCompletions $res.CompletionMatches[0].CompletionText | Should -BeExactly 'about_Splatting' } + + It 'Should complete about help topic regardless of culture' { + try + { + ## Save original culture and temporarily set it to da-DK because there's no localized help for da-DK. + $OriginalCulture = [cultureinfo]::CurrentCulture + [cultureinfo]::CurrentCulture="da-DK" + + $res = TabExpansion2 -inputScript 'get-help about_spla' -cursorColumn 'get-help about_spla'.Length + $res.CompletionMatches | Should -HaveCount 1 + $res.CompletionMatches[0].CompletionText | Should -BeExactly 'about_Splatting' + } + finally + { + [cultureinfo]::CurrentCulture = $OriginalCulture + } + } } }