Sublime Text tmLanguage 文件中的正则表达式不使用多行

Regex in Sublime Text tmLanguage file doesn't use multiline

我正在尝试创建一个自定义语法语言文件来突出显示并帮助在 Sublime Text 2 中创建新文档。我已经走了很远,但是我遇到了一个关于 tmLanguage 中的 Regex 搜索的特定问题文件。我只是希望能够匹配 YAML 文档中多行的正则表达式,然后将其转换为 PList 以在 Sublime Text 中作为一个包使用。不行。

This 是我的正则表达式:

/(foo[^.#]*bar)/

这就是它在 tmLanguage YAML 文档中的样子:

patterns:
- include: '#test'

repository:
  test:
    comment: Tester pattern
    name: constant.numeric.xdoc
    match: (foo[^.#]*bar)

如果我将此 YAML 构建为一个 tmLanguage 文件并将其用作 Sublime Text 中的一个包,我会创建一个使用此自定义语法的文档,尝试一下并发生以下情况:

这将匹配:

foo 12345 bar

这将不匹配:

foo
12345
bar

a Regex tester 中,它们应该并且将会匹配,但在我的 tmLanguage 文件中它不起作用。

我也已经尝试在 tmLanguage 文件中向我的正则表达式添加修饰符,但以下要么不起作用,要么完全破坏文档:

match: (/foo[^.#]*bar/gm)
match: /(/foo[^.#]*bar/)/gm
match: /foo[^.#]*bar/gm
match: foo[^.#]*bar

Note: My Regex rule works in the tester, this problem occurs in the tmLanguage file in Sublime Text 2 only.

非常感谢任何帮助。

编辑:我使用匹配而不是 begin/end 子句的原因是因为我想使用捕获组给它们不同的名称。如果有人有一个包含开始和结束子句的解决方案,您仍然可以以不同的方式命名 'foo'、'12345' 和 'bar',那对我来说也很好。

我发现这是不可能的。这直接来自 TextMate Manual,它是 Sublime Text 所基于的文本编辑器。

12.2 Language Rules

<...>

Note that the regular expressions are matched against only a single line of the document at a time. That means it is not possible to use a pattern that matches multiple lines. The reason for this is technical: being able to restart the parser at an arbitrary line and having to re-parse only the minimal number of lines affected by an edit. In most situations it is possible to use the begin/end model to overcome this limitation.

我的情况是 begin/end 模型无法克服限制的少数情况之一。不幸的是。

好久没问了,你确定不能用begin/end吗?在我更好地掌握 syntax/logic 之前,我对 begin/end 也有类似的问题。这是我正在做的 json tmLanguage 文件的粗略示例(不知道正确的 YAML 语法)。

"repository": {

"foobar": {
    "begin": "foo(?=[^.#]*)", // not sure about what's needed for your circumstance. the lookahead probably only covers the foo line
    "end": "bar",
    "beginCaptures": {
        "0": {
            "name": "foo"
        }
    },
    "endCaptures": {
        "0": {
            "name": "bar"
        }
    },
    "patterns": [
        {"include": "#test-after-foobarmet"}
    ]
},
"test-after-foobarmet": {
    "comment": "this can apply to many lines before next bar so you may need more testing",
    "comment2": "you could continue to have captures here that go to another deeper level...",
    "name": "constant.numeric.xdoc",
    "match": "anyOtherRegexNeeded?"
}

}

我没有关注你的

"i need to number the different sections between the '#' and '.' characters."

,但如果需要在 foo bar 之间命名不同的组,您应该能够在 test-after-foobarmet 中进行测试并进行更多捕获。

有好的explanation of TextMate Grammar here。可能仍然会遇到一些错误,但在我对该主题一无所知时以对我有帮助的方式进行解释。