1

I'm writing a vim syntax highlighting script to determine if a global param is in uppercase only. If its not - I want to highlight it. The problem is that the global params is in a specific part in the page.

The code looks something like this:

***VARS***
${VAR1}
${var2}

***OTHERS***
${var3}

So I want that all the variables under VARS which contains lowercase to be highlighted - in my example, only ${var2} should be highlighted.

I tried to do this:

syn match global_var_match "\${.*[a-z][^}]+}" contained
syn region global_variables start="\(\*\*\*VARS\*\*\*\)\@<=" end="\(\*\*\* OTHERS\*\*\*\)\@=" contains=global_var_match 
hi link global_variables ErrorMsg

But then also ${VAR1} and ${var2} is highlighted.

1
  • Note that \${.*[a-z][^}]+} should probably be \${[^}]*[a-z][^}]*}. The .* will match the closing }; and what if the lower-case character is just before the closing }? Commented Jul 15, 2019 at 21:50

1 Answer 1

1

One problem is the simple typo in:

hi link global_variables ErrorMsg

This should be

hi link global_var_match ErrorMsg

I.e. the bad match, not the containing region. However, with this change, variables containing lower case are still matched under ***OTHERS***.

That issue is caused by a spurious space you have in your match for ***OTHERS***.

I also chhanged your global_var_match regex. I have it as:

syn match global_var_match "\${[^}]*[a-z][^}]*}" contained

This behaves very well for me in test cases like

${VAR} blah ${VAr} ${vAR}

and others: only the vars containing lower case are flagged. Still investigating why the region is incorrect.

Here is what I have:

syn match global_var_match "\${[^}]*[a-z][^}]*}" contained
syn region global_variables start="\(\*\*\*VARS\*\*\*\)\@<=" end="\(\*\*\*OTHERS
\*\*\*\)\@=" contains=global_var_match
hi link global_var_match ErrorMsg
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.