lib/Get-3rdCompletions.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function Get-3rdCompletions([string] $WordToComplete, [string] $Command, [string] $CommandSubPart) {
    if (-not $options) {
        . $PSScriptRoot\..\options.ps1
    }
    if (-not $subCommands) {
        . $PSScriptRoot\..\subCommands.ps1
    }

    $searchBlock = { $_ -like "$WordToComplete*" }
    $completions = @()

    # Command option's values
    if ($options[$Command][$CommandSubPart]) {
        $optionValues = $options[$Command][$CommandSubPart]

        $completions += $optionValues | Where-Object $searchBlock | ForEach-Object {
            [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }

        # Immediately return if it's command's value completion
        return $completions
    }

    # Sub-command's options
    if ($subCommands[$Command][$CommandSubPart]) {
        $subCommand = $subCommands[$Command][$CommandSubPart]
        $subCommandOptions = if ($subCommand.options) { $subCommand.options.Keys } else { @() }

        $completions += $subCommandOptions | Where-Object $searchBlock | ForEach-Object {
            [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_)
        }
    }

    return $completions
}