【问题标题】:How to embed a syntax object in another in TextMate language definitions, tmLanguage如何在 TextMate 语言定义中嵌入另一个语法对象,tmLanguage
【发布时间】:2019-01-08 11:26:35
【问题描述】:

我正在尝试在 VS Code 中支持 Clojure 的 忽略文本表单#_(一种注释),它使用 tmLanguage 进行语法定义。由于使用#_ 禁用代码块很常见,因此我希望禁用的代码块保留其语法突出显示并仅将其斜体显示其状态。

但我缺乏使用 tmLanguage 的技能似乎阻止了我。这是失败的尝试之一(cson 的 sn-p):

  'comment-constants':
    'begin': '#_\\s*(?=\'?#?[^\\s\',"\\(\\)\\[\\]\\{\\}]+)'
    'beginCaptures':
      '0':
        'name': 'punctuation.definition.comment.begin.clojure'
    'end': '(?=[\\s\',"\\(\\)\\[\\]\\{\\}])'
    'name': 'meta.comment-expression.clojure'
    'patterns':
      [
        {
          'include': '#constants'
        }
      ]

使用 constants 定义一些 Clojure 常量对象,例如 keyword

  'keyword':
    'match': '(?<=(\\s|\\(|\\[|\\{)):[\\w\\#\\.\\-\\_\\:\\+\\=\\>\\<\\/\\!\\?\\*]+(?=(\\s|\\)|\\]|\\}|\\,))'
    'name': 'constant.keyword.clojure'

我想要发生的是constants 定义将在评论“内部”使用。对于关键字,我有这个(失败的)规范:

  it "tokenizes keywords", ->
    tests =
      "meta.expression.clojure": ["(:foo)"]
      "meta.map.clojure": ["{:foo}"]
      "meta.vector.clojure": ["[:foo]"]
      "meta.quoted-expression.clojure": ["'(:foo)", "`(:foo)"]
      "meta.comment-expression.clojure": ["#_:foo"]

    for metaScope, lines of tests
      for line in lines
        {tokens} = grammar.tokenizeLine line
        expect(tokens[1]).toEqual value: ":foo", scopes: ["source.clojure", metaScope, "constant.keyword.clojure"]

(该列表中的最后一个测试)。它失败并显示此消息:

Expected
{  value : ':foo',
  scopes : [ 'source.clojure', 'meta.comment-expression.clojure' ] }
to equal
{  value : ':foo',
  scopes : [ 'source.clojure', 'meta.comment-expression.clojure', 'constant.keyword.clojure' ] }.

意思是我没有得到constant.keyword.clojure 范围,因此我没有关键字着色。 ????

有人知道怎么做吗?

【问题讨论】:

    标签: syntax visual-studio-code tokenize vscode-extensions tmlanguage


    【解决方案1】:

    您的keyword 正则表达式以lookbehind 开头,要求关键字前必须有一个空格、([{ 字符。 #_ 中的 _ 不符合该要求。

    (?<=(\\s|\\(|\\[|\\{))
    

    您可以简单地将_ 添加到允许的字符列表中:

    (?<=(\\s|\\(|\\[|\\{|_))
    

    请注意,这个still wouldn't work 原样用于您的"#_:foo" 测试用例,因为最后有类似的前瞻。您可以在那里允许$,使匹配成为可选,或更改测试用例。

    【讨论】:

    • 感谢该正则表达式站点的链接!有趣的是它确实有效。使用此语法的测试通过和语法突出显示也有效。现在,我需要调查为什么会这样。 =)
    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 2011-03-09
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多