Skip to content

Commit

Permalink
Merge pull request #741 from dahlbyk/rkeithhill/fix-new-tab-expansion
Browse files Browse the repository at this point in the history
Fix tab-completion regression caused by switch to Register-ArgumentCompleter
  • Loading branch information
rkeithhill authored Feb 24, 2020
2 parents 8fc8e8e + dca5b4a commit bce6e48
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
21 changes: 15 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
Thanks @pinkfloydx33 for the direction and motivation to add this.
- Feature request: abbreviate path from git root with new setting `DefaultPromptAbbreviateGitDirectory`
([#719](https://github.com/dahlbyk/posh-git/issues/719))
([PR #720](https://github.com/dahlbyk/posh-git/pull/720)
([PR #720](https://github.com/dahlbyk/posh-git/pull/720))
Thanks Philippe Elsass (@elsassph)
- Two new properties have been added to `$global:GitTabSettings`
- `EnableLogging` - default is `$false` but when set to `$true`, tab expansion functions log to a file.
- `LogPath` - the path to the log file that is appended to (created if necessary) when `EnableLogging` is `$true`.

### Changed

Expand All @@ -29,7 +32,10 @@
- Shortened up the Windows title text to work better with Windows Terminal tabs. Now only displays '32-bit' in 32-bit
PowerShell. Otherwise, assumption is you're running 64-bit. Also display only PowerShell major.minor version number.
([PR #707](https://github.com/dahlbyk/posh-git/pull/707))
- Switched from overriding `TabExpansion` function to using `Register-ArgumentCompleter` for tab-completion.
- Switched from overriding `TabExpansion` function to using `Register-ArgumentCompleter` for tab-completion on
PowerShell >= 6.0. `posh-git` can be configured to use the old `TabExpansion` function on PowerShell >= 6.0 by
importing the module using the following arguments: `Import-Module posh-git -ArgumentList 0,1`
before importing `posh-git`.
([PR #609](https://github.com/dahlbyk/posh-git/pull/609))
Thanks Andrew Bradley (@cspotcode)
- Update Ubuntu build system from 14.04 to 16.04
Expand Down Expand Up @@ -58,20 +64,23 @@
([PR #675](https://github.com/dahlbyk/posh-git/pull/675))
Thanks @KexyBiscuit
- Fix bug w/multiple completions of name parameter on remove-gitbranch
([PR #705](https://github.com/dahlbyk/posh-git/pull/705)
([PR #705](https://github.com/dahlbyk/posh-git/pull/705))
- Prompt error in remote PSSession.
([#708](https://github.com/dahlbyk/posh-git/issues/708))
([PR #727](https://github.com/dahlbyk/posh-git/pull/727)
- Fix conflict with `TabExpansionPlusPlus` module's `Register-ArgumentCompleter` command.
([#715](https://github.com/dahlbyk/posh-git/issues/715))
([PR #714](https://github.com/dahlbyk/posh-git/pull/714)
([PR #714](https://github.com/dahlbyk/posh-git/pull/714))
Thanks Kris Borowinski (@kborowinski)
- Typo in CHANGELOG.md file: Git-RemoteBranch to Git-RemoveBranch.
([PR #716](https://github.com/dahlbyk/posh-git/pull/716)
([PR #716](https://github.com/dahlbyk/posh-git/pull/716))
Thanks @theaquamarine
- posh-git messes with the HOME environment variable.
([#718](https://github.com/dahlbyk/posh-git/issues/718))
([PR #722](https://github.com/dahlbyk/posh-git/pull/722)
([PR #722](https://github.com/dahlbyk/posh-git/pull/722))
- Fix tab-completion regression with new `Register-ArgumentCompleter`.
([#733](https://github.com/dahlbyk/posh-git/issues/733))
([PR #738](https://github.com/dahlbyk/posh-git/pull/738))

## 1.0.0-beta3 - March 10, 2019

Expand Down
43 changes: 26 additions & 17 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ $Global:GitTabSettings = New-Object PSObject -Property @{
KnownAliases = @{
'!f() { exec vsts code pr "$@"; }; f' = 'vsts.pr'
}
EnableLogging = $false
LogPath = Join-Path ([System.IO.Path]::GetTempPath()) posh-git_tabexp.log
}

$subcommands = @{
Expand Down Expand Up @@ -470,52 +472,59 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) {
}
}

if ($PSVersionTable.PSVersion.Major -ge 6) {
function WriteTabExpLog([string] $Message) {
if (!$global:GitTabSettings.EnableLogging) { return }

$timestamp = Get-Date -Format HH:mm:ss
"[$timestamp] $Message" | Out-File -Append $global:GitTabSettings.LogPath
}

if (!$UseLegacyTabExpansion -and ($PSVersionTable.PSVersion.Major -ge 6)) {
Microsoft.PowerShell.Core\Register-ArgumentCompleter -CommandName git,tgit,gitk -Native -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)

Expand-GitCommand $commandAst.Extent.Text
# The PowerShell completion has a habit of stripping the trailing space when completing:
# git checkout <tab>
# The Expand-GitCommand expects this trailing space, so pad with a space if necessary.
$padLength = $cursorPosition - $commandAst.Extent.StartOffset
$textToComplete = $commandAst.ToString().PadRight($padLength, ' ').Substring(0, $padLength)

WriteTabExpLog "Expand: command: '$($commandAst.Extent.Text)', padded: '$textToComplete', padlen: $padLength"
Expand-GitCommand $textToComplete
}
}
else {
$PowerTab_RegisterTabExpansion = if (Get-Module -Name powertab) { Get-Command Register-TabExpansion -Module powertab -ErrorAction SilentlyContinue }
if ($PowerTab_RegisterTabExpansion) {
& $PowerTab_RegisterTabExpansion git -Type Command {
param($Context, [ref]$TabExpansionHasOutput, [ref]$QuoteSpaces) # 1:
param($Context, [ref]$TabExpansionHasOutput, [ref]$QuoteSpaces)

$line = $Context.Line
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
$TabExpansionHasOutput.Value = $true
WriteTabExpLog "PowerTab expand: '$lastBlock'"
Expand-GitCommand $lastBlock
}
return
}

if (Test-Path Function:\TabExpansion) {
Rename-Item Function:\TabExpansion TabExpansionBackup
return
}

function TabExpansion($line, $lastWord) {
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
$msg = "Legacy expand: '$lastBlock'"

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
}
}
"^$(Get-AliasPattern git) (.*)" { WriteTabExpLog $msg; Expand-GitCommand $lastBlock }
"^$(Get-AliasPattern tgit) (.*)" { WriteTabExpLog $msg; Expand-GitCommand $lastBlock }
"^$(Get-AliasPattern gitk) (.*)" { WriteTabExpLog $msg; Expand-GitCommand $lastBlock }
}
}
}

# Handles Remove-GitBranch -Name parameter auto-completion using the built-in mechanism for cmdlet parameters
Microsoft.PowerShell.Core\Register-ArgumentCompleter -CommandName Remove-GitBranch -ParameterName Name -ScriptBlock {
param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams)

gitBranches $WordToComplete $true
}
2 changes: 1 addition & 1 deletion src/posh-git.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
param([bool]$ForcePoshGitPrompt)
param([bool]$ForcePoshGitPrompt, [bool]$UseLegacyTabExpansion)

. $PSScriptRoot\CheckRequirements.ps1 > $null

Expand Down

0 comments on commit bce6e48

Please sign in to comment.