From fb14c901857cdca55cea105973a1997d87f8c8cc Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Thu, 20 Feb 2020 18:47:13 -0700 Subject: [PATCH 1/2] Enable tab expansion logging --- src/GitTabExpansion.ps1 | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/GitTabExpansion.ps1 b/src/GitTabExpansion.ps1 index 3b5fb8c1d..669a762c5 100644 --- a/src/GitTabExpansion.ps1 +++ b/src/GitTabExpansion.ps1 @@ -1,6 +1,8 @@ # Initial implementation by Jeremy Skinner # http://www.jeremyskinner.co.uk/2010/03/07/using-git-with-windows-powershell/ +$tabLogPath = Join-Path ([System.IO.Path]::GetTempPath()) posh-git_tabexp.log + $Global:GitTabSettings = New-Object PSObject -Property @{ AllCommands = $false KnownAliases = @{ @@ -470,10 +472,13 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) { } } -if ($PSVersionTable.PSVersion.Major -ge 6) { +if (!$env:PoshGitUseLegacyTabExpansion -and ($PSVersionTable.PSVersion.Major -ge 6)) { Microsoft.PowerShell.Core\Register-ArgumentCompleter -CommandName git,tgit,gitk -Native -ScriptBlock { param($wordToComplete, $commandAst, $cursorPosition) + $trimToLength = $cursorPosition - $commandAst.Extent.StartOffset + $alt = $commandAst.toString().PadRight($trimToLength, ' ').substring(0, $trimToLength) + "[$(Get-Date -Format HH:mm:ss)] New tab exp: '$($commandAst.Extent.Text)' - alt: '$alt'" | Out-File -Append $tabLogPath Expand-GitCommand $commandAst.Extent.Text } } @@ -492,12 +497,16 @@ else { } if (Test-Path Function:\TabExpansion) { + "[$(Get-Date -Format HH:mm:ss)] Old tab exp: backing up TabExpansion2 to TabExpansionBackup" | Out-File -Append $tabLogPath Rename-Item Function:\TabExpansion TabExpansionBackup + "[$(Get-Date -Format HH:mm:ss)] Old tab exp: dir $(Get-ChildItem function:\Tab* | % Name)" | Out-File -Append $tabLogPath } function TabExpansion($line, $lastWord) { $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart() + "[$(Get-Date -Format HH:mm:ss)] Old tab exp: '$lastBlock'" | Out-File -Append $tabLogPath + switch -regex ($lastBlock) { # Execute git tab completion for all git-related commands "^$(Get-AliasPattern git) (.*)" { Expand-GitCommand $lastBlock } @@ -506,8 +515,10 @@ else { # Fall back on existing tab expansion default { + "[$(Get-Date -Format HH:mm:ss)] Old tab exp: fallback to default case" | Out-File -Append $tabLogPath if (Test-Path Function:\TabExpansionBackup) { - TabExpansionBackup $line $lastWord + "[$(Get-Date -Format HH:mm:ss)] Old tab exp: line: '$line' lastWord: '$lastWord'" | Out-File -Append $tabLogPath + TabExpansionBackup -InputScript $line $lastWord } } } From dca5b4a198b12c3cf1090e752dffd27a986d7d87 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Thu, 20 Feb 2020 22:09:32 -0700 Subject: [PATCH 2/2] Fix tab-completion bug due to Reg-ArgCompleter stripping trailing space --- CHANGELOG.md | 21 +++++++++++----- src/GitTabExpansion.ps1 | 54 ++++++++++++++++++++--------------------- src/posh-git.psm1 | 2 +- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e3dbc20f..5fdde9f47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 @@ -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 diff --git a/src/GitTabExpansion.ps1 b/src/GitTabExpansion.ps1 index 669a762c5..14a5bfa33 100644 --- a/src/GitTabExpansion.ps1 +++ b/src/GitTabExpansion.ps1 @@ -1,13 +1,13 @@ # Initial implementation by Jeremy Skinner # http://www.jeremyskinner.co.uk/2010/03/07/using-git-with-windows-powershell/ -$tabLogPath = Join-Path ([System.IO.Path]::GetTempPath()) posh-git_tabexp.log - $Global:GitTabSettings = New-Object PSObject -Property @{ AllCommands = $false KnownAliases = @{ '!f() { exec vsts code pr "$@"; }; f' = 'vsts.pr' } + EnableLogging = $false + LogPath = Join-Path ([System.IO.Path]::GetTempPath()) posh-git_tabexp.log } $subcommands = @{ @@ -472,55 +472,52 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) { } } -if (!$env:PoshGitUseLegacyTabExpansion -and ($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) - $trimToLength = $cursorPosition - $commandAst.Extent.StartOffset - $alt = $commandAst.toString().PadRight($trimToLength, ' ').substring(0, $trimToLength) - "[$(Get-Date -Format HH:mm:ss)] New tab exp: '$($commandAst.Extent.Text)' - alt: '$alt'" | Out-File -Append $tabLogPath - Expand-GitCommand $commandAst.Extent.Text + # The PowerShell completion has a habit of stripping the trailing space when completing: + # git checkout + # 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) { - "[$(Get-Date -Format HH:mm:ss)] Old tab exp: backing up TabExpansion2 to TabExpansionBackup" | Out-File -Append $tabLogPath - Rename-Item Function:\TabExpansion TabExpansionBackup - "[$(Get-Date -Format HH:mm:ss)] Old tab exp: dir $(Get-ChildItem function:\Tab* | % Name)" | Out-File -Append $tabLogPath + return } function TabExpansion($line, $lastWord) { $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart() - - "[$(Get-Date -Format HH:mm:ss)] Old tab exp: '$lastBlock'" | Out-File -Append $tabLogPath + $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 { - "[$(Get-Date -Format HH:mm:ss)] Old tab exp: fallback to default case" | Out-File -Append $tabLogPath - if (Test-Path Function:\TabExpansionBackup) { - "[$(Get-Date -Format HH:mm:ss)] Old tab exp: line: '$line' lastWord: '$lastWord'" | Out-File -Append $tabLogPath - TabExpansionBackup -InputScript $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 } } } } @@ -528,5 +525,6 @@ else { # 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 } diff --git a/src/posh-git.psm1 b/src/posh-git.psm1 index a9e090ab7..df90b0903 100644 --- a/src/posh-git.psm1 +++ b/src/posh-git.psm1 @@ -1,4 +1,4 @@ -param([bool]$ForcePoshGitPrompt) +param([bool]$ForcePoshGitPrompt, [bool]$UseLegacyTabExpansion) . $PSScriptRoot\CheckRequirements.ps1 > $null