Add -Scope argument completer for *-Variable, *-Alias & *-PSDrive commands (#20451)

This commit is contained in:
Armaan Mcleod
2023-12-19 14:12:33 +05:00
committed by GitHub
parent 37547567b8
commit 9383f554a2
9 changed files with 79 additions and 0 deletions
@@ -1129,6 +1129,7 @@ namespace Microsoft.PowerShell.Commands
/// Gets or sets the scope identifier for the drive being created.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
[ArgumentCompleter(typeof(ScopeArgumentCompleter))]
public string Scope { get; set; }
#if !UNIX
@@ -1533,6 +1534,7 @@ namespace Microsoft.PowerShell.Commands
/// global scope until a drive of the given name is found to remove.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
[ArgumentCompleter(typeof(ScopeArgumentCompleter))]
public string Scope { get; set; }
/// <summary>
@@ -1701,6 +1703,7 @@ namespace Microsoft.PowerShell.Commands
/// Gets or sets the scope parameter to the command.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
[ArgumentCompleter(typeof(ScopeArgumentCompleter))]
public string Scope { get; set; }
/// <summary>
@@ -184,6 +184,7 @@ namespace Microsoft.PowerShell.Commands
/// which scope the aliases are retrieved from.
/// </summary>
[Parameter]
[ArgumentCompleter(typeof(ScopeArgumentCompleter))]
public string Scope { get; set; }
#endregion Parameters
@@ -51,6 +51,7 @@ namespace Microsoft.PowerShell.Commands
/// which scope the aliases are retrieved from.
/// </summary>
[Parameter]
[ArgumentCompleter(typeof(ScopeArgumentCompleter))]
public string Scope { get; set; }
/// <summary>
@@ -55,6 +55,7 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
[ArgumentCompleter(typeof(ScopeArgumentCompleter))]
public string Scope { get; set; }
/// <summary>
@@ -25,6 +25,7 @@ namespace Microsoft.PowerShell.Commands
/// The scope parameter for the command determines which scope the alias is removed from.
/// </summary>
[Parameter]
[ArgumentCompleter(typeof(ScopeArgumentCompleter))]
public string Scope { get; set; }
/// <summary>
@@ -22,6 +22,7 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
[ArgumentCompleter(typeof(ScopeArgumentCompleter))]
public string Scope { get; set; }
#endregion parameters
@@ -61,6 +61,7 @@ namespace Microsoft.PowerShell.Commands
/// The scope parameter for the command determines which scope the alias is set in.
/// </summary>
[Parameter]
[ArgumentCompleter(typeof(ScopeArgumentCompleter))]
public string Scope { get; set; }
/// <summary>
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation.Language;
namespace System.Management.Automation
{
/// <summary>
/// Provides argument completion for Scope parameter.
/// </summary>
public class ScopeArgumentCompleter : IArgumentCompleter
{
private static readonly string[] s_Scopes = new string[] { "Global", "Local", "Script" };
/// <summary>
/// Returns completion results for scope 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 scopePattern = WildcardPattern.Get(wordToComplete + "*", WildcardOptions.IgnoreCase);
foreach (string scope in s_Scopes)
{
if (scopePattern.IsMatch(scope))
{
yield return new CompletionResult(scope);
}
}
}
}
}
@@ -744,6 +744,32 @@ ConstructorTestClass(int i, bool b)
$res.CompletionMatches[0].CompletionText | Should -BeExactly '$TestVar1'
}
Context 'Scope parameter completion' {
BeforeAll {
$allScopes = 'Global Local Script'
$globalScope = 'Global'
$localScope = 'Local'
$scriptScope = 'Script'
$allScopeCommands = 'Clear-Variable', 'Export-Alias', 'Get-Alias', 'Get-PSDrive', 'Get-Variable', 'Import-Alias', 'New-Alias', 'New-PSDrive', 'New-Variable', 'Remove-Alias', 'Remove-PSDrive', 'Remove-Variable', 'Set-Alias', 'Set-Variable'
}
It "Should complete '<ParameterInput>' for '<Commands>'" -TestCases @(
@{ Commands = $allScopeCommands; ParameterInput = "-Scope "; ExpectedScopes = $allScopes }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope G"; ExpectedScopes = $globalScope }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope Lo"; ExpectedScopes = $localScope }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope Scr"; ExpectedScopes = $scriptScope }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope NonExistentScope"; ExpectedScopes = '' }
) {
param($Commands, $ParameterInput, $ExpectedScopes)
foreach ($command in $Commands) {
$joinedCommand = "$command $ParameterInput"
$res = TabExpansion2 -inputScript $joinedCommand -cursorColumn $joinedCommand.Length
$completionText = $res.CompletionMatches.CompletionText | Sort-Object
$completionText -join ' ' | Should -BeExactly $ExpectedScopes
}
}
}
Context 'Get-Verb & Get-Command -Verb parameter completion' {
BeforeAll {
$allVerbs = 'Add Approve Assert Backup Block Build Checkpoint Clear Close Compare Complete Compress Confirm Connect Convert ConvertFrom ConvertTo Copy Debug Deny Deploy Disable Disconnect Dismount Edit Enable Enter Exit Expand Export Find Format Get Grant Group Hide Import Initialize Install Invoke Join Limit Lock Measure Merge Mount Move New Open Optimize Out Ping Pop Protect Publish Push Read Receive Redo Register Remove Rename Repair Request Reset Resize Resolve Restart Restore Resume Revoke Save Search Select Send Set Show Skip Split Start Step Stop Submit Suspend Switch Sync Test Trace Unblock Undo Uninstall Unlock Unprotect Unpublish Unregister Update Use Wait Watch Write'