zsh-users / zsh-syntax-highlighting

Fish shell like syntax highlighting for Zsh.
github.com/zsh-users/zsh-syntax-highlighting
BSD 3-Clause "New" or "Revised" License
19.55k stars 1.32k forks source link

Wrapped ZLE widgets are not dumped by compdump #851

Open ericbn opened 2 years ago

ericbn commented 2 years ago

Describe the bug The ZLE widgets dumped by compinit (before sourcing zsh-syntax-highlighting) are not dumped in a subsequent compdump (after sourcing zsh-syntax-highlighting) because they have been wrapped by zsh-syntax-highlighting.

How to reproduce

❯ docker run -it --rm zshusers/zsh:5.8
# apt update && apt install -y git
...
# git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.zsh/zsh-syntax-highlighting
...
# > ~/.zshrc <<\END
autoload -Uz compinit && compinit -C
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
END
# exec zsh
# cp ~/.zcompdump{,.old}
# compdump
# diff ~/.zcompdump{.old,}

Current output

1764,1793d1763
< zle -C _bash_complete-word .complete-word _bash_completions
< zle -C _bash_list-choices .list-choices _bash_completions
< zle -C _complete_debug .complete-word _complete_debug
< zle -C _complete_help .complete-word _complete_help
< zle -C _complete_tag .complete-word _complete_tag
< zle -C _correct_filename .complete-word _correct_filename
< zle -C _correct_word .complete-word _correct_word
< zle -C _expand_alias .complete-word _expand_alias
< zle -C _expand_word .complete-word _expand_word
< zle -C _history-complete-newer .complete-word _history_complete_word
< zle -C _history-complete-older .complete-word _history_complete_word
< zle -C _list_expansions .list-choices _expand_word
< zle -C _most_recent_file .complete-word _most_recent_file
< zle -C _next_tags .list-choices _next_tags
< zle -C _read_comp .complete-word _read_comp
< bindkey '^X^R' _read_comp
< bindkey '^X?' _complete_debug
< bindkey '^XC' _correct_filename
< bindkey '^Xa' _expand_alias
< bindkey '^Xc' _correct_word
< bindkey '^Xd' _list_expansions
< bindkey '^Xe' _expand_word
< bindkey '^Xh' _complete_help
< bindkey '^Xm' _most_recent_file
< bindkey '^Xn' _next_tags
< bindkey '^Xt' _complete_tag
< bindkey '^X~' _bash_list-choices
< bindkey '^[,' _history-complete-newer
< bindkey '^[/' _history-complete-older
< bindkey '^[~' _bash_complete-word

Expected output There should be no output from diff.

Additional context Without sourcing zsh-syntax-highlighting, I get:

# zle -lL | grep _bash_completions
zle -C _bash_complete-word .complete-word _bash_completions
zle -C _bash_list-choices .list-choices _bash_completions

After sourcing zsh-syntax-highlighting, I get:

# zle -lL | grep _bash_completions
zle -C orig-s0.0000110000-r12616-_bash_complete-word .complete-word _bash_completions
zle -C orig-s0.0000110000-r12616-_bash_list-choices .list-choices _bash_completions

And compdump will dump all widgets from zle -lL that march _* in the 3rd and 5th words in the list above.

Maybe a solution is to prepend a _ to the ZLE widget wrapper name when the original name also starts with _?

EDIT: Or letting compdump use the wrapper names will actually not work at all! In this case, the solution could be to not wrap widgets with names and respective function names both starting with _...

danielshahaf commented 2 years ago

The ZLE widgets dumped by compinit (before sourcing zsh-syntax-highlighting) are not dumped in a subsequent compdump (after sourcing zsh-syntax-highlighting) because they have been wrapped by zsh-syntax-highlighting.

First things first, could you please confirm that you have a valid use-case for running compdump? Asking because of this bit of zsh's documentation: https://github.com/zsh-users/zsh/blob/2876c25a28b8052d6683027998cc118fc9b50157/Doc/Zsh/compsys.yo#L176-L179

There's also https://github.com/zsh-users/zsh/blob/2876c25a28b8052d6683027998cc118fc9b50157/Doc/Zsh/contrib.yo#L252-L259.

Maybe a solution is to prepend a _ to the ZLE widget wrapper name when the original name also starts with _?

We're migrating away from wrapping widgets. Could you try the redrawhook codepath (by inserting a true before is-at-least 5.8.0.2 in zsh-syntax-highlighting.zsh)? It'll be the default under zsh 5.9 once that is released.

Thanks for the thorough investigation!

ericbn commented 2 years ago

Hi @danielshahaf. What I'm trying to do with compdump is dump the completion configuration only after I update the git repositories of the external plugins I use. I'm trying this instead of using compinit without -C during startup, since this has its limitations: compinit will only detect that the completion configuration changed if the number of functions changed or the Zsh version changed. Is there a safe way to do it, and faster than just recreating the dumpfile every time?

And yes, using the redrawhook codepath solves the issue and makes compdump output the exact same configuration as compinit -C did before zsh-syntax-highlighting was sourced. Cool!

danielshahaf commented 2 years ago

Is there a safe way to do it, and faster than just recreating the dumpfile every time?

I'm not sure. You might ask zsh-users@.

And yes, using the redrawhook codepath solves the issue and makes compdump output the exact same configuration as compinit -C did before zsh-syntax-highlighting was sourced. Cool!

Thanks for confirming. I guess we'll just close this issue when zsh 5.9 is released, unless there's a particular reason to fix it for older versions as well.