Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Register-ArgumentCompleter to >= PS 6 #711

Merged
merged 1 commit into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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