Starting a syn region with a keyword

梦想与她 提交于 2019-12-24 03:16:36

问题


I would like to start a region with "virtual" to highlight all virtual methods with a different color, but being virtual already defined as keyword in the cpp.vim file, the region matching is never performed. I tried the following

syn region   cVirtualMethod   start="virtual" end=";" contains=cppType
hi cVirtualMethod ctermfg=red

but it does not work. It does however, if I remove virtual from cppType, but it highlights the whole line (fair enough, I'd prefer just the method name, but it's good as well).

Is there a way of leaving the cpp.vim virtual highlight (so virtual will end up green) and highlight just the method name ?


回答1:


Syntax keywords always take precedence over regions, so there's no way around removing virtual as a syntax keyword.

You can maintain the highlighting by defining a highlight group for the region start (and not the end; that's the reason for the reverse argument order):

:syn region   cVirtualMethod end=";" matchgroup=cppType start="\<virtual\>"

Still, the entire inner region will be highlighted. To avoid that, you can add a contained :syn-match that matches only the method name, contain it in the above region, and put the :highlight on that instead of the region.




回答2:


can you test if this works for you?

hi! virtualMethod ctermfg=red
call matchadd("virtualMethod", "virtual.*;")

if it does, you can add autocmd for that matchadd call



来源:https://stackoverflow.com/questions/15523282/starting-a-syn-region-with-a-keyword

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!