Skip to content

Instantly share code, notes, and snippets.

@bbarry
Created August 16, 2018 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbarry/9ba042dca5da21be8851b3d086b21bd3 to your computer and use it in GitHub Desktop.
Save bbarry/9ba042dca5da21be8851b3d086b21bd3 to your computer and use it in GitHub Desktop.
mercurial argument completion, works in powershell 5.1, should work with TabExpansionPlusPlus; faster (and more limited) than https://github.com/JeremySkinner/posh-hg
# http://www.wtfpl.net/txt/copying/
function isHgDirectory() {
if(test-path ".hg") {
return (Get-Item .).fullname
}
$d = (Get-Item .).parent
while ($d -ne $NULL) {
$p = $d.fullname + "\.hg"
if (Test-Path $p) {
return $d.fullname
} else {
$d = $d.parent
}
}
return $false
}
function hgStatus($filter) {
$hgdir = isHgDirectory
$arg = '-' + $filter
if ($hgdir) {
(hg st -n $arg) | ForEach-Object {
$p = Join-Path $hgdir $_
[System.Management.Automation.CompletionResult]::new($p, $p, 'ParameterValue', $p)
}
}
}
$global:hgcommandset = (hg debugcomplete) | Sort-Object -Unique
function HgArgumentCompletion {
param($wordToComplete, $commandAst, $cursorPosition)
$ct = $commandAst.CommandElements.Count
if ($ct -eq 1) {
$hgcommandset | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
} elseif ($ct -eq 2 -and $commandAst.CommandElements[1].ToString() -eq $wordToComplete) {
$hgcommandset | Where-Object { $_ -like "${wordToComplete}*" }| ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
} elseif ($ct -gt 2 -and $wordToComplete -match '^[mardcui]+$') {
hgStatus $wordToComplete
} else {
$cmd = $commandAst.CommandElements[1].ToString()
$c = "${wordToComplete}*"
if ($c -eq "*") {
$c = "--*"
}
(hg debugcomplete -o $cmd) | Sort-Object -Unique | Where-Object { $_ -like $c }| ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
Register-ArgumentCompleter -Native -CommandName hg -ScriptBlock $function:HgArgumentCompletion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment