-
-
Notifications
You must be signed in to change notification settings - Fork 802
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
Pass PowerShell aliases down to posh-git TabExpansion autocomplete function #257
Comments
Working through open issues and realized that I never commented. This is a great idea and I would totally accept a PR for a generalized version of this. |
I can easily see aliases working, but functions are tricky. For aliases, we just need to insert Assuming we have function git-checkout { git checkout $args }
set-alias gco git-checkout I have no idea how I would interpret that. Would I grab the ScriptBlock to do a find/replace? But what if it's multiple lines? What if I executed something in the ScriptBlock? Well we can't run the function as is because we aren't ready to execute. We still want tab completion. But what if the > function git-checkout { "git checkout" }
> set-alias gco git-checkout Then we could run the following: $command = (Get-Alias $alias).Definition
if (Test-Path Function:/$command) { # Check if the command is actually a function
$command = & $alias # execute
} Since we are now only dealing with strings we could just create a "dictionary file", kind of like from Thoughts? |
posh-git wouldn't be responsible for executing the function, we just need to tease out the definition. You can get the function definition like this. I would try a regex to match the definition on |
What would you do if there were multiple lines in the definition? |
Similar to how we parse This would be a great opportunity to TDD handling different function definitions. 😀 |
I would grab this, since I realized something similiar some months ago. |
I'd also love to see this! I alias |
If |
@dahlbyk Correct. Although, I was declaring my alias with |
@dahlbyk I have the exact same setup, i.e. having |
Also, if I set the |
@szelpe Your bug is a little different since you alias directly to the git executable as opposed to aliasing to a custom function. Can you submit a new issue? It turns out PowerShell's built-in Register-ArgumentCompleter does not take into account aliases to native apps that have registered completers. I think the fix for this is pretty easy. |
Sure: #769 |
How can do that? |
$scriptBlock = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
Expand-GitCommand 'git checkout -b '
}
Register-ArgumentCompleter -CommandName gcob -ScriptBlock $scriptBlock |
@MisinformedDNA |
Try this: $scriptBlock = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
Expand-GitCommand "git checkout -b $commandName"
}
Register-ArgumentCompleter -CommandName gcob -ScriptBlock $scriptBlock |
@MisinformedDNA Thank you and it's working fine
like this Now my profile
|
Yes, you need separate script blocks for each one. |
@MisinformedDNA gotcha! |
I find that passing in a parameter doesn't work correctly in certain scenarios. For instance, if you run this: function gl {
param ($commandComplete)
git log $commandComplete
}
$scriptBlock = {
param($wordToComplete, $commandAst, $cursorPosition)
Expand-GitCommand "git log $wordToComplete"
}
Register-ArgumentCompleter -Native -CommandName gl -ScriptBlock $scriptBlock
gl-<TAB> You will get
Instead, I use: function gl { git log @args }
$scriptBlock = {
param($wordToComplete, $commandAst, $cursorPosition)
Expand-GitCommand "git log $wordToComplete"
}
Register-ArgumentCompleter -Native -CommandName gl -ScriptBlock $scriptBlock NOTE: I've also explicitly set the native parameter (even though it is implied) and changed the names of the script parameters to reflect the native parameters. |
The code below works well for me. It will complete the parameter of git subcommand or subsubcommand correctly. function RegisterGit {
Invoke-Expression -Command "function global:$($args[0]) { git $($args[1]) @args }"
$tab = [Scriptblock]::Create("
param(`$wordToComplete, `$commandAst, `$cursorPosition)
`$cmdline = `$commandAst.ToString().Replace(`"$($args[0])`", `"`")
if (`$wordToComplete.Length -ne 0) { `$tail = '' } else { `$tail = ' ' }
Expand-GitCommand `"git $($args[1]) `$cmdline`$tail`"
")
Register-ArgumentCompleter -CommandName $args[0] -ScriptBlock $tab
}
# define function gst to execute git remote, and register the tab completer for it.
RegisterGit gr "remote" |
I have PS aliases for git, defined as such:
The
$args
represents a branch name, which posh-git would typically autocomplete. However, since aliases are not passed down to posh-git, autocompletion doesn't work.I monkey-patched this functionality myself, using the technique described in this SO answer:
It'd be super rad if posh-git could somehow retrieve existing PS aliases related to git functionality like this and patch it into posh-git's autocompletion functionality.
I prefer to use PS aliases over git aliases, primarily for brevity - I'm able to do
gcm
instead ofgit co
or similar.The text was updated successfully, but these errors were encountered: