-
Notifications
You must be signed in to change notification settings - Fork 142
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
Consolidate zsh completions in one file for homebrew #340
Conversation
@@ -336,8 +336,7 @@ export FORGIT_LOG_FZF_OPTS=' | |||
|
|||
## Zsh | |||
|
|||
- Put [`completions/_git-forgit`](completions/_git-forgit) in a directory in your `$fpath` (e.g. `/usr/share/zsh/site-functions`) to have zsh tab completion for `git forgit` and configured git aliases. | |||
- Source [`completions/git-forgit.zsh`](completions/git-forgit.zsh) to have zsh tab completion for forgit shell functions and aliases (e.g. `gcb <tab>` completes branches). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two files were essentially merged into a single file
completions/_git-forgit
Outdated
@@ -1,12 +1,10 @@ | |||
#compdef git-forgit | |||
#compdef git-forgit forgit::add forgit::branch::delete forgit::checkout::branch forgit::checkout::commit forgit::checkout::file forgit::checkout::tag forgit::cherry::pick forgit::cherry::pick::from::branch forgit::clean forgit::diff forgit::fixup forgit::log forgit::rebase forgit::reset::head forgit::revert::commit forgit::stash::show |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By listing all of these functions, this file will wake up and run on any of these commands, including aliases
compdef _git-staged forgit::reset::head | ||
compdef __git_recent_commits forgit::revert::commit | ||
compdef _git-stash-show forgit::stash::show | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sections was just grabbed from the deleted file in this PR.
I am pretty sure that this will fix #332 if we have it merged! It seems to be playing nice on my machine at least. There are two possible solutions:
The user installation process in this case would just be
and if they have the homebrew autocompletions set up it will "just work"
The brew formula would then install the completion script (completions/git-forgit.zsh) along side forgit.plugin.zsh, and _git-forgit in the completion area. We would instruct users in our README to source this file. If I've understood all of this correctly (big chance I have not) I much prefer option 1, as it's less hassle for the end user. This works on my machine at least, but definitely needs someone to test on a linux distro |
I am not an zsh expert as well, but from what I know there is a reason for having two files, and hence we shouldn't change it just to make the homebrew installation easier. |
The reason I did this way was to match the other completions I had in my brew completion folder, so there is good precedence for this type of pattern at least in brew formulas. What I mean by this is all other formulas only have a single completion file loaded dynamically, and not one that must be sourced. The other thing helping us here is that the completions are populated (and are slow, I agree) only the very first time user pushes tab with forgit. After that they seem to be cached in a file in the users $HOME directory, meaning that whenever they use it next, even after startup it is snappy. |
The reason the other formulas have only a single completion file is probably that they contain a single command only. In forgit we have the single command ( |
Sure, I agree. But isn't it better for our users to be able to complete all commands dynamically without any additional intervention like sourcing a file? If the completions are cached (which they are), this will "just work" for the end user, and they don't need to additionally add something more to their RC files to get the completions for I see that as (at least a minor) improvement. I don't see the downside. |
Of course that would be better. But the dynamic completion works for |
Ah! I think I see the miscommunication. This line at the top of the _git-forgit file registers the which commands it completes:
That means if any of these functions are called (including through an alias) the completion is registered and runs for that command. Note that it used to just intercept git-forgit, but I added all of our internal functions. This file is dynamically read if it is in the fpath, registering all functions that are in the #compdef declaration, then caching them. I have tested this on my machine and it seems to work for all instances of I'll say again that I have barely ever cracked open If you believe this doesn't work, I highly recommend testing it out to make sure I haven't done something stupid! (always a possibility). |
Ah, I see, thanks for the clarification. That is obviously a zsh feature superior to bash and I wasn't aware of that. If that indeed works that way, then I don't see a reason to have two completion files. |
Agreed!! Definitely the gatekeeper, I am just kind of blindly swinging around here. I'll wait for his thoughts and thumbs up or down. |
Unfortunately this does not seem to be working for me. When I place completions/_git-forgit from this PR in my $fpath and only source forgit.plugin.zsh, completions only work for forgit as a git subcommand. Functions and aliases to not get completions. Am I missing something here @cjappl? |
completions/_git-forgit
Outdated
# We're reusing existing completion functions, so load those first | ||
# if not already loaded and check if completion function exists afterwards. | ||
(( $+functions[_git-add] )) || _git | ||
(( $+functions[_git-add] )) || return 1 | ||
(( $+functions[_git-branches] )) || _git-forgit | ||
(( $+functions[_git-branches] )) || return 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're loading the completion functions of git here. This is necessary because we rely on gits completion functions being present. This might slow down shell startup. Previously this was optional, because the user had to explicitly source completions/git-forgit.zsh. I think we should move this part into _git-forgit()
. This way the first completion of forgit is slowed down, but not the shell startup, which is better in my opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the way this seems to work is that this entire file is called when the completion is triggered the very first time.
The way I tested this.
- put some jibberish in this file, that would fail if it gets hit.
i.e.:
# Check if forgit plugin is loaded
if (( $+functions[forgit::add] )); then
jalksdjflaksjfl;asdkjf;lakdsfj # should crash!!
# We're reusing existing completion functions, so load those first
# if not already loaded and check if completion function exists afterwards.
(( $+functions[_git-add] )) || _git
(( $+functions[_git-add] )) || return 1
I then restarted my shell, after clearing the cache.
rm ~/.zcomp*
zsh
There is no crash, indicating this file has not been sourced or read.
However, on first completion:
> forgit::add <TAB> _git-forgit:104: command not found: jalksdjflaksjfl
_git-forgit:104: command not found: asdkjf
_git-forgit:104: command not found: lakdsfj
Any additional call defers to the cache, and does not hit that line again:
... continuing session from above
Nothing to add.
> forgit::add <TAB>
README.md completions/
This leads me to believe that these lines existing here are OK. This file seems to be sourced once, and only once the first time any of the completion commands in the very top line are asked for.
I would propose keeping this here. What do you think?
completions/_git-forgit
Outdated
(( $+functions[_git-branches] )) || _git-forgit | ||
(( $+functions[_git-branches] )) || return 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two lines are not necessary anymore, when this file is being executed the forgit completion functions have obviously been loaded.
Let me see if I can repro when I get home! Would you try clearing your cache and restarting your shell?
I believe its something like ~/.zcompdump, you should just be able to delete it.
…On Tue, Jan 30, 2024 at 1:24 PM, sandr01d ***@***.***(mailto:On Tue, Jan 30, 2024 at 1:24 PM, sandr01d <<a href=)> wrote:
Unfortunately this does not seem to be working for me. When I place completions/_git-forgit from this PR in my $fpath and only source forgit.plugin.zsh, completions only work for forgit as a git subcommand. Functions and aliases to not get completions. Am I missing something here ***@***.***(https://github.com/cjappl)?
—
Reply to this email directly, [view it on GitHub](#340 (comment)), or [unsubscribe](https://github.com/notifications/unsubscribe-auth/ADMDXYYPFU64YKO2J3EGU6LYRFQJRAVCNFSM6AAAAABCQIJJPSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJXHEYTSMRUGU).
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Alright, the cache clearing thing may be it. This is my steps to reproing it working. > echo $fpath
/usr/local/share/zsh/site-functions /usr/share/zsh/site-functions /usr/share/zsh/5.9/functions /opt/homebrew/share/zsh/site-functions/
> ls /usr/local/share/zsh/site-functions
_git-forgit
> cat ~/.zshrc
# clear cache for testing purposes
rm -rf ~/.zcomp*
autoload -Uz compinit
compinit
source ~/code/forgit/forgit.plugin.zsh
> ga README.md ## this autocompletion works! Will you give that a shot and see if it makes any difference? it may just be that once zsh looks at the completion file once, it caches what is in there and refuses to crack it open again. |
You are right, with the instructions provided above things are working for me 🎉 Reverting back to my regular .zshrc does not break it again. |
Woo hoo! Thanks for the help on the other thread, good teamwork 🍻
I have updated this review's README to indicate appropriate instructions, please review.
Do we want to wait for your changes with deferred execution to do this? Seems like a pretty minor change to ship a release on, but I'll defer to you. Please merge at your discretion if you're happy with it. Eventually we will need to do a github release and update our hombrew formula. I will likely work on auto-updating homebrew as a github action as the next task in my queue. |
* Add new homebrew workflow * Review comments * Review comments
This reverts commit 091d2f7.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The info about the update should land in the final commit message, so that it appears in the release notes as well. Can you squash the commits before merging and write a proper message? Then I will review again. Otherwise all approved from my side. Good work! 👍
I will plan on "squash and merge" with a proper commit message when the time comes using the github UI!
Still waiting on final approval from sandroid, but count on the commit message being reasonable (and me not merging any interim commits, just the final squash and merge)
…On Fri, Feb 2, 2024 at 1:19 PM, Tim ***@***.***(mailto:On Fri, Feb 2, 2024 at 1:19 PM, Tim <<a href=)> wrote:
@carlfriedrich requested changes on this pull request.
The info about the update should land in the final commit message, so that it appears in the release notes as well. Can you squash the commits before merging and write a proper message? Then I will review again. Otherwise all approved from my side. Good work! 👍
—
Reply to this email directly, [view it on GitHub](#340 (review)), or [unsubscribe](https://github.com/notifications/unsubscribe-auth/ADMDXYYKKZMOZ2GYX2CFJXDYRVJ4VAVCNFSM6AAAAABCQIJJPSVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMYTQNRQGQYDMOBSGA).
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Thanks for the reviews and sanity checks on this and the others @carlfriedrich ! Cheers :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for requesting changes so late, I had overlooked this previously.
Great work, happy to see our setup simplified 👍
I've prepared the updates for the AUR package. I would prefer to be the one to merge this PR, so I can push the changes to the AUR immediately. I will make sure to squash and edit the commit message for the release notes as discussed previously. Let me know once this is ready to be merged.
completions/_git-forgit
Outdated
# Check if forgit plugin is loaded | ||
if (( $+functions[forgit::add] )); then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that we are no longer sourcing this, we no longer need to check if the plugin is loaded and can remove the if statement. Compsys will not execute this file in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in this most recent version! Check it out. This still works as intended on my machine.
Hey @cjappl, I found out that there is an easier way to define completions for all forgit functions in the first line. From the zsh manual:
I gave this a try, but unfortunately it messed with our logic for evaluating the command and I had to do a slight refactor. You can find it here: https://github.com/sandr01d/forgit/tree/zshcomp I though about adding it to this PR, but I think it might convolute it too much, so I might do a follow up. What do you think? |
I think this is great and overall a big improvement! I would vouch for doing it in a quick follow up review, as I agree that it may convolute. If you agree with me, this review is good to merge if you approve it. Please merge at will (and give it a good commit title/description using "Squash and merge"). |
Thanks for implementing the changes @cjappl! |
oof, yes confirmed. I have to press TAB twice in a row on the first instance of trying to complete after I start a
This seems reasonable, but do we have a thought as to how all the other commands work the very first time, and ours does not? I think that would be a crucial thing to get to (in this code review or a future one).
I would like to get to the bottom of this problem, so that hopefully the zsh completions "just work". Our options as I see it:
Any preference on one versus the other @sandr01d ? I can't make up my mind which is preferable. Do you have an intuition of if this is fixable for us? |
Wow, had some success here!! I was noticing that other completion files actually explicitly called a helper function at the end of the file, leading to an automatic completion on the first load. I tried to emulate that here: And it worked! @sandr01d can you try this out and see if it gets rid of the double tab?? It is a little clunky right now, but this is just a proof of concept to see if it's feasible. A couple examples of the same or similar pattern from a few packages I have installed:
|
Works on my side, nice find!
I think we should address this in this PR before we merge. If we require manual user intervention during an update, I prefer to do so only once and not twice. |
Alright, put in this review! Please confirm one last time that the file on this branch works for you @sandr01d. @carlfriedrich I cleared your approval so you could look at what we did with these final two commits. This is working on my machine and has my approval (if it has each of yours). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Functionality wise, everything works as before and at the first time 👍
I think we should combine most of the code from _forgit-dispatch
and _git-forgit
into one function. The case statement in _git-forgit
does essentially the same as the all the if-else
statements in _forgit-dispatch
. I think just having a separate function with the case statement that is called by _forgit-git
and _fogit-dispatch
would already simplify this a lot. My simplification in 398470c should make this easier, so maybe we should include it here after all.
I am getting a little concerned this review is getting a bit long in the tooth, and the more we tinker with it the more likely it will be that we break something.
How do you feel about submitting this, then doing a follow up where we clean it up stylistically?
I have to admit I'm at the very bleeding edge of my zsh knowledge and skill, so I worry about tipping the house of cards and leading to some breakage.
…On Mon, Feb 5, 2024 at 12:37 PM, sandr01d ***@***.***(mailto:On Mon, Feb 5, 2024 at 12:37 PM, sandr01d <<a href=)> wrote:
@sandr01d requested changes on this pull request.
Functionality wise, everything works as before and at the first time 👍
I think we should combine most of the code from _forgit-dispatch and _git-forgit into one function. The case statement in _git-forgit does essentially the same as the all the if-else statements in _forgit-dispatch. I think just having a separate function with the case statement that is called by _forgit-git and _fogit-dispatch would already simplify this a lot. My simplification in [398470c](398470c) should make this easier, so maybe we should include it here after all.
—
Reply to this email directly, [view it on GitHub](#340 (review)), or [unsubscribe](https://github.com/notifications/unsubscribe-auth/ADMDXY63Y3FISPWYR5CSSDDYSE7JRAVCNFSM6AAAAABCQIJJPSVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMYTQNRTG43TAOJVG4).
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
I don't think that is true. During the review we have found a solid way to test the completions, so that should make us confident about further changes.
This PR is called "Consolidate zsh completions in one file...", so if there's more to consolidate we should do it here before merging IMO. @sandr01d Can you take over? |
@sandr01d and @carlfriedrich just updated, cherry picking the commit from @sandr01d's branch and getting rid of the dispatch function. Please test on your machines, it works on mine but as stated before I'm worried about regressions :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works flawlessly for me 👍
I will use this as my daily driver until the end of the week just to be sure. Will merge it and update the AUR package together on the weekend.
Great! Thanks @sandr01d . This review is in your hands now, merge when appropriate. |
Check list
Description
In my hunting down of #332 , I realized we do things a bit differently than other
brew
formulas. We have two files, and require the user to put one in$fpath
and source another one. Most other commands just have a single file they put in$fpath
.NOTE: I am VERY far from a zsh expert, I completely defer to @sandr01d on what's right to do here.
In the homebrew formula, hopefully now we can just use it's facilities to install this completion file, and things "just work" right out of the box, no additional work necessary from the user (sourcing files, etc).
I would love if someone else tested this, to ensure I am not crazy and this still works!
Type of change
Test environment