Repository navigation
fix: style the variable name in ${VAR} as an env var - #1030
Merged
HalFrgrd merged 1 commit intoOct 10, 2026
Merged
Conversation
The parser marked a word as an environment variable only when it followed
a bare $, so ${BASH} was not coloured like $BASH and got no tooltip.
Mark the name that directly follows ${ as well. The ${ and } tokens keep
their bracket colouring.
Fixes HalFrgrd#1029
Owner
|
Thanks! |
This works! I think it would be better to also color the |
Contributor
Author
|
Agreed, that reads better. I opened #1036 for it: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1029.
Problem
echo $BASHcolours the variable with theenv-varstyle, butecho ${BASH}does not, and the name gets no variable tooltip either.Cause
In
DParser, a word is markedis_env_varonly when the previous token isDollar. In${BASH}the previous token isParamExpansion(${), so the name was treated as ordinary text.Change
Mark the word that directly follows
${as an env var too, when that word is a plain variable name (letters, digits, underscores, not starting with a digit).The name check is there because of how these forms lex:
${${HOME}HOME"${USER}"USER${ARR[0]}ARR"${LOGNAME:-nobody}"LOGNAME:-nobody(one word inside double quotes)${#PATH}#'${PWD}'Without the check,
LOGNAME:-nobodyand#would be looked up as variable names and painted with the unrecognised-env-var colour, which is worse than today. Those three "no" rows are styled exactly as before this PR.The
${and}tokens are not marked. They carry abracket_depth, andtoken_to_stylegives rainbow bracket colouring priority over the env-var colour, so they stay bracket-coloured like every other delimiter. If you would rather have the whole${BASH}in the env-var colour, as the issue's wording suggests, that needs a change intoken_to_styleand I'm happy to do it.Tests
grammar::dparser::tests::test_param_expansion_name_is_env_varcovers the table above.app::formatted_buffer::tests::test_brace_delimited_env_var_stylingchecks that${HOME}getspalette.env_var()and${UNRECOGNISED_VAR_XYZ}getspalette.unrecognised_env_var(), mirroring the existing$HOMEtest.Both fail with the parser change disabled and pass with it.
Run on Ubuntu 24.04, rustc 1.99.0:
cargo test --lib: 876 passed, 0 failed, 1 ignored (874 onmain)cargo fmt --all -- --check: cleancargo clippy --workspace --all-features -- -D warnings: cleancargo doc --workspace --no-deps --document-private-items: builds, 43 warnings, the same count asmainNot done: I did not run the Docker integration tests, and I did not check the colours by eye in a terminal; the styling is covered by the unit test only.