覆盖语法定义中的范围?

Override scopes in syntax definitions?

我正在尝试为 Sublime Text 3 编写 mustache 语法定义,但我 运行 遇到了 HTML 标签范围的问题。

任何 mustache 变量或部分在 html 标签外都可以正常工作,但如果它们在标签内,它们的样式将根据标签的范围设置。

例如:

{{var}}
{{#block}}
    <div {{#enabled}}class="enabled"{{/enabled}} id="{{id}}"></div>
{{/block}}

varblock 将正确突出显示,但 enabled 将像属性一样突出显示,而 id 作为字符串突出显示。

有没有办法让 mustache 变量和部分优先于 HTML 标签?

这是我的语法定义的 YAML:

patterns:
- include: text.html.basic

- name: comment.block.mustache
  match: '\{\{!(.*?)\}\}'

- name: markup.mustache
  begin: '\{\{[&>#^] *(.*?) *\}\}'
  beginCaptures:
    '1': {name: entity.name.tag.mustache}
  end: '\{\{\/ *() *\}\}'
  endCaptures:
    '1': {name: entity.name.tag.mustache}
  patterns:
  - include: $self
  - include: text.html.basic
    match: '[\s\S]'

- name: variable.mustache
  begin: '\{\{\{?'
  end: '\}?\}\}'
  captures:
    '0': {name: entity.name.tag.mustache}

我不知道如何使用旧的 YAML 语法定义来执行此操作。然而,由于您使用 ST3,您可以使用新的 .sublime-syntax 文件(已解释 here)。有了这些你就可以在 "pushing" 其他语法定义时定义原型。 在此定义中,您通过编写 push "Packages/path/to/file.sublime-syntax" 来包含其他语法。之后你可以添加原型,它会在语法内部进行匹配。

我做了一个语法定义,它应该有你想要的行为:

%YAML 1.2
---
name: Mustache
file_extensions: ["mustache"]
scope: text.html.mustache

contexts:
  main:
    - match: ""
      push: "Packages/HTML/HTML.sublime-syntax"
      with_prototype:
        - include: unescape
        - include: comment
        - include: block

  unescape:
    - match: "{{{"
      push: "Packages/HTML/HTML.sublime-syntax"
      with_prototype:
        - match: "}}}"
          pop: true

  comment:
    - match: '{{!(.*?)}}'
      scope: comment.block.mustache

  block:
    - match: "{{"
      scope: meta.block.begin.mustache
      push:
      - match: "}}"
        pop: true
        scope: meta.block.end.mustache
      - include: sections
      - include: variable

  sections:
    - match: "(#|^)(\w+)\b"
      captures:
        2: entity.name.tag.mustache
      scope: meta.block.section.start.mustache
    - match: "(/)(\w+)\b"
      captures:
        2: entity.name.tag.mustache
      scope: meta.block.section.end.mustache

  variable:
    - match: "\b\w+\b"
      scope: entity.name.tag.mustache