覆盖语法定义中的范围?

emm*_*mma 4 sublimetext sublimetext3

我正在尝试为Sublime Text 3 编写胡子语法定义,但我遇到了HTML标记范围的问题.

任何胡子变量或部分在html标记之外都可以正常工作,但如果它们在里面,它们会根据标记的范围进行样式设置.

例如:

{{var}}
{{#block}}
    <div {{#enabled}}class="enabled"{{/enabled}} id="{{id}}"></div>
{{/block}}
Run Code Online (Sandbox Code Playgroud)

var并将block正确突出显示,但enabled将突出显示,就好像它是一个属性,并id作为一个字符串.

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

这是我的语法定义的YAML:

patterns:
- include: text.html.basic

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

- name: markup.mustache
  begin: '\{\{[&>#^] *(.*?) *\}\}'
  beginCaptures:
    '1': {name: entity.name.tag.mustache}
  end: '\{\{\/ *(\1) *\}\}'
  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}
Run Code Online (Sandbox Code Playgroud)

r-s*_*ein 5

我不知道如何使用旧的YAML语法定义执行此操作.但是,由于您使用ST3,您可以使用新.sublime-syntax文件(在此处说明).通过这些,您可以在"推送"其他语法定义时定义原型.在此定义中,您通过编写包含其他语法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
Run Code Online (Sandbox Code Playgroud)