8000 Add tooltips for hashtable key completions (#17864) · PowerShell/PowerShell@1ac3ff1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1ac3ff1

Browse files
authored
Add tooltips for hashtable key completions (#17864)
1 parent e308222 commit 1ac3ff1

File tree

3 files changed

+364
-71
lines changed

3 files changed

+364
-71
lines changed

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

+134-54
Original file line numberDiff line numberDiff line change
@@ -5989,12 +5989,13 @@ private static List<CompletionResult> CompleteRequires(CompletionContext context
59895989

59905990
// Produce completions for all parameters that begin with the prefix we've found,
59915991
// but which haven't already been specified in the line we need to complete
5992-
foreach (KeyValuePair<string, string> parameter in s_requiresParameters)
5992+
foreach (string parameter in s_requiresParameters)
59935993
{
5994-
if (parameter.Key.StartsWith(currentParameterPrefix, StringComparison.OrdinalIgnoreCase)
5995-
&& !context.CursorPosition.Line.Contains($" -{parameter.Key}", StringComparison.OrdinalIgnoreCase))
5994+
if (parameter.StartsWith(currentParameterPrefix, StringComparison.OrdinalIgnoreCase)
5995+
&& !context.CursorPosition.Line.Contains($" -{parameter}", StringComparison.OrdinalIgnoreCase))
59965996
{
5997-
results.Add(new CompletionResult(parameter.Key, parameter.Key, CompletionResultType.ParameterName, parameter.Value));
5997+
string toolTip = GetRequiresParametersToolTip(parameter);
5998+
results.Add(new CompletionResult(parameter, parameter, CompletionResultType.ParameterName, toolTip));
59985999
}
59996000
}
60006001

@@ -6019,11 +6020,12 @@ private static List<CompletionResult> CompleteRequires(CompletionContext context
60196020
// Complete PSEdition parameter values
60206021
if (currentParameterMatch.Value.Equals("PSEdition", StringComparison.OrdinalIgnoreCase))
60216022
{
6022-
foreach (KeyValuePair<string, string> psEditionEntry in s_requiresPSEditions)
6023+
foreach (string psEditionEntry in s_requiresPSEditions)
60236024
{
6024-
if (psEditionEntry.Key.StartsWith(currentValue, StringComparison.OrdinalIgnoreCase))
6025+
if (psEditionEntry.StartsWith(currentValue, StringComparison.OrdinalIgnoreCase))
60256026
{
6026-
results.Add(new CompletionResult(psEditionEntry.Key, psEditionEntry.Key, CompletionResultType.ParameterValue, psEditionEntry.Value));
6027+
string toolTip = GetRequiresPsEditionsToolTip(psEditionEntry);
6028+
results.Add(new CompletionResult(psEditionEntry, psEditionEntry, CompletionResultType.ParameterValue, toolTip));
60276029
}
60286030
}
60296031

@@ -6051,7 +6053,7 @@ private static List<CompletionResult> CompleteRequires(CompletionContext context
60516053
hashtableKeyMatches = Regex.Matches(hashtableString, @"(@{|;)\s*(?:'|\""|\w*)\w*");
60526054

60536055
// Build the list of keys we might want to complete, based on what's already been provided
6054-
var moduleSpecKeysToComplete = new HashSet<string>(s_requiresModuleSpecKeys.Keys);
6056+
var moduleSpecKeysToComplete = new HashSet<string>(s_requiresModuleSpecKeys);
60556057
bool sawModuleNameLast = false;
60566058
foreach (Match existingHashtableKeyMatch in hashtableKeyMatches)
60576059
{
@@ -6107,35 +6109,62 @@ private static List<CompletionResult> CompleteRequires(CompletionContext context
61076109
{
61086110
if (moduleSpecKey.StartsWith(currentValue, StringComparison.OrdinalIgnoreCase))
61096111
{
6110-
results.Add(new CompletionResult(moduleSpecKey, moduleSpecKey, CompletionResultType.ParameterValue, s_requiresModuleSpecKeys[moduleSpecKey]));
6112+
string toolTip = GetRequiresModuleSpecKeysToolTip(moduleSpecKey);
6113+
results.Add(new CompletionResult(moduleSpecKey, moduleSpecKey, CompletionResultType.ParameterValue, toolTip));
61116114
}
61126115
}
61136116
}
61146117

61156118
return results;
61166119
}
61176120

6118-
private static readonly IReadOnlyDictionary<string, string> s_requiresParameters = new SortedList<string, string>(StringComparer.OrdinalIgnoreCase)
6121+
private static readonly string[] s_requiresParameters = new string[]
61196122
{
6120-
{ "Modules", "Specifies PowerShell modules that the script requires." },
6121-
{ "PSEdition", "Specifies a PowerShell edition that the script requires." },
6122-
{ "RunAsAdministrator", "Specifies that PowerShell must be running as administrator on Windows." },
6123-
{ "Version", "Specifies the minimum version of PowerShell that the script requires." },
6123+
"Modules",
6124+
"PSEdition",
6125+
"RunAsAdministrator",
6126+
"Version"
61246127
};
61256128

6126-
private static readonly IReadOnlyDictionary<string, string> s_requiresPSEditions = new SortedList<string, string>(StringComparer.OrdinalIgnoreCase)
6129+
private static string GetRequiresParametersToolTip(string name) => name switch
61276130
{
6128-
{ "Core", "Specifies that the script requires PowerShell Core to run." },
6129-
{ "Desktop", "Specifies that the script requires Windows PowerShell to run." },
6131+
"Modules" => TabCompletionStrings.RequiresModulesParameterDescription,
6132+
"PSEdition" => TabCompletionStrings.RequiresPSEditionParameterDescription,
6133+
"RunAsAdministrator" => TabCompletionStrings.RequiresRunAsAdministratorParameterDescription,
6134+
"Version" => TabCompletionStrings.RequiresVersionParameterDescription,
6135+
_ => string.Empty
61306136
};
61316137

6132-
private static readonly IReadOnlyDictionary<string, string> s_requiresModuleSpecKeys = new SortedList<string, string>(StringComparer.OrdinalIgnoreCase)
6138+
private static readonly string[] s_requiresPSEditions = new string[]
61336139
{
6134-
{ "ModuleName", "Required. Specifies the module name." },
6135-
{ "GUID", "Optional. Specifies the GUID of the module." },
6136-
{ "ModuleVersion", "Specifies a minimum acceptable version of the module." },
6137-
{ "RequiredVersion", "Specifies an exact, required version of the module." },
6138-
{ "MaximumVersion", "Specifies the maximum acceptable version of the module." },
6140+
"Core",
6141+
"Desktop"
6142+
};
6143+
6144+
private static string GetRequiresPsEditionsToolTip(string name) => name switch
6145+
{
6146+
"Core" => TabCompletionStrings.RequiresPsEditionCoreDescription,
6147+
"Desktop" => TabCompletionStrings.RequiresPsEditionDesktopDescription,
6148+
_ => string.Empty
6149+
};
6150+
6151+
private static readonly string[] s_requiresModuleSpecKeys = new string[]
6152+
{
6153+
"GUID",
6154+
"MaximumVersion",
6155+
"ModuleName",
6156+
"ModuleVersion",
6157+
"RequiredVersion"
6158+
};
6159+
6160+
private static string GetRequiresModuleSpecKeysToolTip(string name) => name switch
6161+
{
6162+
"GUID" => TabCompletionStrings.RequiresModuleSpecGUIDDescription,
6163+
"MaximumVersion" => TabCompletionStrings.RequiresModuleSpecMaximumVersionDescription,
6164+
"ModuleName" => TabCompletionStrings.RequiresModuleSpecModuleNameDescription,
6165+
"ModuleVersion" => TabCompletionStrings.RequiresModuleSpecModuleVersionDescription,
6166+
"RequiredVersion" => TabCompletionStrings.RequiresModuleSpecRequiredVersionDescription,
6167+
_ => string.Empty
61396168
};
61406169

61416170
private static readonly char[] s_hashtableKeyPrefixes = new[]
@@ -6180,7 +6209,7 @@ private static List<CompletionResult> CompleteCommentHelp(CompletionContext cont
61806209
replacementIndex = context.TokenAtCursor.Extent.StartOffset + lineKeyword.Index;
61816210
replacementLength = lineKeyword.Value.Length;
61826211

6183-
var validKeywords = new HashSet<String>(s_commentHelpKeywords.Keys, StringComparer.OrdinalIgnoreCase);
6212+
var validKeywords = new HashSet<string>(s_commentHelpKeywords, StringComparer.OrdinalIgnoreCase);
61846213
foreach (Match keyword in usedKeywords)
61856214
{
61866215
if (keyword == lineKeyword || s_commentHelpAllowedDuplicateKeywords.Contains(keyword.Value))
@@ -6196,7 +6225,8 @@ private static List<CompletionResult> CompleteCommentHelp(CompletionContext cont
61966225
{
61976226
if (keyword.StartsWith(lineKeyword.Value, StringComparison.OrdinalIgnoreCase))
61986227
{
6199-
result.Add(new CompletionResult(keyword, keyword, CompletionResultType.Keyword, s_commentHelpKeywords[keyword]));
6228+
string toolTip = GetCommentHelpKeywordsToolTip(keyword);
6229+
result.Add(new CompletionResult(keyword, keyword, CompletionResultType.Keyword, toolTip));
62006230
}
62016231
}
62026232

@@ -6256,46 +6286,66 @@ private static List<CompletionResult> CompleteCommentHelp(CompletionContext cont
62566286
return null;
62576287
}
62586288

6259-
private static readonly IReadOnlyDictionary<string, string> s_commentHelpKeywords = new SortedList<string, string>(StringComparer.OrdinalIgnoreCase)
6260-
{
6261-
{ "SYNOPSIS", "A brief description of the function or script. This keyword can be used only once in each topic." },
6262-
{ "DESCRIPTION", "A detailed description of the function or script. This keyword can be used only once in each topic." },
6263-
{ "PARAMETER", ".PARAMETER <Parameter-Name>\nThe description of a parameter. Add a .PARAMETER keyword for each parameter in the function or script syntax." },
6264-
{ "EXAMPLE", "A sample command that uses the function or script, optionally followed by sample output and a description. Repeat this keyword for each example." },
6265-
{ "INPUTS", "The .NET types of objects that can be piped to the function or script. You can also include a description of the input objects." },
6266-
{ "OUTPUTS", "The .NET type of the objects that the cmdlet returns. You can also include a description of the returned objects." },
6267-
{ "NOTES", "Additional information about the function or script." },
6268-
{ "LINK", "The name of a related topic. Repeat the .LINK keyword for each related topic. The .Link keyword content can also include a URI to an online version of the same help topic." },
6269-
{ "COMPONENT", "The name of the technology or feature that the function or script uses, or to which it is related." },
6270-
{ "ROLE", "The name of the user role for the help topic." },
6271-
{ "FUNCTIONALITY", "The keywords that describe the intended use of the function." },
6272-
{ "FORWARDHELPTARGETNAME", ".FORWARDHELPTARGETNAME <Command-Name>\nRedirects to the help topic for the specified command." },
6273-
{ "FORWARDHELPCATEGORY", ".FORWARDHELPCATEGORY <Category>\nSpecifies the help category of the item in .ForwardHelpTargetName" },
6274-
{ "REMOTEHELPRUNSPACE", ".REMOTEHELPRUNSPACE <PSSession-variable>\nSpecifies a session that contains the help topic. Enter a variable that contains a PSSession object." },
6275-
{ "EXTERNALHELP", ".EXTERNALHELP <XML Help File>\nThe .ExternalHelp keyword is required when a function or script is documented in XML files." }
6289+
private static readonly string[] s_commentHelpKeywords = new string[]
6290+
{
6291+
"COMPONENT",
6292+
"DESCRIPTION",
6293+
"EXAMPLE",
6294+
"EXTERNALHELP",
6295+
"FORWARDHELPCATEGORY",
6296+
"FORWARDHELPTARGETNAME",
6297+
"FUNCTIONALITY",
6298+
"INPUTS",
6299+
"LINK",
6300+
"NOTES",
6301+
"OUTPUTS",
6302+
"PARAMETER",
6303+
"REMOTEHELPRUNSPACE",
6304+
"ROLE",
6305+
"SYNOPSIS"
6306+
};
6307+
6308+
private static string GetCommentHelpKeywordsToolTip(string name) => name switch
6309+
{
6310+
"COMPONENT" => TabCompletionStrings.CommentHelpCOMPONENTKeywordDescription,
6311+
"DESCRIPTION" => TabCompletionStrings.CommentHelpDESCRIPTIONKeywordDescription,
6312+
"EXAMPLE" => TabCompletionStrings.CommentHelpEXAMPLEKeywordDescription,
6313+
"EXTERNALHELP" => TabCompletionStrings.CommentHelpEXTERNALHELPKeywordDescription,
6314+
"FORWARDHELPCATEGORY" => TabCompletionStrings.CommentHelpFORWARDHELPCATEGORYKeywordDescription,
6315+
"FORWARDHELPTARGETNAME" => TabCompletionStrings.CommentHelpFORWARDHELPTARGETNAMEKeywordDescription,
6316+
"FUNCTIONALITY" => TabCompletionStrings.CommentHelpFUNCTIONALITYKeywordDescription,
6317+
"INPUTS" => TabCompletionStrings.CommentHelpINPUTSKeywordDescription,
6318+
"LINK" => TabCompletionStrings.CommentHelpLINKKeywordDescription,
6319+
"NOTES" => TabCompletionStrings.CommentHelpNOTESKeywordDescription,
6320+
"OUTPUTS" => TabCompletionStrings.CommentHelpOUTPUTSKeywordDescription,
6321+
"PARAMETER" => TabCompletionStrings.CommentHelpPARAMETERKeywordDescription,
6322+
"REMOTEHELPRUNSPACE" => TabCompletionStrings.CommentHelpREMOTEHELPRUNSPACEKeywordDescription,
6323+
"ROLE" => TabCompletionStrings.CommentHelpROLEKeywordDescription,
6324+
"SYNOPSIS" => TabCompletionStrings.CommentHelpSYNOPSISKeywordDescription,
6325+
_ => string.Empty
62766326
};
62776327

62786328
private static readonly HashSet<string> s_commentHelpAllowedDuplicateKeywords = new(StringComparer.OrdinalIgnoreCase)
62796329
{
6280-
"PARAMETER",
62816330
"EXAMPLE",
6282-
"LINK"
6331+
"LINK",
6332+
"PARAMETER"
62836333
};
62846334

62856335
private static readonly string[] s_commentHelpForwardCategories = new string[]
62866336
{
62876337
"Alias",
6338+
"All",
62886339
"Cmdlet",
6289-
"HelpFile",
6340+
"ExternalScript",
6341+
"FAQ",
6342+
"Filter",
62906343
"Function",
6291-
"Provider",
62926344
"General",
6293-
"FAQ",
62946345
"Glossary",
6295-
"ScriptCommand",
6296-
"ExternalScript",
6297-
"Filter",
6298-
"All"
6346+
"HelpFile",
6347+
"Provider",
6348+
"ScriptCommand"
62996349
};
63006350

63016351
private static FunctionDefinitionAst GetCommentHelpFunctionTarget(CompletionContext context)
@@ -8066,7 +8116,7 @@ internal static List<CompletionResult> CompleteHashtableKey(CompletionContext co
80668116
}
80678117

80688118
var result = new List<CompletionResult>();
8069-
foreach (var key in s_requiresModuleSpecKeys.Keys)
8119+
foreach (string key in s_requiresModuleSpecKeys)
80708120
{
80718121
if (excludedKeys.Contains(key)
80728122
|| (wordToComplete is not null && !key.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase))
@@ -8075,7 +8125,10 @@ internal static List<CompletionResult> CompleteHashtableKey(CompletionContext co
80758125
{
80768126
continue;
80778127
}
8078-
result.Add(new CompletionResult(key, key, CompletionResultType.Property, s_requiresModuleSpecKeys[key]));
8128+
8129+
string toolTip = GetRequiresModuleSpecKeysToolTip(key);
8130+
8131+
result.Add(new CompletionResult(key, key, CompletionResultType.Property, toolTip));
80798132
}
80808133

80818134
return result;
@@ -8327,7 +8380,9 @@ private static List<CompletionResult> GetSpecialHashTableKeyMembers(HashSet<stri
83278380
{
< 341A div aria-hidden="true" style="left:-2px" class="position-absolute top-0 d-flex user-select-none DiffLineTableCellParts-module__in-progress-comment-indicator--hx3m3">
83288381
if ((string.IsNullOrEmpty(wordToComplete) || key.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase)) && !excludedKeys.Contains(key))
83298382
{
8330-
result.Add(new CompletionResult(key, key, CompletionResultType.Property, key));
8383+
string toolTip = GetHashtableKeyDescriptionToolTip(key);
8384+
8385+
result.Add(new CompletionResult(key, key, CompletionResultType.Property, toolTip));
83318386
}
83328387
}
83338388

@@ -8339,6 +8394,31 @@ private static List<CompletionResult> GetSpecialHashTableKeyMembers(HashSet<stri
83398394
return result;
83408395
}
83418396

8397+
private static string GetHashtableKeyDescriptionToolTip(string name) => name switch
8398+
{
8399+
"Alignment" => TabCompletionStrings.AlignmentHashtableKeyDescription,
8400+
"Ascending" => TabCompletionStrings.AscendingHashtableKeyDescription,
8401+
"Data" => TabCompletionStrings.DataHashtableKeyDescription,
8402+
"Depth" => TabCompletionStrings.DepthHashtableKeyDescription,
8403+
"Descending" => TabCompletionStrings.DescendingHashtableKeyDescription,
8404+
"EndTime" => TabCompletionStrings.EndTimeHashtableKeyDescription,
8405+
"Expression" => TabCompletionStrings.ExpressionHashtableKeyDescription,
8406+
"FormatString" => TabCompletionStrings.FormatStringHashtableKeyDescription,
8407+
"ID" => TabCompletionStrings.IDHashtableKeyDescription,
8408+
"Keywords" => TabCompletionStrings.KeywordsHashtableKeyDescription,
8409+
"Label" => TabCompletionStrings.LabelHashtableKeyDescription,
8410+
"Level" => TabCompletionStrings.LevelHashtableKeyDescription,
8411+
"LogName" => TabCompletionStrings.LogNameHashtableKeyDescription,
8412+
"Name" => TabCompletionStrings.NameHashtableKeyDescription,
8413+
"Path" => TabCompletionStrings.PathHashtableKeyDescription,
8414+
"ProviderName" => TabCompletionStrings.ProviderNameHashtableKeyDescription,
8415+
"StartTime" => TabCompletionStrings.StartTimeHashtableKeyDescription,
8416+
"SuppressHashFilter" => TabCompletionStrings.SuppressHashFilterHashtableKeyDescription,
8417+
"UserID" => TabCompletionStrings.UserIDHashtableKeyDescription,
8418+
"Width" => TabCompletionStrings.WidthHashtableKeyDescription,
8419+
_ => string.Empty
8420+
};
8421+
83428422
#endregion Hashtable Keys
83438423

83448424
#region Helpers

0 commit comments

Comments
 (0)
0