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

Enable custom completers for complex user aliases #973

Closed
wants to merge 3 commits into from
Closed
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
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,53 @@ function prompt {
}
```

## Creating custom completers for complex aliases

`posh-git` will search the `$env:PSModulePath` for modules whose name match `posh-git-extras-*` and use any
function in the found modules that has the name `GitTabCustomExpansion` and expects a single parameter of type
`[string]`.

These functions receive the command as written on the console, stripped by the `git` binary command
and should return/write custom suggestions.

Here's how you can do it:

```
# Configure an alias:
> git config --global alias.wttr '!f() { curl https://wttr.in/$1; }; f'

# Find your PS-Module-Paths:
> $env:PSModulePath
C:\Users\me\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules
```

You probably want to select a folder in your user space. In the `Modules` folder, create another folder,
e.g. `posh-git-extras-aliases` and in that folder a file named `posh-git-extras-aliases.psm1`.

In this module file, you must declare and export the function `GitTabCustomExpansion`:

```
# C:\Users\me\Documents\WindowsPowerShell\Modules\posh-git-extras-aliases\posh-git-extras-aliases.psm1

function GitTabCustomExpansion()
{
param(
[string]$gitCommandBlock
)

switch -regex ($gitCommandBlock) {
"^wttr\s+(?<loc>[\S]*)$" {
@('London', 'Berlin', 'Amsterdam') |
Where-Object { $_ -like "$($Matches['loc'])*" }
}
}
}

Export-ModuleMember -Function GitTabCustomExpansion

```


## Based on work by

- Keith Dahlby, http://solutionizing.net/
Expand Down
22 changes: 11 additions & 11 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,6 @@ catch {
Write-Debug "Search for 'flow' in 'git help' output failed with error: $_"
}

filter quoteStringWithSpecialChars {
if ($_ -and ($_ -match '\s+|#|@|\$|;|,|''|\{|\}|\(|\)')) {
$str = $_ -replace "'", "''"
"'$str'"
}
else {
$_
}
}

function script:gitCommands($filter, $includeAliases) {
$cmdList = @()
Expand Down Expand Up @@ -307,10 +298,12 @@ function Expand-GitCommand($Command) {
$res
}

function GitTabExpansionInternal($lastBlock, $GitStatus = $null) {
function GitTabExpansionInternal($Command, $GitStatus = $null) {
$ignoreGitParams = '(?<params>\s+-(?:[aA-zZ0-9]+|-[aA-zZ0-9][aA-zZ0-9-]*)(?:=\S+)?)*'

if ($lastBlock -match "^$(Get-AliasPattern git) (?<cmd>\S+)(?<args> .*)$") {
$lastBlock = $Command
if ($Command -match "^$(Get-AliasPattern git) (?<cmd>\S+)(?<args> .*)$") {
$Command = $Matches['cmd'] + " " + $Matches['args']
$lastBlock = expandGitAlias $Matches['cmd'] $Matches['args']
}

Expand Down Expand Up @@ -499,6 +492,13 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) {
{
expandShortParams $shortVstsParams $matches['cmd'] $matches['shortparam']
}

# Handles any other command
default {
$extraGitTabExpansions | ForEach-Object {
& $_ $Command
}
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/Utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,13 @@ function dbg($Message, [Diagnostics.Stopwatch]$Stopwatch) {
Write-Verbose ('{0:00000}:{1}' -f $Stopwatch.ElapsedMilliseconds,$Message) -Verbose # -ForegroundColor Yellow
}
}

filter global:quoteStringWithSpecialChars {
if ($_ -and ($_ -match '\s+|#|@|\$|;|,|''|\{|\}|\(|\)')) {
$str = $_ -replace "'", "''"
"'$str'"
}
else {
$_
}
}
13 changes: 13 additions & 0 deletions src/posh-git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ if (Test-Path Env:\POSHGIT_ENABLE_STRICTMODE) {

. $PSScriptRoot\CheckRequirements.ps1 > $null

# Load extra modules with user customized configuration
$extraGitTabExpansions = @()
Get-Module -ListAvailable posh-git-extras-* | ForEach-Object {
Import-Module -Force $_
$local:expandGitCommand = Get-Command -Module $_ -Name "GitTabCustomExpansion" -All
$local:params = $expandGitCommand.Parameters

if ($params.Count -eq 1 -and
$params["gitCommandBlock"].ParameterType -eq [string]) {
$extraGitTabExpansions += ($expandGitCommand)
}
}

. $PSScriptRoot\ConsoleMode.ps1
. $PSScriptRoot\Utils.ps1
. $PSScriptRoot\AnsiUtils.ps1
Expand Down
Loading