mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Adding IArgumentCompleterFactory for parameterized ArgumentCompleters (#12605)
This commit is contained in:
@@ -175,6 +175,77 @@ function TestFunction
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
class NumberCompleter : IArgumentCompleter
|
||||
{
|
||||
|
||||
[int] $From
|
||||
[int] $To
|
||||
[int] $Step
|
||||
|
||||
NumberCompleter([int] $from, [int] $to, [int] $step)
|
||||
{
|
||||
if ($from -gt $to) {
|
||||
throw [ArgumentOutOfRangeException]::new("from")
|
||||
}
|
||||
$this.From = $from
|
||||
$this.To = $to
|
||||
$this.Step = if($step -lt 1) { 1 } else { $step }
|
||||
}
|
||||
|
||||
[IEnumerable[CompletionResult]] CompleteArgument(
|
||||
[string] $CommandName,
|
||||
[string] $parameterName,
|
||||
[string] $wordToComplete,
|
||||
[CommandAst] $commandAst,
|
||||
[IDictionary] $fakeBoundParameters)
|
||||
{
|
||||
$resultList = [List[CompletionResult]]::new()
|
||||
$local:to = $this.To
|
||||
for ($i = $this.From; $i -le $to; $i += $this.Step) {
|
||||
if ($i.ToString().StartsWith($wordToComplete, [System.StringComparison]::Ordinal)) {
|
||||
$num = $i.ToString()
|
||||
$resultList.Add([CompletionResult]::new($num, $num, "ParameterValue", $num))
|
||||
}
|
||||
}
|
||||
|
||||
return $resultList
|
||||
}
|
||||
}
|
||||
|
||||
class NumberCompletionAttribute : ArgumentCompleterAttribute, IArgumentCompleterFactory
|
||||
{
|
||||
[int] $From
|
||||
[int] $To
|
||||
[int] $Step
|
||||
|
||||
NumberCompletionAttribute([int] $from, [int] $to)
|
||||
{
|
||||
$this.From = $from
|
||||
$this.To = $to
|
||||
$this.Step = 1
|
||||
}
|
||||
|
||||
[IArgumentCompleter] Create() { return [NumberCompleter]::new($this.From, $this.To, $this.Step) }
|
||||
}
|
||||
|
||||
function FactoryCompletionAdd {
|
||||
param(
|
||||
[NumberCompletion(0, 50, Step = 5)]
|
||||
[int] $Number
|
||||
)
|
||||
}
|
||||
|
||||
Describe "Factory based extensible completion" -Tags "CI" {
|
||||
@{
|
||||
ExpectedResults = @(
|
||||
@{CompletionText = "5"; ResultType = "ParameterValue" }
|
||||
@{CompletionText = "50"; ResultType = "ParameterValue" }
|
||||
)
|
||||
TestInput = 'FactoryCompletionAdd -Number 5'
|
||||
} | Get-CompletionTestCaseData | Test-Completions
|
||||
}
|
||||
|
||||
Describe "Script block based extensible completion" -Tags "CI" {
|
||||
@{
|
||||
ExpectedResults = @(
|
||||
|
||||
Reference in New Issue
Block a user