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

🐛 Fix zsh autocompletion installation #237

Merged
merged 10 commits into from
Aug 17, 2024

Conversation

alexjurkiewicz
Copy link
Contributor

I ran into issues installing completiong from a typer-enhanced program. Here are two fixes:

  1. Don't set the completion style. This overrides user styling globally and is a purely personal, cosmetic preference.
  2. Adjust the order of lines inserted into .zshrc. compinit must be run after fpath is adjusted, for new completions to be loaded.

I tested this logic with my current zsh (using oh-my-zsh) and with a bare zshrc, and it works in both cases.

This is an opinionated change that can affect users existing completion configuration.
compinit must be run after fpath is updated, to load new completions.

Don't add extraneous newlines at the end of zshrc.
@codecov
Copy link

codecov bot commented Feb 22, 2021

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@RuiLoureiro
Copy link

I was having problems with autocompletion on zsh (oh-my-zsh), and these changes fixed it.

@svlandeg svlandeg added feature New feature, enhancement or request p3 bug Something isn't working p2 and removed feature New feature, enhancement or request p3 labels Mar 6, 2024
Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

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

I can confirm that the zsh completion wasn't working as expected. The second "compinit" line was never written to .zshrc due to the if line not in completion_init_lines check, as the string "compinit" was already part of the first line.

Additionally, as pointed out in the description of the PR and in previous discussions, the order of commands was wrong.

On master, my .zshrc would look like this:

autoload -Uz compinit
zstyle ':completion:*' menu select
fpath+=C:\Users\Sofie\.zfunc

and autocompletion wouldn't work.

With this PR, my .zshrc would look like this:

autoload -Uz compinit
fpath+=~/.zfunc
compinit

and autocompletion works 🎉

Thanks for this contribution @alexjurkiewicz and apologies for the long delay in reviewing this!

@svlandeg svlandeg changed the title zsh autocomplete improvements 🐛 Fix zsh autocompletion Jul 1, 2024
@svlandeg svlandeg linked an issue Jul 1, 2024 that may be closed by this pull request
4 tasks
@tiangolo tiangolo changed the title 🐛 Fix zsh autocompletion 🐛 Fix zsh autocompletion installation Aug 17, 2024
@tiangolo
Copy link
Member

Great, thank you @alexjurkiewicz! 🚀

And thanks @svlandeg for the review! 🙇


I've been studying today a bit about all this (I always have to re-study all the completion stuff when working on this 😅 ), these were good resources:

I realized that all the Zsh completion installation could be a single line, which makes it easier to test for in the if block.

About the styling, as up to now the completion has installed the style configs, I won't remove this, at least not in this PR that is fixing a bug, maybe that could be considered a breaking change as it's a change in behavior, not sure. 🤔

Still, I updated it to checking if there's a zstyle config in the ~/.zshrc and only set this default config if there's no other one.

My doubt about removing the zstyle config is that I don't expect many people to know how to tweak it, but they all would benefit from a default nice UX from the CLIs they install, that's the tradeoff I'm trying to balance. Maybe an option is to set the configs only for the current app, that would mean people would have the menu only for this program they are installing but not for the rest of their shell... still not sure. I added a working (commented) line there with that config so we can revisit it later.

For now, let's leave this PR at fixing completion installation for Zsh. 😎 🚀

Thank you for your contribution! 🍰

This will be available in Typer 0.12.4 released in the next hours. 🎉

@tiangolo tiangolo merged commit 640fb09 into fastapi:master Aug 17, 2024
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p2 shell / zsh
Projects
None yet
4 participants