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

feature/tab-expand-pwsh-alias #779

Merged
merged 31 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
72f70f6
Added proxy command tab expansion with tests.
csc027 Jun 22, 2020
f8d94fb
Separated regular expression into a separate function for reuse. Add…
csc027 Jun 22, 2020
c520b5d
Added explicit parameter names.
csc027 Jun 22, 2020
debc66a
Simplified cleanup in the Expand-GitProxyCommand function.
csc027 Jun 22, 2020
2becd9c
Removed an errant tab in the git proxy command test file.
csc027 Jun 22, 2020
819c6b6
Added a property to the $GitTabSettings object to configure enabling …
csc027 Jun 22, 2020
3e49e09
Removed an unnecessary intermediate variable.
csc027 Jun 22, 2020
30c1e23
Added more tests to cover argument replacement.
csc027 Jun 22, 2020
e387b2d
Fixed a bug where proxy command expansion would be registered on prox…
csc027 Jun 22, 2020
2ce8e5d
Simplified assignment of proxy command names.
csc027 Jun 22, 2020
0f13ad1
Simplified the args regex group in the command match.
csc027 Jun 23, 2020
9ccb7cc
Switched function declarations in the GitProxyCommandExpansion.Test.p…
csc027 Jun 23, 2020
9bfca1f
Switched to script scoped variable for the git proxy command regular …
csc027 Jun 23, 2020
3831893
Fixed a bug in the declaration of the git proxy command regex.
csc027 Jun 23, 2020
c6b1c1a
Disallowed prefix piped commands, since it doesn't make sense.
csc027 Jun 23, 2020
ebf3386
Recombined alias tests, since they were duplicates; cleaned up the se…
csc027 Jun 23, 2020
541d55a
Added tests to cover more edge cases. Updated the proxy command expa…
csc027 Jun 23, 2020
cd8aa43
Fixed a bug where some parameters without whitespace preceding backti…
csc027 Jun 23, 2020
423194b
Updated the git proxy command regular expressions to use negative mat…
csc027 Jun 23, 2020
e6ba17a
Expanded the comment explaining the git proxy command regular express…
csc027 Jun 23, 2020
5e21089
Fixed a bug where the git proxy command expansion was not enabled whe…
csc027 Jun 24, 2020
50b3df9
Switched to function to enable proxy command expansion.
csc027 Jun 24, 2020
fa4412e
Switched to argument list loading the proxy command expansion variable.
csc027 Jun 26, 2020
36fc986
Removed a duplicate test.
csc027 Jun 27, 2020
8492243
Added some comments to explain the Expand-GitProxyCommand function>
csc027 Aug 1, 2020
559cd58
Switched to an if statement to check for aliases, because the Resolve…
csc027 Aug 1, 2020
7d283ca
Switched to camel casing for local scope variables.
csc027 Sep 6, 2020
d6bfb5a
Fixed some formatting.
csc027 Sep 6, 2020
a09ae8c
Renamed variables from GitProxyCommand* to GitProxyFunction* to disam…
csc027 Sep 12, 2020
fec371f
Renamed the GitProxyCommandExpansion.Tests.ps1 test file to GitProxyF…
csc027 Sep 12, 2020
bebace6
Updated the proxy function tests to reflect the rename from *ProxyCom…
csc027 Sep 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ $script:gitCommandsWithParamValues = $gitParamValues.Keys -join '|'
$script:vstsCommandsWithShortParams = $shortVstsParams.Keys -join '|'
$script:vstsCommandsWithLongParams = $longVstsParams.Keys -join '|'

# The regular expression here is roughly follows this pattern:
#
# <begin anchor><whitespace>*<git>(<whitespace><parameter>)*<whitespace>+<$args><whitespace>*<end anchor>
#
# The delimiters inside the parameter list and between some of the elements are non-newline whitespace characters ([^\S\r\n]).
# In those instances, newlines are only allowed if they preceded by a non-newline whitespace character.
#
# Begin anchor (^|[;`n])
# Whitespace (\s*)
# Git Command (?<cmd>$(GetAliasPattern git))
# Parameters (?<params>(([^\S\r\n]|[^\S\r\n]``\r?\n)+\S+)*)
# $args Anchor (([^\S\r\n]|[^\S\r\n]``\r?\n)+\`$args)
# Whitespace (\s|``\r?\n)*
# End Anchor ($|[|;`n])
$script:GitProxyCommandRegex = "(^|[;`n])(\s*)(?<cmd>$(Get-AliasPattern git))(?<params>(([^\S\r\n]|[^\S\r\n]``\r?\n)+\S+)*)(([^\S\r\n]|[^\S\r\n]``\r?\n)+\`$args)(\s|``\r?\n)*($|[|;`n])"
rkeithhill marked this conversation as resolved.
Show resolved Hide resolved

try {
if ($null -ne (git help -a 2>&1 | Select-String flow)) {
$script:someCommands += 'flow'
Expand Down Expand Up @@ -472,6 +488,34 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) {
}
}

function Expand-GitProxyCommand($command) {
# Make sure the incoming command matches: <Command> <Args>, so we can extract the alias/command
# name and the arguments being passed in.
if ($command -notmatch '^(?<command>\S+)([^\S\r\n]|[^\S\r\n]`\r?\n)+(?<args>([^\S\r\n]|[^\S\r\n]`\r?\n|\S)*)$') {
return $command
}

# Store arguments for replacement later
$arguments = $matches['args']

# Get the command name; if an alias exists, get the actual command name
$commandName = $matches['command']
if (Test-Path -Path Alias:\$commandName) {
$commandName = Get-Item -Path Alias:\$commandName | Select-Object -ExpandProperty 'ResolvedCommandName'
}

# Extract definition of git usage
if (Test-Path -Path Function:\$commandName) {
$definition = Get-Item -Path Function:\$commandName | Select-Object -ExpandProperty 'Definition'
if ($definition -match $script:GitProxyCommandRegex) {
# Clean up the command by removing extra delimiting whitespace and backtick preceding newlines
return (("$($matches['cmd'].TrimStart()) $($matches['params']) $arguments") -replace '`\r?\n', ' ' -replace '\s+', ' ')
}
}

return $command
}

function WriteTabExpLog([string] $Message) {
if (!$global:GitTabSettings.EnableLogging) { return }

Expand All @@ -481,6 +525,9 @@ function WriteTabExpLog([string] $Message) {

if (!$UseLegacyTabExpansion -and ($PSVersionTable.PSVersion.Major -ge 6)) {
$cmdNames = "git","tgit","gitk"
if ($EnableProxyCommandExpansion) {
$cmdNames += Get-ChildItem -Path Function:\ | Where-Object { $_.Definition -match $script:GitProxyCommandRegex } | Select-Object -ExpandProperty 'Name'
}
$cmdNames += Get-Alias -Definition $cmdNames -ErrorAction Ignore | ForEach-Object Name

Microsoft.PowerShell.Core\Register-ArgumentCompleter -CommandName $cmdNames -Native -ScriptBlock {
Expand All @@ -491,6 +538,9 @@ if (!$UseLegacyTabExpansion -and ($PSVersionTable.PSVersion.Major -ge 6)) {
# 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)
if ($EnableProxyCommandExpansion) {
$textToComplete = Expand-GitProxyCommand($textToComplete)
}

WriteTabExpLog "Expand: command: '$($commandAst.Extent.Text)', padded: '$textToComplete', padlen: $padLength"
Expand-GitCommand $textToComplete
Expand All @@ -504,6 +554,9 @@ else {

$line = $Context.Line
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
if ($EnableProxyCommandExpansion) {
$lastBlock = Expand-GitProxyCommand($lastBlock)
}
$TabExpansionHasOutput.Value = $true
WriteTabExpLog "PowerTab expand: '$lastBlock'"
Expand-GitCommand $lastBlock
Expand All @@ -514,6 +567,9 @@ else {

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

switch -regex ($lastBlock) {
Expand Down
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, [bool]$UseLegacyTabExpansion)
param([bool]$ForcePoshGitPrompt, [bool]$UseLegacyTabExpansion, [bool]$EnableProxyCommandExpansion)
rkeithhill marked this conversation as resolved.
Show resolved Hide resolved

. $PSScriptRoot\CheckRequirements.ps1 > $null

Expand Down