Lemme try to analyze the syntax a little bit...
- Everything inside
(
and )
are pVars
pKeyword
are a sequence of identifiers followed by =
but not ==
pParam
are identifiers that are not pKeyword
nor a function call
So... lemme get the pVars
: :syn match pVars /\v\(\zs.*\ze\)/ contains=pKeyword,pParam
I tested with :hi pVars ctermfg=1
so I can see if the syn ...
is correct. With :syn region
, the last Green
isn't highlighted. I guess that is not good -- so I excluded the brackets using \zs
and \ze
instead of using :syn region
(though we still got the inner brackets -- at least we saved the last Green).
By the way, as suggested by @Rastapopoulos using /\v\(\zs.*\ze\)/
directly we can see the highlighted region without bothering with :syn clear
. After testing the /regex
, we know the regex works and using :hi pVars ctermfg=1
helps us deal with contained
and contains
...
Next, the pKeyword
: :syn match pKeyword /\i*\ze\s*=[^=]/ contained
. Oh we see the keywords not highlighted anymore -- that means the regex is also working.
After that, the pParam
: Let's start with :hi pParam ctermfg=4
so we can see which are captured just by look at the blue chars. :syn match pParam /\i*\(\i*(\)\@!/ contained
We excluded functions calls by the logic of "a sequence of identifiers, which is not followed by a sequence of identifiers with an open bracket (
".
Wait, this is also having the issue of overwriting the highlighting for keywords! pParam
needs to also exclude pKeyword
!
In that case, we make use of priorities! :help syn-priority
Let's reopen our vim to avoid the colors that we used to debug.
Rearranging gives:
:syn match pVars /\v\(\zs.*\ze\)/ contains=pKeyword,pParam
:syn match pParam /\i*\(\i*(\)\@!/ contained
:syn match pKeyword /\i*\ze\s*=[^=]/ contained
Then apply the colors. See if it works?
TL;DR:
Sample Text:
abc = myFunc(Green, Orange=abcd(Green, orange=ab(abc(e),g)), Green, abcd(Orange=Green), Green)
(Optional) Start Vim with vim -Nu NONE
to avoid plugins conflicting just in case. Do :hi pVars ctermfg=7 | hi pParam ctermfg=4 | hi pKeyword ctermfg=1
to see highlighting in action.
Everything inside of () (excluding the outmost brackets) are pVars
that contains pKeyword
and pParam
:syn match pVars /\v\(\zs.*\ze\)/ contains=pKeyword,pParam
pParam
is a sequence of identifiers, contained in pVars
, not immediately followed by function names (Identifiers followed by an open bracket, i.e. \i*(
, note that =
is not identifier by default: :help /character-classes
:help isident
)
:syn match pParam /\i*\(\i*(\)\@!/ contained
pKeyword
-- identifiers followed by a =
but not ==
, overriding pParam
(as pParam
also catches keyword -- we can negative lookahead if we want though)
:syn match pKeyword /\i*\ze\s*=[^=]/ contained
(Extra) If we want to highlight function names inside pVars
, recall that in (2.) function names are identifiers followed by an open bracket:
:syn match pFunc /\i*\ze(/ containedin=pVars contained
and use :hi pFunc ctermfg=6
to check. However I haven't found a way to also highlight the outermost function call...
