Skip to content

Commit

Permalink
Fix tab completion for un-localized about topics (PowerShell#15265)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinGC94 committed May 5, 2021
1 parent a95b2cb commit 5d8336a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6270,69 +6270,43 @@ private static string GetNamespaceToRemove(CompletionContext context, TypeComple
internal static List<CompletionResult> CompleteHelpTopics(CompletionContext context)
{
var results = new List<CompletionResult>();
var searchPaths = new List<string>();
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<string> files = new List<string>();

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));
}
}
}
}
}
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;
}

Expand Down
17 changes: 17 additions & 0 deletions test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}

Expand Down

0 comments on commit 5d8336a

Please sign in to comment.