Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
99b1ff5
Add single/double quote support for `Join-String` Argument Completer
ArmaanMcleod Apr 5, 2025
c22bcdd
Merge branch 'master' into join-string-completion-use-completion-helpers
ArmaanMcleod Apr 5, 2025
d4a5a7d
CodeFactor: SA1413 Add trailing comma in multi-line initializers
ArmaanMcleod Apr 5, 2025
401ac49
Split into two argument completers
ArmaanMcleod Apr 8, 2025
51d9437
Update src/System.Management.Automation/engine/CommandCompletion/Comp…
ArmaanMcleod Apr 8, 2025
20ccaa7
Fix IsMatch method and test
ArmaanMcleod Apr 9, 2025
ffae7ef
Merge branch 'master' into join-string-completion-use-completion-helpers
ArmaanMcleod Apr 9, 2025
8d556b4
Update src/Microsoft.PowerShell.Commands.Utility/commands/utility/Joi…
ArmaanMcleod Apr 9, 2025
163a6ec
Fixed indentation
ArmaanMcleod Apr 9, 2025
09818bb
Escape word to complete before it gets to WildcardPattern.Get
ArmaanMcleod Apr 10, 2025
e8fe981
Merge branch 'master' into join-string-completion-use-completion-helpers
ArmaanMcleod Apr 10, 2025
d57f8c8
Fix Xunit test
ArmaanMcleod Apr 10, 2025
33ca0a7
Add IsMatch delegate which includes escape strategy delegate
ArmaanMcleod Apr 12, 2025
3258b66
Merge branch 'master' into join-string-completion-use-completion-helpers
ArmaanMcleod Apr 12, 2025
f159a33
Add MatchStrategy delegates
ArmaanMcleod Apr 13, 2025
99f2e3a
Wrap delegates on multiple lines
ArmaanMcleod Apr 13, 2025
5a2ab03
Remove separate list for separator values and extract keys from mappi…
ArmaanMcleod Apr 13, 2025
de63820
Make completer classes sealed
ArmaanMcleod Apr 13, 2025
38bc0a7
Set capacity on collections
ArmaanMcleod Apr 13, 2025
231f7bc
Add XML documentation for methods
ArmaanMcleod Apr 13, 2025
8066ef5
Fix normalization with newlines
ArmaanMcleod Apr 14, 2025
cd0b123
Add const for quotes
ArmaanMcleod Apr 17, 2025
f3ec9f9
Move normalize newlines code outside of match strategy to separate me…
ArmaanMcleod Apr 19, 2025
f4bfa8e
Merge branch 'master' into join-string-completion-use-completion-helpers
ArmaanMcleod Apr 19, 2025
139a778
Check newlines in conditions before replacing
ArmaanMcleod Apr 19, 2025
bcc9af4
Revert "Check newlines in conditions before replacing"
ArmaanMcleod Apr 19, 2025
e7153fa
Rename method to NormalizeToExpandableString
ArmaanMcleod Apr 19, 2025
0022a79
Add more special character to escape sequence normalizing
ArmaanMcleod Apr 20, 2025
89da820
Update src/System.Management.Automation/engine/CommandCompletion/Comp…
ArmaanMcleod Apr 20, 2025
7ea1680
Update test/xUnit/csharp/test_CompletionHelpers.cs
iSazonov Apr 20, 2025
6156af3
Update test/xUnit/csharp/test_CompletionHelpers.cs
iSazonov Apr 20, 2025
0a853cf
Update src/System.Management.Automation/engine/CommandCompletion/Comp…
ArmaanMcleod Apr 20, 2025
babbde7
Update src/System.Management.Automation/engine/CommandCompletion/Comp…
ArmaanMcleod Apr 20, 2025
002f46a
Update src/System.Management.Automation/engine/CommandCompletion/Comp…
ArmaanMcleod Apr 20, 2025
542a28a
Update src/System.Management.Automation/engine/CommandCompletion/Comp…
ArmaanMcleod Apr 20, 2025
7125ec9
Add IgnoreCase to match strategy delegate names
ArmaanMcleod Apr 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public sealed class JoinStringCommand : PSCmdlet
/// Gets or sets the delimiter to join the output with.
/// </summary>
[Parameter(Position = 1)]
[ArgumentCompleter(typeof(JoinItemCompleter))]
[ArgumentCompleter(typeof(SeparatorArgumentCompleter))]
[AllowEmptyString]
public string Separator
{
Expand Down Expand Up @@ -79,7 +79,7 @@ public string Separator
/// Gets or sets a format string that is applied to each input object.
/// </summary>
[Parameter(ParameterSetName = "Format")]
[ArgumentCompleter(typeof(JoinItemCompleter))]
[ArgumentCompleter(typeof(FormatStringArgumentCompleter))]
public string FormatString { get; set; }

/// <summary>
Expand Down Expand Up @@ -160,79 +160,100 @@ protected override void EndProcessing()
}
}

[SuppressMessage(
"Microsoft.Performance",
"CA1812:AvoidUninstantiatedInternalClasses",
Justification = "Class is instantiated through late-bound reflection")]
internal class JoinItemCompleter : IArgumentCompleter
/// <summary>
/// Provides completion for the Separator parameter of the Join-String cmdlet.
/// </summary>
public sealed class SeparatorArgumentCompleter : IArgumentCompleter
{
public IEnumerable<CompletionResult> CompleteArgument(
string commandName,
string parameterName,
string wordToComplete,
CommandAst commandAst,
IDictionary fakeBoundParameters)
{
switch (parameterName)
{
case "Separator": return CompleteSeparator(wordToComplete);
case "FormatString": return CompleteFormatString(wordToComplete);
}

return null;
}
private static readonly string NewLineText =
#if UNIX
"`n";
#else
"`r`n";
#endif

private static IEnumerable<CompletionResult> CompleteFormatString(string wordToComplete)
private static readonly Dictionary<string, (string Tooltip, string ListItemText)> s_separatorMappings = new(capacity: 7)
{
var res = new List<CompletionResult>();
void AddMatching(string completionText)
{
if (completionText.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase))
{
res.Add(new CompletionResult(completionText));
}
}
{ ",", (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") },
};

AddMatching("'[{0}]'");
AddMatching("'{0:N2}'");
AddMatching("\"`r`n `${0}\"");
AddMatching("\"`r`n [string] `${0}\"");
private static readonly IEnumerable<string> s_separatorValues = s_separatorMappings.Keys;

return res;
}

private IEnumerable<CompletionResult> CompleteSeparator(string wordToComplete)
{
var res = new List<CompletionResult>(10);
private static string GetSeparatorToolTip(string separator)
=> s_separatorMappings.TryGetValue(separator, out var mapping)
? mapping.Tooltip
: separator;

void AddMatching(string completionText, string listText, string toolTip)
{
if (completionText.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase))
{
res.Add(new CompletionResult(completionText, listText, CompletionResultType.ParameterValue, toolTip));
}
}
private static string GetSeparatorListItemText(string separator)
=> s_separatorMappings.TryGetValue(separator, out var mapping)
? mapping.ListItemText
: separator;

AddMatching("', '", "Comma-Space", "', ' - Comma-Space");
AddMatching("';'", "Semi-Colon", "';' - Semi-Colon ");
AddMatching("'; '", "Semi-Colon-Space", "'; ' - Semi-Colon-Space");
AddMatching($"\"{NewLineText}\"", "Newline", $"{NewLineText} - Newline");
AddMatching("','", "Comma", "',' - Comma");
AddMatching("'-'", "Dash", "'-' - Dash");
AddMatching("' '", "Space", "' ' - Space");
return res;
}
/// <summary>
/// Returns completion results for Separator parameter.
/// </summary>
/// <param name="commandName">The command name.</param>
/// <param name="parameterName">The parameter name.</param>
/// <param name="wordToComplete">The word to complete.</param>
/// <param name="commandAst">The command AST.</param>
/// <param name="fakeBoundParameters">The fake bound parameters.</param>
/// <returns>List of Completion Results.</returns>
public IEnumerable<CompletionResult> CompleteArgument(
string commandName,
string parameterName,
string wordToComplete,
CommandAst commandAst,
IDictionary fakeBoundParameters)
=> CompletionHelpers.GetMatchingResults(
wordToComplete,
possibleCompletionValues: s_separatorValues,
listItemTextMapping: GetSeparatorListItemText,
toolTipMapping: GetSeparatorToolTip,
resultType: CompletionResultType.ParameterValue);
}

public string NewLineText
/// <summary>
/// Provides completion for the FormatString parameter of the Join-String cmdlet.
/// </summary>
public sealed class FormatStringArgumentCompleter : IArgumentCompleter
{
private static readonly IReadOnlyList<string> s_formatStringValues = new List<string>(capacity: 4)
{
get
{
"[{0}]",
"{0:N2}",
#if UNIX
return "`n";
"`n `${0}",
"`n [string] `${0}",
#else
return "`r`n";
"`r`n `${0}",
"`r`n [string] `${0}",
#endif
}
}
};

/// <summary>
/// Returns completion results for FormatString parameter.
/// </summary>
/// <param name="commandName">The command name.</param>
/// <param name="parameterName">The parameter name.</param>
/// <param name="wordToComplete">The word to complete.</param>
/// <param name="commandAst">The command AST.</param>
/// <param name="fakeBoundParameters">The fake bound parameters.</param>
/// <returns>List of Completion Results.</returns>
public IEnumerable<CompletionResult> CompleteArgument(
string commandName,
string parameterName,
string wordToComplete,
CommandAst commandAst,
IDictionary fakeBoundParameters)
=> CompletionHelpers.GetMatchingResults(
wordToComplete,
possibleCompletionValues: s_formatStringValues,
matchStrategy: CompletionHelpers.WildcardPatternEscapeMatch);
}
}
Loading
Loading