-
Notifications
You must be signed in to change notification settings - Fork 1.3k
main: Allow for "]" in shell aliases #793
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
Conversation
I think Small nit but in commit messages we generally prepend the highlighter name or component and then a colon. Could you prepend Thanks for catching this and submitting a PR! |
Hi @phy1729, thanks for a quick response and suggestions. I updated the commit message, as well as PR title. I also changed to use
However, I don't understand why tests are failing on CI. |
In the commit pushed to github (be93c7d), |
Sorry, I don't follow? I reduced the changes to the minimum necessary. All the tests pass on my machines (macOS and Raspbian), yet they fail on the CI. I really don't understand where is the discrepancy. |
The branch passes locally here as well. I'll take a closer look at the CI system later today. |
The failure in CI is:
I can reproduce that locally with both |
I've tried running it locally (Darwin Mojave) on 4 different versions of zsh, and tests succeed every time:
However, when I run on my raspberry pi, I get:
I'll try to dig further. The notable difference is that I have |
Tests run under Note that Raspbian is a Debian derivative. |
Could the failure be caused by the RHS of the alias? |
Yes, it is caused by using Thanks for helping @danielshahaf ! |
I've just installed zsh 4.3.11 to test the failures on CI, and it seems this is causing problems: https://unix.stackexchange.com/questions/626393/in-zsh-how-do-i-unset-an-arbitrary-associative-array-element tl;dr in older zsh, it's not possible to unset map elements that contain If there is no other proposal, I'll suggest that we don't insert values directly, but map them. For example, we represent any string as its ascii/utf8 representation. That is, instead of storing |
The solution given by Stephane Chazelas in workers/43269 appears to work as long as the resulting array is non-empty. A bit of code refactoring can take care of that. Not sure if there's a significant speed impact though. diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh
index 4ea3f34..5ec6985 100644
--- a/highlighters/main/main-highlighter.zsh
+++ b/highlighters/main/main-highlighter.zsh
@@ -601,16 +601,17 @@ _zsh_highlight_main_highlighter_highlight_list()
(( in_alias[1]-- ))
# Remove leading 0 entries
in_alias=($in_alias[$in_alias[(i)<1->],-1])
- (){
- local alias_name
- for alias_name in ${(k)seen_alias[(R)<$#in_alias->]}; do
- unset "seen_alias[$alias_name]"
- done
- }
if (( $#in_alias == 0 )); then
seen_alias=()
# start_pos and end_pos are of the alias (previous $arg) here
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
+ else
+ (){
+ local alias_name
+ for alias_name in ${(k)seen_alias[(R)<$#in_alias->]}; do
+ seen_alias=("${(@kv)seen_alias[(I)^$arg]}")
+ done
+ }
fi
fi
if (( in_param )); then
@@ -890,7 +891,7 @@ _zsh_highlight_main_highlighter_highlight_list()
(){
local alias_name
for alias_name in ${(k)seen_alias[(R)<$#in_alias->]}; do
- unset "seen_alias[$alias_name]"
+ seen_alias=("${(@kv)seen_alias[(I)^$arg]}")
done
}
if [[ $arg != '|' && $arg != '|&' ]]; then |
@phy1729 The loop body doesn't use the loop variable. |
Thanks for catching that. I updated the second commit on this PR to use the loop variable, and all tests pass. |
The diff looks ok to me. Just should rebase it back down to one commit. Credit in the message should go to Stephane Chazelas as he found the workaround; all I did was the obvious application of it. |
PR #776 fixed an issue with complex aliases and expansion. However, this change also introduced a problem with aliases which contain `]` (for example, commonly seen on macOS: `alias ]=open`), due to using an associative array `seen_alias`, indexed by the alias name. Due to `"$seen_alias[$arg]"`, it would fail when `$arg` is expanded to anything containing `]`'. Thus, typing `] /` would result in: ``` > ] / (anon):unset:3: seen_alias[]]: invalid parameter name ``` This change fixes the issue by ensuring we properly access keys in the associative array `seen_alias`. Older versions of zsh have issues with map keys having special characters, especially lacking ways to remove such keys. The issue is described in detail in https://unix.stackexchange.com/questions/626393/in-zsh-how-do-i-unset-an-arbitrary-associative-array-element. This fix uses proposal from [zsh-workers/43269](https://www.zsh.org/mla/workers/2018/msg01073.html), discovered by Stephane Chazelas, that boils down to avoid removing keys from the map, and reconstruct the map anew with some keys omitted. Co-authored-by: @phy1729
@danielshahaf @phy1729 I've rebased into a single commit and hopefully gave correct credits. Please take another look and thanks for the help! |
@knl The rebase and log message look good. I was going to leave reviewing this to @phy1729, but since you asked: Is there a measurable performance impact (e.g., due to using the If there is, we could consider what to do about it: e.g., accept it, or use The last one seems like an independently good idea, actually. Upstream's code is here: https://github.com/zsh-users/zsh/blob/5ede2c55f144593c16498c3131a76e188114a9c6/Src/builtin.c#L3746-L3750 |
Merged. Thanks for the contributions and sticking with it! |
PR #776 fixed an issue with complex aliases and expansion. However, this change
also introduced a problem with aliases which contain
]
(for example, commonlyseen on macOS:
alias ]=open
), due to using an associative arrayseen_alias
,indexed by the alias name. Due to
"$seen_alias[$arg]"
, it would fail when$arg
is expanded to anything containing]
'. Thus, typing] /
would resultin:
This change fixes the issue by ensuring we properly access keys in the
associative array
seen_alias
.Older versions of zsh have issues with map keys having special
characters, especially lacking ways to remove such keys. The
issue is described in detail in
https://unix.stackexchange.com/questions/626393/in-zsh-how-do-i-unset-an-arbitrary-associative-array-element.
This fix uses proposal from
zsh-workers/43269,
discovered by Stephane Chazelas, that boils down to avoid removing keys
from the map, and reconstruct the map anew with some keys omitted.
Co-authored-by:
@phy1729