Skip to content

Commit 696885a

Browse files
committed
Use Register-ArgumentCompleter to >= PS 6
1 parent 89dafc1 commit 696885a

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

src/GitTabExpansion.ps1

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -470,36 +470,45 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) {
470470
}
471471
}
472472

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

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

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

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

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

499-
# Fall back on existing tab expansion
500-
default {
501-
if (Test-Path Function:\TabExpansionBackup) {
502-
TabExpansionBackup $line $lastWord
501+
switch -regex ($lastBlock) {
502+
# Execute git tab completion for all git-related commands
503+
"^$(Get-AliasPattern git) (.*)" { Expand-GitCommand $lastBlock }
504+
"^$(Get-AliasPattern tgit) (.*)" { Expand-GitCommand $lastBlock }
505+
"^$(Get-AliasPattern gitk) (.*)" { Expand-GitCommand $lastBlock }
506+
507+
# Fall back on existing tab expansion
508+
default {
509+
if (Test-Path Function:\TabExpansionBackup) {
510+
TabExpansionBackup $line $lastWord
511+
}
503512
}
504513
}
505514
}

test/TabExpansion.Tests.ps1

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
. $PSScriptRoot\Shared.ps1
22

3-
Describe 'TabExpansion Tests' {
4-
It 'Exports a TabExpansion function' {
3+
Describe 'TabExpansion function test' {
4+
BeforeAll {
5+
if ($PSVersionTable.PSVersion.Major -eq 5) {
6+
$PSDefaultParameterValues["it:skip"] = $true
7+
}
8+
}
9+
It 'Windows PowerShell v5 exports a TabExpansion function' {
510
$module.ExportedFunctions.Keys -contains 'TabExpansion' | Should Be $true
611
}
12+
}
13+
14+
Describe 'TabExpansion Tests' {
715
Context 'Subcommand TabExpansion Tests' {
816
It 'Tab completes without subcommands' {
917
$result = & $module GitTabExpansionInternal 'git whatever '

0 commit comments

Comments
 (0)