我正在为 DSL 编写 vim 语法突出显示,其中函数遵循以下格式:
# A function with no arguments
<function>
# A function with arguments
<function(arg1, arg2)>
# A function with text
<function block of normal text>
# A function with args and text
<function(arg1,arg2) text>
# Line breaks are allowed pretty much everywhere.
<function(
arg1,
arg2)
a block of text>
# As is nesting
<function(<subfunc>) Some text with <another(subfunction) etc.>>
# Backslash escape
<function(single arg with a comma: "\,") contained bracket between quotes: "\>">
它是一种文本处理语言(想想类固醇的 Markdown ),因此文本 block 必须是非限制性的。
我在为此编写 vim 语法文件时遇到了很多麻烦。
我能做到
syn region myFunction start='<' end='>' skip='\\>'
syn region myArgs start='(' end=')' skip='\\)'
但是 myFunction
不能包含 myArgs
,因为这样括号会在以下示例中错误地突出显示:
<function(arg) some text (with parenthesis) that aren't arguments>
具体来说,我希望函数名称和参数列表仅在该区域的紧邻开头突出显示。但是,我做不到
syn region myFunction start='<(regex to match name and arg list)' ...
因为,即使匹配名称和参数列表的正则表达式
并不可怕,这也会破坏我语法突出显示嵌套函数的能力。
我想要的是类似 nextgroup
的语法区域
的开始
,但我找不到。
这可能吗?我如何在 vimscript 中执行此操作?
最佳答案
为与前导 <
重叠的函数名称定义包含语法匹配myFunction
的性格区域,然后使用 nextgroup
尝试与 myArgs
进行匹配只在它之后出现,而不是后续出现的情况。
:syn region myFunction start='<' end='>' contains=myFunctionName
:syn match myFunctionName '<\i\+' contained nextgroup=myArgs
:syn region myArgs start='(' end=')' contained
编辑:这是一个带有 matchgroup
的变体关于<...>
元素; matchgroup
不能用于起始元素,因为这会阻止 myFunctionName
的锚定:
:syn region myFunction start='<' matchgroup=myMarker end='>' contains=myFunctionName
:syn match myFunctionName '<\i\+' contained nextgroup=myArgs contains=myMarker
:syn match myMarker '<' contained
:syn region myArgs start='(' end=')' contained
关于Vim 语法 : Highlight a match only at the beginning of a region,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13611071/