1 year ago

#72000

test-img

Busayo Bamigbade

Is there a way to make an embedded scope not inherit the general syntax highlighting in textmate

I want to support embedding OCaml code inside HTML. The embedded OCaml starts with the tag <% and closes with %>. Inside these tags, we want everything to use the syntax of source.ocaml. To do this, we use the following rules:

  "patterns": [
    {
      "begin": "(^|\\G)\\s*(<!--)",
      "captures": {
        "1": {
          "name": "punctuation.definition.comment.html"
        },
        "2": {
          "name": "punctuation.definition.comment.html"
        }
      },
      "end": "(-->)",
      "name": "comment.block.html"
    },
    {
      "begin": "(?i)(^|\\G)\\s*(?=<(script|style|pre)(\\s|$|>)(?!.*?</(script|style|pre)>))",
      "end": "(?i)(.*)((</)(script|style|pre)(>))",
      "endCaptures": {
        "1": {
          "patterns": [
            {
              "include": "text.html.derivative"
            }
          ]
        },
        "2": {
          "name": "meta.tag.structure.$4.end.html"
        },
        "3": {
          "name": "punctuation.definition.tag.begin.html"
        },
        "4": {
          "name": "entity.name.tag.html"
        },
        "5": {
          "name": "punctuation.definition.tag.end.html"
        }
      },
      "patterns": [
        {
          "begin": "(\\s*|$)",
          "patterns": [
            {
              "include": "text.html.derivative"
            }
          ],
          "while": "(?i)^(?!.*</(script|style|pre)>)"
        }
      ]
    },
    {
      "begin": "(?i)(^|\\G)\\s*(?=</?[a-zA-Z]+[^\\s/&gt;]*(\\s|$|/?>))",
      "patterns": [
        {
          "include": "text.ocaml"
        }
        {
          "include": "text.html.derivative"
        }
      ],
      "while": "^(?!\\s*$)"
    },
    {
      "begin": "(^|\\G)\\s*(?=(<[a-zA-Z0-9\\-](/?>|\\s.*?>)|</[a-zA-Z0-9\\-]>)\\s*$)",
      "patterns": [
        {
          "include": "text.ocaml"
        },
        {
          "include": "text.html.derivative"
        }
      ],
      "while": "^(?!\\s*$)"
    }
  ]
}

Where text.ocaml looks likes this:

{
  "name": "Embedded Ocaml",
  "patterns": [
    {
      "name": "source.ocaml.embedded",
      "match": "(?<=<%)(.+)?(?=%>)",
      "captures": {
        "0": {
          "name": "source.ocaml",
          "patterns": [
            {
              "include": "source.ocaml"
            }
          ]
        }
      }
    }
  ],
  "scopeName": "text.ocaml"
}

This works fine if the tags are in between HTML tags, however, if the embedding tags are inside of an HTML string attribute, the embedded code inherits both the OCaml and HTML syntax highlighting. So tokens that have a rule in the OCaml syntax are highlighted properly, but all the others are highlighted as HTML strings. Is there a way to make the embedded scope NOT inherit the HTML syntax highlighting?Here's a screenshot of what it currently looks like

visual-studio-code

token

syntax-highlighting

textmate

0 Answers

Your Answer

Accepted video resources