How to Match a Pattern within Another Match in Vim Syntax file -
i have custom config involves following syntax:
key=value$(var)represents variable
the $(var) part can appear in both key , value, i.e. message="hello $(firsname) $(lastname)". value part must surrounded double quote " if contains space characters.
i want match key, value , $(var) , highlight them separately in vim.
here in vim syntax file:
syn match configvalue "\(\s\+=\)\@<=\"[^\"]*\"\|\(\s\+=\)\@<=\s\+" syn match configkey "^\s*[a-za-z0-9_.]\+\(\s*=\)\@=" syn match configvar "\$(.*)" the code matches configvalue , configkey, not configvar if within key=value. ruled syntax match priority (h:syn-priority):
- when multiple match or region items start in same position, item defined last has priority.
- a keyword has priority on match , region items.
- an item starts in earlier position has priority on items start in later positions.
rule 3 gives other 2 matches higher priority of configvar.
my problem that, how match 3 patterns separately, configvar having highest priority?
to have configvar match inside configvalue, have contain it; done via contained (leave off if var can match anywhere, not inside key=value) , contains=... attributes:
syn match configvalue "\(\s\+=\)\@<=\"[^\"]*\"\|\(\s\+=\)\@<=\s\+" contains=configvar syn match configvar "\$([^)]*)" contained note i've changed pattern configvar avoid matching $(foo) , $(bar) one element.
you said configvar can appear in configkey, that, range of allowed characters needs include $(), too. then, containment works well:
syn match configkey "^\s*[a-za-z0-9_.$()]\+\(\s*=\)\@=" contains=configvar
Comments
Post a Comment