diff --git a/src/GitTabExpansion.ps1 b/src/GitTabExpansion.ps1 index 9da270bf1..50baa8de2 100644 --- a/src/GitTabExpansion.ps1 +++ b/src/GitTabExpansion.ps1 @@ -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 + } } } } diff --git a/test/TabExpansion.Tests.ps1 b/test/TabExpansion.Tests.ps1 index f265b94cc..1cda7c40a 100644 --- a/test/TabExpansion.Tests.ps1 +++ b/test/TabExpansion.Tests.ps1 @@ -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 '