summaryrefslogtreecommitdiff
path: root/scripts/deploy/completions/powershell.ps1
blob: ec91038dc8f55e4922c130560ee2c5e71e4223c2 (plain)
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
37
38
39
40
41
42
Register-ArgumentCompleter -CommandName jvn -ScriptBlock {
    param($wordToComplete, $commandAst, $cursorPosition)

    $line = $commandAst.ToString()
    $commandName = if ($commandAst.CommandElements.Count -gt 0) {
        $commandAst.CommandElements[0].Value
    } else { "" }

    $words = @()
    $currentIndex = 0
    $parser = [System.Management.Automation.PSParser]
    $tokens = $parser::Tokenize($line, [ref]$null)

    foreach ($token in $tokens) {
        if ($token.Type -in 'CommandArgument', 'CommandParameter') {
            $words += $token.Content
        }
    }

    $args = @(
        "-f", ($line -replace '-', '^')
        "-C", $cursorPosition.ToString()
        "-w", ($wordToComplete -replace '-', '^')
        "-p", (if ($words.Count -gt 1) { $words[-2] } else { "" }) -replace '-', '^'
        "-c", $commandName
        "-i", ($words.Count - 1).ToString()
        "-a", ($words | ForEach-Object { $_ -replace '-', '^' })
    )

    $suggestions = jvn_comp $args 2>$null

    if ($suggestions) {
        $suggestions | ForEach-Object {
            if ($_ -eq "_file_") {
                $completionType = 'ProviderItem'
            } else {
                $completionType = 'ParameterValue'
            }
            [System.Management.Automation.CompletionResult]::new($_, $_, $completionType, $_)
        }
    }
}