0

我有一个包含以下组件的脚本:

  • 条款:/[a-z][a-z0-9]*(?:-[a-z0-9]+)*/
  • 字符串:<.+>
  • 数字:\d+

但是,字符串也可以是“模板字符串”,并且在内部具有嵌套的术语/字符串/数字。它们由{...}大括号分隔,如下所示:

term <string {term(another-term, 123)}>

如何让 VSCode 正确突出显示模板字符串内大括号内的内容?我用来生成一个项目,它给了我这个<mystring>.tmLanguage.json

{
  "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
  "name": "myscript",
  "patterns": [
    {
      "include": "#terms"
    },
    {
      "include": "#strings"
    },
    {
      "include": "#numbers"
    }
  ],
  "repository": {
    "terms": {
      "patterns": [
        {
          "name": "term.control.myscript",
          "match": "\\b([a-z][a-z0-9]*(?:-[a-z0-9]+)*)\\b"
        },
        {
          "name": "term.context.myscript",
          "match": "\\b(\\{[^\\}]+\\})\\b"
        }
      ]
    },
    "numbers": {
      "patterns": [
        {
          "name": "constant.numeric.integer.myscript",
          "match": "\\b(\\d+)\\b"
        },
        {
          "name": "constant.numeric.decimal.myscript",
          "match": "\\b(\\d+\\.\\d+)\\b"
        }
      ]
    },
    "strings": {
      "name": "string.quoted.double.myscript",
      "begin": "<",
      "end": ">",
      "patterns": [
        {
          "name": "constant.character.escape.myscript",
          "match": "\\\\."
        }
      ]
    }
  },
  "scopeName": "source.myscript"
}

但是,我得到了这个:

在此处输入图像描述

我以前从未为编辑器做过语法高亮,所以不确定在哪里真正调试。

4

1 回答 1

0

我通过使用 textmate 语言语法中的特殊范围来完成这项工作:

{
  "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
  "name": "mylang",
  "patterns": [
    {
      "include": "#terms"
    },
    {
      "include": "#punctuations"
    },
    {
      "include": "#strings"
    },
    {
      "include": "#numbers"
    }
  ],
  "repository": {
    "terms": {
      "patterns": [
        {
          "name": "entity.name.type.language.mylang",
          "match": "([a-z][a-z0-9]*(?:-[a-z0-9]+)*)"
        },
        {
          "name": "entity.name.type.language.parens.mylang",
          "begin": "([a-z][a-z0-9]*(?:-[a-z0-9]+)*)\\(",
          "end": "\\)",
          "patterns": [
            {
              "includes": "#terms"
            },
            {
              "includes": "#strings"
            },
            {
              "includes": "#numbers"
            }
          ]
        }
      ]
    },
    "numbers": {
      "patterns": [
        {
          "name": "constant.numeric.integer.mylang",
          "match": "\\b(\\d+)\\b"
        },
        {
          "name": "constant.numeric.decimal.mylang",
          "match": "\\b(\\d+\\.\\d+)\\b"
        }
      ]
    },
    "punctuations": {
      "patterns": [
        {
          "name": "punctuation.separator.parameter.mylang",
          "match": ","
        },
        {
          "name": "punctuation.curly.open.mylang",
          "match": "\\{"
        },
        {
          "name": "punctuation.curly.close.mylang",
          "match": "\\}"
        }
      ]
    },
    "strings": {
      "name": "string.mylang",
      "begin": "\\<",
      "end": "\\>",
      "patterns": [
        {
          "name": "constant.character.escape.mylang",
          "match": "\\\\."
        },
        {
          "name": "punctuation.term.mylang",
          "begin": "\\{",
          "beginCaptures": {
            "0": {
              "name": "meta.brace.curly.mylang"
            }
          },
          "end": "\\}",
          "endCaptures": {
            "0": {
              "name": "meta.brace.curly.mylang"
            }
          },
          "patterns": [
            {
              "include": "#terms"
            },
            {
              "include": "#numbers"
            },
            {
              "include": "#strings"
            }
          ]
        }
      ]
    }
  },
  "scopeName": "source.mylang"
}
于 2021-08-24T21:02:23.287 回答