如何在 Vim 中获得相同关键字的上下文相关语法着色? [英] How can I get context dependent syntax coloring for the same keyword in Vim?

查看:24
本文介绍了如何在 Vim 中获得相同关键字的上下文相关语法着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一种还没有语法文件的小众语言创建一个 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

我希望与 if 配对的结尾具有条件语句的语法着色,而end"与函数声明配对具有函数语法着色.我如何实现这一目标?我目前将结束设置为关键字.这确保它是彩色的,但它匹配 if only 的颜色,而不是函数名称.

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.

感谢您的帮助.

推荐答案

您需要定义一些语法区域,从 if 开始并以 end 结束,另一个以函数声明开始并以 end 结束.您可以使用 'contains'/'containedin' 选项来允许区域嵌套等.这远非微不足道,但在 :help syn-region 等中有很多有用的指南.通读整个帮助文件并进行实验.为了让您开始,您可以尝试以下操作:

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

祝你好运!

这篇关于如何在 Vim 中获得相同关键字的上下文相关语法着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆