Skip to content

Commit

Permalink
[ShellScript] Fix variable length expansions
Browse files Browse the repository at this point in the history
This commit fixes an syntax highlighting issue with $#var in ZSH.

In Bash ${#var} is required to return length os a variable.
In Zsh however $#var overrides special $# expansion.
  • Loading branch information
deathaxe committed Nov 9, 2024
1 parent 36ca6c6 commit 3bf12c1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
12 changes: 7 additions & 5 deletions ShellScript/Zsh.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,16 @@ contexts:
captures:
1: punctuation.definition.variable.shell
push: zsh-parameter-subscription
# https://www.gnu.org/software/bash/manual/bash.html#Special-Parameters
- match: (\$){{special_variables}}
scope: variable.language.special.shell
# length operator has precedence over special parameters
- match: (\$)(\#?){{identifier}}
scope: meta.interpolation.parameter.shell variable.other.readwrite.shell
captures:
1: punctuation.definition.variable.shell
2: keyword.operator.expansion.length.shell
push: zsh-parameter-subscription
- match: (\$){{identifier}}
scope: variable.other.readwrite.shell
# https://www.gnu.org/software/bash/manual/bash.html#Special-Parameters
- match: (\$){{special_variables}}
scope: variable.language.special.shell
captures:
1: punctuation.definition.variable.shell
push: zsh-parameter-subscription
Expand Down
40 changes: 40 additions & 0 deletions ShellScript/Zsh/tests/syntax_test_scope.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,46 @@ function {
ip=10.10.20.14
# ^^^^^^^^^^^ meta.string.glob.shell string.unquoted.shell
###############################################################################
# Special Parameters #
###############################################################################
# Expands to the number of positional parameters in decimal.
: $# ${#} ${!#} ${##} _$#_
# ^^ meta.interpolation.parameter.shell variable.language.special.shell
# ^ punctuation.definition.variable.shell
# ^^^^ meta.interpolation.parameter.shell
# ^ punctuation.definition.variable.shell
# ^ punctuation.section.interpolation.begin.shell
# ^ variable.language.special.shell
# ^ punctuation.section.interpolation.end.shell
# ^^^^^ meta.interpolation.parameter.shell
# ^ punctuation.definition.variable.shell
# ^ punctuation.section.interpolation.begin.shell
# ^ keyword.operator.expansion.indirection.shell
# ^ variable.language.special.shell
# ^ punctuation.section.interpolation.end.shell
# ^^^^^ meta.interpolation.parameter.shell
# ^ punctuation.definition.variable.shell
# ^ punctuation.section.interpolation.begin.shell
# ^ keyword.operator.expansion.length.shell
# ^ variable.language.special.shell
# ^ punctuation.section.interpolation.end.shell
# ^ meta.string.glob.shell string.unquoted.shell
# ^^^ meta.string.glob.shell meta.interpolation.parameter.shell variable.other.readwrite.shell
: $#__ints {1..$#__hits}
# ^^^^^^^^ meta.interpolation.parameter.shell meta.interpolation.parameter.shell variable.other.readwrite.shell
# ^ punctuation.definition.variable.shell
# ^ keyword.operator.expansion.length.shell
# ^^^^^^^^^^^^^ meta.interpolation.brace.shell
# ^ punctuation.section.interpolation.begin.shell
# ^ meta.number.integer.decimal.shell constant.numeric.value.shell
# ^^ keyword.operator.range.shell
# ^^^^^^^^ meta.interpolation.parameter.shell meta.interpolation.parameter.shell variable.other.readwrite.shell
# ^ punctuation.definition.variable.shell
# ^ keyword.operator.expansion.length.shell
# ^ punctuation.section.interpolation.end.shell
###############################################################################
# 14.3 Parameter Expansion #
Expand Down

0 comments on commit 3bf12c1

Please sign in to comment.