How to get context-sensitive syntax coloring for the same keyword in Vim?

I am creating a vim syntax file for a niche language that does not yet have a syntax file. When setting up a simple function, the language has the following syntax.

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

I want the end to be conjugated with if in order to have syntax coloring for conditional expressions, and the “end” paired with the function declaration has a functional syntax coloring. How do I achieve this? I am currently completing a keyword. This ensures that it is colored, but it matches the color only if, but not the name of the function.

Thanks for any help.

+1
source share
1 answer

, if , - . 'contains'/'containsin', .. , :help syn-region . . , - :

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

!

+1

All Articles