开发者

How can I get context dependent syntax coloring for the same keyword in Vim?

I am creating a vim syntax file for a niche language that has no syntax file yet. The language has the following syntax when setting up a simple function.

foo(parameter)
    if parameter then
       print("yes")
    else
       print("no")
    end
 end

I want the end that is paired with the if to have the syntax coloring for conditionals and the "end" paired with the function declaration to have开发者_如何转开发 function syntax coloring. How do I achieve this? I currently have end set as a keyword. This ensures that it is colored, but it matches the color of the if only, never the function name.

Thanks for any help.


You'll need to define some syntax regions, starting with the if and ending with the end and another one starting with the function declaration and ending with the end. You can use the 'contains'/'containedin' options to allow nesting of regions etc. This is far from trivial, but there are lots of useful guides in :help syn-region among others. Read the whole help file thoroughly and experiment. To get you started, you could try something like this:

syn keyword MyLanguageElse else containedin=MyLanguageIfBlock

syn region MyLanguageIfBlock matchgroup=MyLanguageIf start="^\s*if\>" end="^\s*end\>" containedin=MyLanguageFuncBlock contains=MyLanguageIfBlock
syn region MyLanguageFuncBlock matchgroup=MyLanguageFunc start="^\s*\k*\ze(" end="^\s*end\>"

hi link MyLanguageIf Keyword
hi link MyLanguageElse Keyword
hi link MyLanguageFunc Comment

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜