Adding IArgumentCompleterFactory for parameterized ArgumentCompleters (#12605)

This commit is contained in:
Staffan Gustafsson
2021-03-25 17:06:40 -07:00
committed by GitHub
parent 5d0dadf19d
commit be7d36603d
3 changed files with 163 additions and 12 deletions
@@ -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 = @(