Skip to content

Commit

Permalink
Use Register-ArgumentCompleter to >= PS 6
Browse files Browse the repository at this point in the history
  • Loading branch information
rkeithhill committed Nov 6, 2019
1 parent 89dafc1 commit 2419069
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
55 changes: 32 additions & 23 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -470,36 +470,45 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) {
}
}

$PowerTab_RegisterTabExpansion = if (Get-Module -Name powertab) { Get-Command Register-TabExpansion -Module powertab -ErrorAction SilentlyContinue }
if ($PowerTab_RegisterTabExpansion) {
& $PowerTab_RegisterTabExpansion "git.exe" -Type Command {
param($Context, [ref]$TabExpansionHasOutput, [ref]$QuoteSpaces) # 1:
if ($PSVersionTable.PSVersion.Major -ge 6) {
Register-ArgumentCompleter -CommandName git,tgit,gitk -Native -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)

$line = $Context.Line
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
$TabExpansionHasOutput.Value = $true
Expand-GitCommand $lastBlock
Expand-GitCommand $commandAst.Extent.Text
}
return
}
else {
$PowerTab_RegisterTabExpansion = if (Get-Module -Name powertab) { Get-Command Register-TabExpansion -Module powertab -ErrorAction SilentlyContinue }
if ($PowerTab_RegisterTabExpansion) {
& $PowerTab_RegisterTabExpansion "git.exe" -Type Command {
param($Context, [ref]$TabExpansionHasOutput, [ref]$QuoteSpaces) # 1:

if (Test-Path Function:\TabExpansion) {
Rename-Item Function:\TabExpansion TabExpansionBackup
}
$line = $Context.Line
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
$TabExpansionHasOutput.Value = $true
Expand-GitCommand $lastBlock
}
return
}

function TabExpansion($line, $lastWord) {
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
if (Test-Path Function:\TabExpansion) {
Rename-Item Function:\TabExpansion TabExpansionBackup
}

switch -regex ($lastBlock) {
# Execute git tab completion for all git-related commands
"^$(Get-AliasPattern git) (.*)" { Expand-GitCommand $lastBlock }
"^$(Get-AliasPattern tgit) (.*)" { Expand-GitCommand $lastBlock }
"^$(Get-AliasPattern gitk) (.*)" { Expand-GitCommand $lastBlock }
function TabExpansion($line, $lastWord) {
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()

# Fall back on existing tab expansion
default {
if (Test-Path Function:\TabExpansionBackup) {
TabExpansionBackup $line $lastWord
switch -regex ($lastBlock) {
# Execute git tab completion for all git-related commands
"^$(Get-AliasPattern git) (.*)" { Expand-GitCommand $lastBlock }
"^$(Get-AliasPattern tgit) (.*)" { Expand-GitCommand $lastBlock }
"^$(Get-AliasPattern gitk) (.*)" { Expand-GitCommand $lastBlock }

# Fall back on existing tab expansion
default {
if (Test-Path Function:\TabExpansionBackup) {
TabExpansionBackup $line $lastWord
}
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions test/TabExpansion.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
. $PSScriptRoot\Shared.ps1

Describe 'TabExpansion Tests' {
It 'Exports a TabExpansion function' {
Describe 'TabExpansion function test' {
BeforeAll {
if ($PSVersionTable.PSVersion.Major -gt 5) {
$PSDefaultParameterValues["it:skip"] = $true
}
}
It 'Windows PowerShell v5 exports a TabExpansion function' {
$module.ExportedFunctions.Keys -contains 'TabExpansion' | Should Be $true
}
}

Describe 'TabExpansion Tests' {
Context 'Subcommand TabExpansion Tests' {
It 'Tab completes without subcommands' {
$result = & $module GitTabExpansionInternal 'git whatever '
Expand Down

0 comments on commit 2419069

Please sign in to comment.