Add argument completer for Set-StrictMode -Version (#20554)

This commit is contained in:
Armaan Mcleod
2023-10-24 03:32:25 +00:00
committed by GitHub
parent 7c8b1c819d
commit 1dc05eaac0
2 changed files with 61 additions and 0 deletions
@@ -2686,6 +2686,7 @@ namespace Microsoft.PowerShell.Commands
/// Gets or sets strict mode in the current scope.
/// </summary>
[Parameter(ParameterSetName = "Version", Mandatory = true)]
[ArgumentCompleter(typeof(StrictModeVersionArgumentCompleter))]
[ArgumentToPSVersionTransformation]
[ValidateVersion]
[Alias("v")]
@@ -2717,6 +2718,42 @@ namespace Microsoft.PowerShell.Commands
Context.EngineSessionState.CurrentScope.StrictModeVersion = _version;
}
}
/// <summary>
/// Provides argument completion for StrictMode Version parameter.
/// </summary>
public class StrictModeVersionArgumentCompleter : IArgumentCompleter
{
private static readonly string[] s_strictModeVersions = new string[] { "Latest", "3.0", "2.0", "1.0" };
/// <summary>
/// Returns completion results for version 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)
{
var strictModeVersionPattern = WildcardPattern.Get(wordToComplete + "*", WildcardOptions.IgnoreCase);
foreach (string version in s_strictModeVersions)
{
if (strictModeVersionPattern.IsMatch(version))
{
yield return new CompletionResult(version);
}
}
}
}
#endregion Set-StrictMode
#endregion Built-in cmdlets that are used by or require direct access to the engine.
@@ -744,6 +744,30 @@ ConstructorTestClass(int i, bool b)
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$TestVar1'
}
Context 'StrictMode Version parameter completion' {
BeforeAll {
$allStrictModeVersions = '1.0 2.0 3.0 Latest'
$versionOne = '1.0'
$versionTwo = '2.0'
$versionThree = '3.0'
$latestVersion = 'Latest'
}
It "Should complete Version for '<TextInput>'" -TestCases @(
@{ TextInput = "Set-StrictMode -Version "; ExpectedVersions = $allStrictModeVersions }
@{ TextInput = "Set-StrictMode -Version 1"; ExpectedVersions = $versionOne }
@{ TextInput = "Set-StrictMode -Version 2"; ExpectedVersions = $versionTwo }
@{ TextInput = "Set-StrictMode -Version 3"; ExpectedVersions = $versionThree }
@{ TextInput = "Set-StrictMode -Version Lat"; ExpectedVersions = $latestVersion }
@{ TextInput = "Set-StrictMode -Version NonExistentVersion"; ExpectedVersions = '' }
) {
param($TextInput, $ExpectedVersions)
$res = TabExpansion2 -inputScript $TextInput -cursorColumn $TextInput.Length
$completionText = $res.CompletionMatches.CompletionText | Sort-Object
$completionText -join ' ' | Should -BeExactly $ExpectedVersions
}
}
Context "Format cmdlet's View paramter completion" {
BeforeAll {
$viewDefinition = @'