【问题标题】:Sublime Text custom syntax highlightingSublime Text 自定义语法高亮
【发布时间】:2016-10-05 16:55:05
【问题描述】:

我正在处理自定义语法突出显示,并从 HTML 语法中复制了一些 sn-ps,因为这部分非常相似。我一直在试图弄清楚如何突出显示标签内的文本。这是我的定义:

- match: "(</?)([a-zA-Z0-9:]+)"
  captures:
    1: punctuation.definition.tag.begin.html
    2: entity.name.tag.other.html
  push:
    - meta_scope: meta.tag.other.html
    - match: '>'
      scope: punctuation.definition.tag.end.html
      pop: true
    - match: '.'
      scope: string.quoted.single.html

示例文本:

<file bash>
Some bash code block
Some bash code block
</file>

我的代码突出显示了括号 &lt;&gt;、file 和 bash 关键字,但我不知道如何为里面的块添加颜色。最终我想把它作为一个块评论或类似的东西,这样它就会脱颖而出。有什么建议吗?

我需要一个避免为没有结束标签的标签添加注释突出显示的解决方案。例如,我正在使用的标记中的某些标签不使用关闭,例如&lt;tag without close&gt; 没有&lt;/tag&gt;。有什么方法可以在正则表达式中添加排除项以仅在有打开和关闭标签时才有效,但在只有打开标签时无效?

<tag without close>
This should not be a comment.

<file bash>
This should be a comment.
</file>

This also should not be a comment.

只会使用一小部分标签,如上面的&lt;tag&gt;,主要用于元数据。

【问题讨论】:

    标签: sublimetext3 sublimetext sublime-text-plugin


    【解决方案1】:

    一种基于interiors of both &lt;style&gt; and &lt;script&gt; are managed 的方法是使用with_prototype,它具有pop。

    - match: '(?:^\s+)?(<)([a-zA-Z0-9:]+)\b(?![^>]*/>)'
      captures:
        1: punctuation.definition.tag.begin.html
        2: entity.name.tag.other.html
      push:
        - match: '(</)(\2)(>)'
          captures:
            1: punctuation.definition.tag.begin.html
            2: entity.name.tag.other.html
            3: punctuation.definition.tag.end.html
          pop: true
        - match: '>'
          scope: punctuation.definition.tag.end.html
          push:
            - match: '.'
              scope: comment.block.html
          with_prototype:
            - match: (?=</\2)
              pop: true
            - include: main
        - match: '.'
          scope: string.quoted.single.html
    

    请注意,这里的 ([a-zA-Z0-9:]+) 匹配任何有效的标签名称,就像您在问题中一样,\2 用于稍后匹配该组,无论是在立即解耦 match 条件中还是在 @987654331 @ 健康)状况。 with_protoype 定义了应用于当前上下文中所有内容的模式,所以在这里我们使用它来确保我们在到达&lt;/file&gt; 时将pop 注释式突出显示,而不是被视为评论的一部分.

    在with_prototype 中,- include: main 语句可确保您的评论类内容中的任何标签的行为都与外部&lt;file&gt; 标签一样。例如,下面的&lt;hello&gt; 的作用与&lt;file&gt; 相同。

    <file bash>
    Some bash code block
    <hello stuff>
    Some bash code block
    </hello>
    Some bash code block
    </file>
    

    如果您的标签没有匹配的结束标签,您可以通过为堆栈较高的标签定义特定行为来覆盖此行为,如下所示:

    - match: '(?:^\s+)?(<)(hello)\b(?![^>]*/>)'
      captures:
        1: punctuation.definition.tag.begin.html
        2: entity.name.tag.other.html
      push:
        - match: '>'
          scope: punctuation.definition.tag.end.html
          pop: true
        - match: '.'
          scope: string.quoted.single.html
    

    如果这早于match: '(?:^\s+)?(&lt;)([a-zA-Z0-9:]+)\b(?![^&gt;]*/&gt;)' 行,任何&lt;hello&gt; 标记都不会调用注释式突出显示。

    此文本不是comment.block.html。 一些 bash 代码块 一些 bash 代码块 一些 bash 代码块

    【讨论】:

    • 哇,这太棒了,而且比我想要的更先进!也感谢解释,它有助于学习。这段代码在 99% 的情况下都能正常工作,除非没有结束标签。
    • 在什么情况下您希望突出显示一个块,就好像它是一个没有结束标签的评论一样? @user1781482
    • 我想这个想法是将打开和关闭标签之间的任何东西作为一个块来威胁。如果只有打开标签,没有关闭标签,就不会有任何阻塞。
    • 也许这应该分成两个独立的部分以避免过度复杂化。因此,一种情况是突出显示&lt;some text in tags&gt;,另一种情况是&lt;tag open&gt;block quote text&lt;/tag close&gt;
    • @user1781482 我不确定在保持它对所有标签开放的同时这是否可行,但如果只有某些标签会有 cmets——比如file 和 rank——你可以始终使用(file|rank) 之类的东西代替第一个match 中的([a-zA-Z0-9:]+)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 2014-05-17
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多