mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Refactor Tooltip/ListItemText mapping to use CompletionDisplayInfoMapper delegate (#25395)
This commit is contained in:
+27
-13
@@ -189,7 +189,32 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </summary>
|
||||
public class PropertyTypeArgumentCompleter : IArgumentCompleter
|
||||
{
|
||||
private static readonly string[] s_RegistryPropertyTypes = new string[]
|
||||
private static readonly CompletionHelpers.CompletionDisplayInfoMapper RegistryPropertyTypeDisplayInfoMapper = registryPropertyType => registryPropertyType switch
|
||||
{
|
||||
"String" => (
|
||||
ToolTip: TabCompletionStrings.RegistryStringToolTip,
|
||||
ListItemText: "String"),
|
||||
"ExpandString" => (
|
||||
ToolTip: TabCompletionStrings.RegistryExpandStringToolTip,
|
||||
ListItemText: "ExpandString"),
|
||||
"Binary" => (
|
||||
ToolTip: TabCompletionStrings.RegistryBinaryToolTip,
|
||||
ListItemText: "Binary"),
|
||||
"DWord" => (
|
||||
ToolTip: TabCompletionStrings.RegistryDWordToolTip,
|
||||
ListItemText: "DWord"),
|
||||
"MultiString" => (
|
||||
ToolTip: TabCompletionStrings.RegistryMultiStringToolTip,
|
||||
ListItemText: "MultiString"),
|
||||
"QWord" => (
|
||||
ToolTip: TabCompletionStrings.RegistryQWordToolTip,
|
||||
ListItemText: "QWord"),
|
||||
_ => (
|
||||
ToolTip: TabCompletionStrings.RegistryUnknownToolTip,
|
||||
ListItemText: "Unknown"),
|
||||
};
|
||||
|
||||
private static readonly IReadOnlyList<string> s_RegistryPropertyTypes = new List<string>(capacity: 7)
|
||||
{
|
||||
"String",
|
||||
"ExpandString",
|
||||
@@ -200,17 +225,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
"Unknown"
|
||||
};
|
||||
|
||||
private static string GetRegistryPropertyTypeToolTip(string propertyTypeName) => propertyTypeName switch
|
||||
{
|
||||
"String" => TabCompletionStrings.RegistryStringToolTip,
|
||||
"ExpandString" => TabCompletionStrings.RegistryExpandStringToolTip,
|
||||
"Binary" => TabCompletionStrings.RegistryBinaryToolTip,
|
||||
"DWord" => TabCompletionStrings.RegistryDWordToolTip,
|
||||
"MultiString" => TabCompletionStrings.RegistryMultiStringToolTip,
|
||||
"QWord" => TabCompletionStrings.RegistryQWordToolTip,
|
||||
_ => TabCompletionStrings.RegistryUnknownToolTip
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Returns completion results for PropertyType parameter.
|
||||
/// </summary>
|
||||
@@ -230,7 +244,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
? CompletionHelpers.GetMatchingResults(
|
||||
wordToComplete,
|
||||
possibleCompletionValues: s_RegistryPropertyTypes,
|
||||
toolTipMapping: GetRegistryPropertyTypeToolTip,
|
||||
displayInfoMapper: RegistryPropertyTypeDisplayInfoMapper,
|
||||
resultType: CompletionResultType.ParameterValue)
|
||||
: [];
|
||||
|
||||
|
||||
@@ -165,35 +165,51 @@ namespace Microsoft.PowerShell.Commands.Utility
|
||||
/// </summary>
|
||||
public sealed class SeparatorArgumentCompleter : IArgumentCompleter
|
||||
{
|
||||
private static readonly string NewLineText =
|
||||
private const string NewLineText =
|
||||
#if UNIX
|
||||
"`n";
|
||||
#else
|
||||
"`r`n";
|
||||
#endif
|
||||
|
||||
private static readonly Dictionary<string, (string Tooltip, string ListItemText)> s_separatorMappings = new(capacity: 7)
|
||||
private static readonly CompletionHelpers.CompletionDisplayInfoMapper SeparatorDisplayInfoMapper = separator => separator switch
|
||||
{
|
||||
{ ",", (TabCompletionStrings.SeparatorCommaToolTip, "Comma") },
|
||||
{ ", ", (TabCompletionStrings.SeparatorCommaSpaceToolTip, "Comma-Space") },
|
||||
{ ";", (TabCompletionStrings.SeparatorSemiColonToolTip, "Semi-Colon") },
|
||||
{ "; ", (TabCompletionStrings.SeparatorSemiColonSpaceToolTip, "Semi-Colon-Space") },
|
||||
{ NewLineText, (StringUtil.Format(TabCompletionStrings.SeparatorNewlineToolTip, NewLineText), "Newline") },
|
||||
{ "-", (TabCompletionStrings.SeparatorDashToolTip, "Dash") },
|
||||
{ " ", (TabCompletionStrings.SeparatorSpaceToolTip, "Space") },
|
||||
"," => (
|
||||
ToolTip: TabCompletionStrings.SeparatorCommaToolTip,
|
||||
ListItemText: "Comma"),
|
||||
", " => (
|
||||
ToolTip: TabCompletionStrings.SeparatorCommaSpaceToolTip,
|
||||
ListItemText: "Comma-Space"),
|
||||
";" => (
|
||||
ToolTip: TabCompletionStrings.SeparatorSemiColonToolTip,
|
||||
ListItemText: "Semi-Colon"),
|
||||
"; " => (
|
||||
ToolTip: TabCompletionStrings.SeparatorSemiColonSpaceToolTip,
|
||||
ListItemText: "Semi-Colon-Space"),
|
||||
"-" => (
|
||||
ToolTip: TabCompletionStrings.SeparatorDashToolTip,
|
||||
ListItemText: "Dash"),
|
||||
" " => (
|
||||
ToolTip: TabCompletionStrings.SeparatorSpaceToolTip,
|
||||
ListItemText: "Space"),
|
||||
NewLineText => (
|
||||
ToolTip: StringUtil.Format(TabCompletionStrings.SeparatorNewlineToolTip, NewLineText),
|
||||
ListItemText: "Newline"),
|
||||
_ => (
|
||||
ToolTip: separator,
|
||||
ListItemText: separator),
|
||||
};
|
||||
|
||||
private static readonly IEnumerable<string> s_separatorValues = s_separatorMappings.Keys;
|
||||
|
||||
private static string GetSeparatorToolTip(string separator)
|
||||
=> s_separatorMappings.TryGetValue(separator, out var mapping)
|
||||
? mapping.Tooltip
|
||||
: separator;
|
||||
|
||||
private static string GetSeparatorListItemText(string separator)
|
||||
=> s_separatorMappings.TryGetValue(separator, out var mapping)
|
||||
? mapping.ListItemText
|
||||
: separator;
|
||||
private static readonly IReadOnlyList<string> s_separatorValues = new List<string>(capacity: 7)
|
||||
{
|
||||
",",
|
||||
", ",
|
||||
";",
|
||||
"; ",
|
||||
NewLineText,
|
||||
"-",
|
||||
" ",
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Returns completion results for Separator parameter.
|
||||
@@ -213,8 +229,7 @@ namespace Microsoft.PowerShell.Commands.Utility
|
||||
=> CompletionHelpers.GetMatchingResults(
|
||||
wordToComplete,
|
||||
possibleCompletionValues: s_separatorValues,
|
||||
listItemTextMapping: GetSeparatorListItemText,
|
||||
toolTipMapping: GetSeparatorToolTip,
|
||||
displayInfoMapper: SeparatorDisplayInfoMapper,
|
||||
resultType: CompletionResultType.ParameterValue);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,19 +23,18 @@ namespace System.Management.Automation
|
||||
/// </summary>
|
||||
/// <param name="wordToComplete">The word to complete.</param>
|
||||
/// <param name="possibleCompletionValues">The possible completion values to iterate.</param>
|
||||
/// <param name="toolTipMapping">The optional tool tip mapping delegate.</param>
|
||||
/// <param name="listItemTextMapping">The optional list item text mapping delegate.</param>
|
||||
/// <param name="displayInfoMapper">The optional completion display info mapper delegate for tool tip and list item text.</param>
|
||||
/// <param name="resultType">The optional completion result type. Default is Text.</param>
|
||||
/// <param name="matchStrategy">The optional match strategy delegate.</param>
|
||||
/// <returns>List of matching completion results.</returns>
|
||||
internal static IEnumerable<CompletionResult> GetMatchingResults(
|
||||
string wordToComplete,
|
||||
IEnumerable<string> possibleCompletionValues,
|
||||
Func<string, string> toolTipMapping = null,
|
||||
Func<string, string> listItemTextMapping = null,
|
||||
CompletionDisplayInfoMapper displayInfoMapper = null,
|
||||
CompletionResultType resultType = CompletionResultType.Text,
|
||||
MatchStrategy matchStrategy = null)
|
||||
{
|
||||
displayInfoMapper ??= DefaultDisplayInfoMapper;
|
||||
matchStrategy ??= DefaultMatch;
|
||||
|
||||
string quote = HandleDoubleAndSingleQuote(ref wordToComplete);
|
||||
@@ -49,14 +48,30 @@ namespace System.Management.Automation
|
||||
if (matchStrategy(value, wordToComplete))
|
||||
{
|
||||
string completionText = QuoteCompletionText(value, quote);
|
||||
string toolTip = toolTipMapping?.Invoke(value) ?? value;
|
||||
string listItemText = listItemTextMapping?.Invoke(value) ?? value;
|
||||
|
||||
(string toolTip, string listItemText) = displayInfoMapper(value);
|
||||
|
||||
yield return new CompletionResult(completionText, listItemText, resultType, toolTip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides the display information for a completion result.
|
||||
/// This delegate is used to map a string value to its corresponding display information.
|
||||
/// </summary>
|
||||
/// <param name="value">The input value to be mapped</param>
|
||||
/// <returns>Completion display info containing tool tip and list item text.</returns>
|
||||
internal delegate (string ToolTip, string ListItemText) CompletionDisplayInfoMapper(string value);
|
||||
|
||||
/// <summary>
|
||||
/// Provides the default display information for a completion result.
|
||||
/// Defaults to using the input value for both the tool tip and list item text.
|
||||
/// </summary>
|
||||
/// <returns>Completion display info containing tool tip and list item text.</returns>
|
||||
internal static readonly CompletionDisplayInfoMapper DefaultDisplayInfoMapper = value
|
||||
=> (value, value);
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes the input string to an expandable string format for PowerShell.
|
||||
/// </summary>
|
||||
|
||||
@@ -1368,6 +1368,13 @@ param([ValidatePattern(
|
||||
$res = TabExpansion2 -inputScript $TextInput -cursorColumn $TextInput.Length
|
||||
$completionText = $res.CompletionMatches.CompletionText
|
||||
$completionText -join ' ' | Should -BeExactly $ExpectedPropertyTypes
|
||||
|
||||
foreach ($match in $res.CompletionMatches) {
|
||||
$completionText = $match.CompletionText.Replace("""", "").Replace("'", "")
|
||||
$listItemText = $match.ListItemText
|
||||
$completionText | Should -BeExactly $listItemText
|
||||
$match.ToolTip | Should -Not -BeNullOrEmpty
|
||||
}
|
||||
}
|
||||
|
||||
It "Test fallback to provider of current location if no path specified" -Skip:(!$IsWindows) {
|
||||
@@ -1554,6 +1561,14 @@ param([ValidatePattern(
|
||||
$res = TabExpansion2 -inputScript $TextInput -cursorColumn $TextInput.Length
|
||||
$completionText = $res.CompletionMatches.CompletionText
|
||||
$completionText -join ' ' | Should -BeExactly $Expected
|
||||
|
||||
foreach ($match in $res.CompletionMatches) {
|
||||
$toolTip = $match.ToolTip.Replace("""", "").Replace("'", "")
|
||||
$completionText = $match.CompletionText.Replace("""", "").Replace("'", "")
|
||||
$listItemText = $match.ListItemText
|
||||
$toolTip.StartsWith($completionText) | Should -BeTrue
|
||||
$toolTip.EndsWith($listItemText) | Should -BeTrue
|
||||
}
|
||||
}
|
||||
|
||||
It "Should complete for '<TextInput>'" -Skip:(!$IsWindows) -TestCases @(
|
||||
@@ -1574,6 +1589,14 @@ param([ValidatePattern(
|
||||
$res = TabExpansion2 -inputScript $TextInput -cursorColumn $TextInput.Length
|
||||
$completionText = $res.CompletionMatches.CompletionText
|
||||
$completionText -join ' ' | Should -BeExactly $Expected
|
||||
|
||||
foreach ($match in $res.CompletionMatches) {
|
||||
$toolTip = $match.ToolTip.Replace("""", "").Replace("'", "")
|
||||
$completionText = $match.CompletionText.Replace("""", "").Replace("'", "")
|
||||
$listItemText = $match.ListItemText
|
||||
$toolTip.StartsWith($completionText) | Should -BeTrue
|
||||
$toolTip.EndsWith($listItemText) | Should -BeTrue
|
||||
}
|
||||
}
|
||||
|
||||
It "Should complete for '<TextInput>'" -Skip:($IsWindows) -TestCases @(
|
||||
@@ -1590,6 +1613,14 @@ param([ValidatePattern(
|
||||
$res = TabExpansion2 -inputScript $TextInput -cursorColumn $TextInput.Length
|
||||
$completionText = $res.CompletionMatches.CompletionText
|
||||
$completionText -join ' ' | Should -BeExactly $Expected
|
||||
|
||||
foreach ($match in $res.CompletionMatches) {
|
||||
$toolTip = $match.ToolTip.Replace("""", "").Replace("'", "")
|
||||
$completionText = $match.CompletionText.Replace("""", "").Replace("'", "")
|
||||
$listItemText = $match.ListItemText
|
||||
$toolTip.StartsWith($completionText) | Should -BeTrue
|
||||
$toolTip.EndsWith($listItemText) | Should -BeTrue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user