简体   繁体   中英

Define a VSCode Scheme Syntax rule that "always wins"

I'd like to add some additional syntax highlighting to markdown.

I defined an injection grammar:

{
  "scopeName": "markdown.mytodos",
  "injectionSelector": "L:text.html.markdown",
    "patterns": [
        { "include": "#todo" }
  ],
    "repository": {
        "todo": {
        "match": "^[ \t]*o .*",
        "_comment": "Line start, tabs or spaces, then literal `o` and a space",
        "name": "entity.name.tag.css"
    }
  }
}

Result:

I expected to see my rule for both of these examples, but it only works for the first one:

foo
o bar <- correct scope

foo
    o bar <- wrong scope (meta.paragraph.markdown)

So it looks like my scope isn't taking, even though the regex matches (tested in isolation).

I looked it up, and Markdown defines meta.paragraph.markdown with this begin rule:

(^|\\G)[ ]{0,3}(?=\\S)

and this while rule:

(^|\\G)((?=\\s*[-=]{3,}\\s*$)|[ ]{4,}(?=\\S))

My current theory is that this unclosed while is blocking my rule.

Question:

  1. Does an open rule block any other matches from happening?
  2. How can I tell my vscode/textmate grammar that I want it to "win" all the time, even inside of a different scope?

I have tried:

  1. Using begin/end rules instead of match (no change)
  2. Defining the language as a subset of meta.paragraph.markdown instead of text.html.markdown : "injectionSelector": "L:meta.paragraph.markdown" (doesn't add nested scopes as expected)

I have found that changing my match pattern resolves the issue.

Did not work in the nested case:

^[ \t]*o .*

Works great for all cases I've tested:

(^|\G)[ \t]*o .*

Docs say:

\G asserts position at the end of the previous match or the start of the string for the first match

What does this mean in the context of multiple scope selectors trying to be matched by vscode/textmate? I'm not sure. Please chime in if you know!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM