2

I want to write my own java syntax file for more customization, But can't find the definition of javaParen, it certainly isn't defined in default java.vim file. I actually want to highlight all the braces, but when there is a square bracket inside normal () pair, it won't be highlighted, so I wanted to redefine javaParen to include the [] pair, so I want to find the definition of javaParen, and include [] pair in it.

3

You can search the default syntax with:

:vimgrep /javaParen/ $VIMRUNTIME/syntax/*.vim

You will see that it is in $VIMRUNTIME/syntax/java.vim, but it's a syntax region matchgroup. A matchgroup allows you to use highlight to only affect the matches for start and end on syntax regions, but not the text between them.

It looks like there are actually three syntax regions for () and []:

" catch errors caused by wrong parenthesis
syn region  javaParenT  transparent matchgroup=javaParen  start="(" end=")" contains=@javaTop,javaParenT1
syn region  javaParenT1 transparent matchgroup=javaParen1 start="(" end=")" contains=@javaTop,javaParenT2 contained
syn region  javaParenT2 transparent matchgroup=javaParen2 start="(" end=")" contains=@javaTop,javaParenT  contained
syn match   javaParenError   ")"
" catch errors caused by wrong square parenthesis
syn region  javaParenT  transparent matchgroup=javaParen  start="\[" end="\]" contains=@javaTop,javaParenT1
syn region  javaParenT1 transparent matchgroup=javaParen1 start="\[" end="\]" contains=@javaTop,javaParenT2 contained
syn region  javaParenT2 transparent matchgroup=javaParen2 start="\[" end="\]" contains=@javaTop,javaParenT  contained
syn match   javaParenError   "\]"

javaParenT, javaParenT1, and javaParenT2 are the syntax group names. javaParen, javaParen1, and javaParen2 are the highlight matchgroups.

Each one contains the next, allowing you to highlight nested braces. So, what you want is already covered. You just need to add highlights for them:

highlight link javaParen Special
highlight link javaParen1 Keyword
highlight link javaParen2 Type

Here's a screenshot with the highlights above:

enter image description here

  • So, when I do matchgroup=somthing , I'm actually defining a matchgroup ? I used to think that matchgroup is something that is used to tell vim that this syntax group's highlighting style matches the other already defined syntax group. XD – Harshiv Apr 3 '17 at 4:19
  • 1
    @Harshiv It's defining a highlight group, actually. It's pretty confusing since the vocabulary in the docs have match and group meaning syntax groups. What helps me remember is reading it as: Match's (highlight) Group. – Tommy A Apr 3 '17 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.