定义取决于缩进级别的语法区域 [英] Define a syntax region which depends on the indentation level

查看:33
本文介绍了定义取决于缩进级别的语法区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Vim 中为 reStructuredText 构建一个更轻量级的语法文件.首先,文字块在行尾遇到::"时开始:

I'm trying to built a lighter syntax file for reStructuredText in Vim. In rst, literal blocks start when "::" is encountered at the end of a line:

I'll show you some code::

    if foo = bar then
        do_something()
    end

Literal blocks end when indentation level is lowered.

但是,文字块可以在其他缩进但不是文字的结构中:

But, literal blocks can be inside other structures that are indented but not literal:

.. important::


    Some code for you inside this ".. important" directive::

        Code comes here

    Back to normal text, but it is indented with respect to ".. important".

那么,问题来了:如何制作检测缩进的区域?我使用以下规则做到了这一点:

So, the problem is: how to make a region that detects the indentation? I did that with the following rule:

syn region rstLiteralBlock  start=/^\%(\.\.\)\@!\z(\s*\).*::$/ms=e-1 skip=/^$/ end=/^\z1\S/me=e-1

它工作得很好,但有一个问题:任何应该由开始"匹配的行中出现的匹配项或区域接管语法规则.示例:

It works pretty fine but has a problem: any match or region that appear in line that should be matched by "start" takes over the syntax rules. Example:

Foo `this is a link and should be colored`_.  Code comes here::

它不会使我的规则起作用,因为有一个链接"规则接管了这种情况.这是因为 msme 匹配参数但我不能去掉它们,因为它只会给整行着色.

It will not make my rule work because there is a "link" rule that takes over the situation. This is because the ms and me matching parameters but I cannot take them off, because it would just color the whole line.

有什么帮助吗?

谢谢!

推荐答案

通过匹配 :: 之前的文本作为区域的开始,你确实阻止了其他语法规则在那里应用.我会通过正面回顾来解决这个问题;即仅在 :: 之前断言文本的规则,而不将其包含在匹配中.有了这个,您甚至不需要 ms=e-1,因为与区域开始匹配的唯一内容是 :: 本身:

By matching the text before the :: as the region's start, you're indeed preventing other syntax rules from applying there. I would solve this by positive lookbehind; i.e. only assert the rules for the text before the ::, without including it in the match. With this, you even don't need the ms=e-1, as the only thing that gets matched for the region start is the :: itself:

syn region rstLiteralBlock  start=/\%(^\%(\.\.\)\@!\z(\s*\).*\)\@<=::$/ skip=/^$/ end=/^\z1\S/me=e-1

缩进仍会被 \z(...\) 捕获.

这篇关于定义取决于缩进级别的语法区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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