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

Support Zsh completion autoload #31

Merged
merged 1 commit into from
Jun 24, 2024
Merged

Conversation

segevfiner
Copy link

@segevfiner segevfiner commented Jun 24, 2024

With this, you can just dump the script in your Zsh fpath and it will be loaded as needed by the completion system

Related pnpm/pnpm#8232

cc @KSXGitHub

With this, you can just dump the script in your Zsh fpath and it will be loaded as needed by the completion system

Fixes pnpm/pnpm#8232
@KSXGitHub KSXGitHub requested a review from zkochan June 24, 2024 17:39
@KSXGitHub
Copy link

@segevfiner I replaced "Fixes" in your comment with "Related" because merging this PR alone is not sufficient. pnpm must be update with the latest version of tabtab in a separate commit.

Comment on lines +1 to 2
#compdef {pkgname}
###-begin-{pkgname}-completion-###
Copy link

@KSXGitHub KSXGitHub Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ###-begin-{pkgname}-completion-### and ###-end-{pkgname}-completion-### are there to determine the range of generate code should it be inserted to .zshrc. I have some questions:

  1. Must compdef strictly be at the top?
  2. Would it affect the pnpm completion zsh >> ~/.zshrc use-case?
  3. Would it affect the pnpm completion zsh > pnpm-completion.zsh + source pnpm-completion.zsh use-case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. compdef must be the first line in the file, for it to be recognized by the completion autoload system, see https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Autoloaded-files for reference.
  2. Depends on what the condition on zsh_eval_context evaluates to when zsh sources .zshrc, if it doesn't work, we can try a different condition, such as [[ $funcstack[1] = _{pkgname}_completion ]] that some other such completion files use.
  3. That should still work.

@@ -13,6 +14,14 @@ if type compdef &>/dev/null; then
_describe 'values' reply
fi
}
compdef _{pkgname}_completion {pkgname}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I did a bit of research and found that #compdef {pkgname} replaces this. The use-cases I listed above would be affected.

Although, it seems that #compdef {pkgname} is strictly necessary for lazily loading completion file.

Therefore, I request that:

  1. If lazy loading is strictly incompatible with the use-cases I listed above, please create new entry for zsh-lazy with new template file. The relevant files are constants.js and installer.js.
  2. If there is a way to be compatible with all use-cases in a single file, please make the necessary modifications for them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what the if is for, for supporting both in the same file. When the file is sourceed it will call compdef (With the #compdef line being just a comment), while when called by the completion system it will run the completion function.

I haven't tested appending to .zshrc though, which might require adjusting the condition.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested appending to .zshrc though, which might require adjusting the condition.

Please do. And tell me the result.

Copy link
Author

@segevfiner segevfiner Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to work fine in .zshrc as well. In .zshrc $zsh_eval_context is file, which will make the snippet call compdef as needed.

@zkochan zkochan merged commit 4709682 into pnpm:main Jun 24, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants