Completers/Completers.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

using namespace System.Management.Automation;

$revisionCompleterIncludingRemoteBranches = {
    [CmdletBinding()]
    param([string]$command, [string]$parameter, [string]$wordToComplete, [CommandAst]$commandAst, [Hashtable]$params)

    (
        (
            Get-GitBranch -All |
                Where-Object {
                    $_.FriendlyName -like "$wordToComplete*" -or
                    ($_.IsRemote -and ($_.FriendlyName -creplace "^$($_.RemoteName)/", "") -like "$wordToComplete*")
                }
        ),
        (Get-GitTag -Name "$wordToComplete*")
    ) |
        ForEach-Object { $_ } |
        ForEach-Object {
            $suggestion = if ($_.IsRemote -and -not ($wordToComplete -like "$($_.RemoteName)*")) {
                ($_.FriendlyName -creplace "^$($_.RemoteName)/", "")
            } else {
                $_.FriendlyName
            }
            $tooltip = "$($_.FriendlyName) → $($_.Tip.Sha.Substring(0, 7)) $($_.Tip.MessageShort.Trim())"
            [CompletionResult]::new($suggestion, $suggestion, [CompletionResultType]::ParameterValue, $tooltip)
        }
}
Register-ArgumentCompleter -CommandName Set-GitHead -ParameterName Revision -ScriptBlock $revisionCompleterIncludingRemoteBranches
Register-ArgumentCompleter -CommandName Reset-GitHead -ParameterName Revision -ScriptBlock $revisionCompleterIncludingRemoteBranches

$branchCompleter = {
    [CmdletBinding()]
    param([string]$command, [string]$parameter, [string]$wordToComplete, [CommandAst]$commandAst, [Hashtable]$params)
    Get-GitBranch -All -Name "$wordToComplete*" |
        ForEach-Object {
            $tooltip = "$($_.FriendlyName) → $($_.Tip.Sha.Substring(0, 7)) $($_.Tip.MessageShort.Trim())"
            [CompletionResult]::new($_.FriendlyName, $_.FriendlyName, [CompletionResultType]::ParameterValue, $tooltip)
        }
}
Register-ArgumentCompleter -CommandName Remove-GitBranch -ParameterName Name -ScriptBlock $branchCompleter
Register-ArgumentCompleter -CommandName Get-GitBranch -ParameterName Name -ScriptBlock $branchCompleter

$revisionCompleter = {
    [CmdletBinding()]
    param([string]$command, [string]$parameter, [string]$wordToComplete, [CommandAst]$commandAst, [Hashtable]$params)

    (Get-GitBranch -All -Name "$wordToComplete*"), (Get-GitTag -Name "$wordToComplete*") |
        ForEach-Object { $_ } |
        ForEach-Object {
            $tooltip = "$($_.FriendlyName) → $($_.Tip.Sha.Substring(0, 7)) $($_.Tip.MessageShort.Trim())"
            [CompletionResult]::new($_.FriendlyName, $_.FriendlyName, [CompletionResultType]::ParameterValue, $tooltip)
        }
}
Register-ArgumentCompleter -CommandName Merge-GitCommit -ParameterName Revision -ScriptBlock $revisionCompleter
Register-ArgumentCompleter -CommandName Get-GitCommit -ParameterName Revision -ScriptBlock $revisionCompleter
Register-ArgumentCompleter -CommandName Start-GitRebase -ParameterName Branch -ScriptBlock $revisionCompleter
Register-ArgumentCompleter -CommandName Start-GitRebase -ParameterName Upstream -ScriptBlock $revisionCompleter
Register-ArgumentCompleter -CommandName Start-GitRebase -ParameterName Onto -ScriptBlock $revisionCompleter
Register-ArgumentCompleter -CommandName Compare-GitTree -ParameterName DifferenceRevision -ScriptBlock $revisionCompleter
Register-ArgumentCompleter -CommandName Compare-GitTree -ParameterName ReferenceRevision -ScriptBlock $revisionCompleter

$remoteCompleter = {
    [CmdletBinding()]
    param([string]$command, [string]$parameter, [string]$wordToComplete, [CommandAst]$commandAst, [Hashtable]$params)
    (Get-GitRemote -Name "$wordToComplete*") |
        ForEach-Object {
            [CompletionResult]::new($_.Name, $_.Name, [CompletionResultType]::ParameterValue, "$($_.Name) → $($_.Url)")
        }
}
Register-ArgumentCompleter -CommandName Send-GitObject -ParameterName Remote -ScriptBlock $remoteCompleter
Register-ArgumentCompleter -CommandName Send-GitBranch -ParameterName Remote -ScriptBlock $remoteCompleter
Register-ArgumentCompleter -CommandName Receive-GitObject -ParameterName Remote -ScriptBlock $remoteCompleter
Register-ArgumentCompleter -CommandName Get-GitRemote -ParameterName Name -ScriptBlock $remoteCompleter
Register-ArgumentCompleter -CommandName Update-GitRemote -ParameterName Name -ScriptBlock $remoteCompleter
Register-ArgumentCompleter -CommandName Remove-GitRemote -ParameterName Name -ScriptBlock $remoteCompleter
Register-ArgumentCompleter -CommandName Rename-GitRemote -ParameterName Name -ScriptBlock $remoteCompleter