10000 Fix tab completion for un-localized about topics (#15265) · PowerShell/PowerShell@5d8336a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d8336a

Browse files
authored
Fix tab completion for un-localized about topics (#15265)
1 parent a95b2cb commit 5d8336a

File tree

2 files changed

+39
-48
lines changed

2 files changed

+39
-48
lines changed

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

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6270,69 +6270,43 @@ private static string GetNamespaceToRemove(CompletionContext context, TypeComple
62706270
internal static List<CompletionResult> CompleteHelpTopics(CompletionContext context)
62716271
{
62726272
var results = new List<CompletionResult>();
6273-
var searchPaths = new List<string>();
6274-
var currentCulture = CultureInfo.CurrentCulture.Name;
6275-
6276-
// Add the user scope path first, since it is searched in order.
6277-
var userHelpRoot = Path.Combine(HelpUtils.GetUserHomeHelpSearchPath(), currentCulture);
6278-
6279-
if (Directory.Exists(userHelpRoot))
6280-
{
6281-
searchPaths.Add(userHelpRoot);
6282-
}
6283-
6284-
var dirPath = Path.Combine(Utils.GetApplicationBase(Utils.DefaultPowerShellShellID), currentCulture);
6285-
searchPaths.Add(dirPath);
6286-
6287-
var wordToComplete = context.WordToComplete + "*";
6288-
var topicPattern = WildcardPattern.Get("about_*.help.txt", WildcardOptions.IgnoreCase);
6289-
List<string> files = new List<string>();
6290-
6273+
string userHelpDir = HelpUtils.GetUserHomeHelpSearchPath();
6274+
string appHelpDir = Utils.GetApplicationBase(Utils.DefaultPowerShellShellID);
6275+
string currentCulture = CultureInfo.CurrentCulture.Name;
6276+
6277+
//search for help files for the current culture + en-US as fallback
6278+
var searchPaths = new string[]
6279+
{
6280+
Path.Combine(userHelpDir, currentCulture),
6281+
Path.Combine(appHelpDir, currentCulture),
6282+
Path.Combine(userHelpDir, "en-US"),
6283+
Path.Combine(appHelpDir, "en-US")
6284+
}.Distinct();
6285+
6286+
string wordToComplete = context.WordToComplete + "*";
62916287
try
62926288
{
62936289
var wildcardPattern = WildcardPattern.Get(wordToComplete, WildcardOptions.IgnoreCase);
62946290

62956291
foreach (var dir in searchPaths)
62966292
{
6297-
foreach (var file in Directory.EnumerateFiles(dir))
6293+
var currentDir = new DirectoryInfo(dir);
6294+
if (currentDir.Exists)
62986295
{
6299-
if (wildcardPattern.IsMatch(Path.GetFileName(file)))
6296+
foreach (var file in currentDir.EnumerateFiles("about_*.help.txt"))
63006297
{
6301-
files.Add(file);
6298+
if (wildcardPattern.IsMatch(file.Name))
6299+
{
6300+
string topicName = file.Name.Substring(0, file.Name.LastIndexOf(".help.txt"));
6301+
results.Add(new CompletionResult(topicName));
6302+
}
63026303
}
63036304
}
63046305
}
63056306
}
63066307
catch (Exception)
63076308
{
63086309
}
6309-
6310-
if (files != null)
6311-
{
6312-
foreach (string file in files)
6313-
{
6314-
if (file == null)
6315-
{
6316-
continue;
6317-
}
6318-
6319-
try
6320-
{
6321-
var fileName = Path.GetFileName(file);
6322-
if (fileName == null || !topicPattern.IsMatch(fileName))
6323-
continue;
6324-
6325-
// All topic files are ending with ".help.txt"
6326-
var completionText = fileName.Substring(0, fileName.Length - 9);
6327-
results.Add(new CompletionResult(completionText));
6328-
}
6329-
catch (Exception)
6330-
{
6331-
continue;
6332-
}
6333-
}
6334-
}
6335-
63366310
return results;
63376311
}
63386312

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,23 @@ dir -Recurse `
13711371
$res.CompletionMatches | Should -HaveCount $expectedCompletions
13721372
$res.CompletionMatches[0].CompletionText | Should -BeExactly 'about_Splatting'
13731373
}
1374+
1375+
It 'Should complete about help topic regardless of culture' {
1376+
try
1377+
{
1378+
## Save original culture and temporarily set it to da-DK because there's no localized help for da-DK.
1379+
$OriginalCulture = [cultureinfo]::CurrentCulture
1380+
[cultureinfo]::CurrentCulture="da-DK"
1381+
1382+
$res = TabExpansion2 -inputScript 'get-help about_spla' -cursorColumn 'get-help about_spla'.Length
1383+
$res.CompletionMatches | Should -HaveCount 1
1384+
$res.CompletionMatches[0].CompletionText | Should -BeExactly 'about_Splatting'
1385+
}
1386+
finally
1387+
{
1388+
[cultureinfo]::CurrentCulture = $OriginalCulture
1389+
}
1390+
}
13741391
}
13751392
}
13761393

0 commit comments

Comments
 (0)
0