Mis
Mis

Reputation: 33

How can I define bracket pair colorization for brackets inside comments for a VS Code language extension?

I'm making the TextMate grammar for a language to get syntax highlighting. This is the rule for @link tags in doc comments:

"scopeName": "source.amx.pawn",
  "patterns": [
    {
      "name": "comment.block.documentation.amx.pawn",
      "begin": "/\\*\\*(?!/)",
      "end": "\\*/",
      "patterns": [{ "include": "#docblock" }]
    },
    // more patterns...
  ],
  "repository": {
    "docblock": {
      "patterns": [
        "match": "({)(@)(link|linkcode|linkplain)\\s+(\\w+)\\s+(.*)(})",
        "captures": {
          "1": { "name": "punctuation.definition.bracket.amx.pawn" },
          "2": { "name": "punctuation.docblock.at.amx.pawn" },
          "3": { "name": "storage.type.class.docblock.tag.amx.pawn" },
          "4": { "name": "variable.other.docblock.link.amx.pawn" },
          "5": { "name": "entity.name.type.instance.docblock.amx.pawn" },
          "6": { "name": "punctuation.definition.bracket.amx.pawn" },
        }
      ]
    },
    // more stuff...
  }
}

Everything is colorized correctly, except for the curly braces. They have the punctuation color (which is what I specified in the rule).

my textmate rule

but I assumed vscode would apply the bracket pair colorizer on top of that anyway, like it does on typescript, for example.

typescript textmate rule

As you can see, in typescript it has the bracket pair colorization applied.

If I inspect the editor tokens and scopes, these are the textmate scopes for the curly braces with my grammar:

punctuation.definition.bracket.amx.pawn
comment.block.documentation.amx.pawn
source.amx.pawn

and these are the scopes of the curly braces in typescript:

punctuation.definition.bracket.curly.end.jsdoc
entity.name.type.instance.jsdoc
comment.block.documentation.ts
source.ts

The only difference is the entity.name.type.instance scope, but I think thats a trailing scope from the previous token, so I'm guessing the bracket pair colorizer doesn't care about those scopes.

How can I achieve the same result as with the typescript grammar?

Upvotes: 1

Views: 748

Answers (2)

RedCMD
RedCMD

Reputation: 382

By default the scope names comment, string and regex will change the standard token type from Other to Comment, String or RegEx; disabling bracket pair colorization.
You can use meta.embedded to revert the standard token type back to Other, re-enabling bracket pair colorization.

> editor.action.inspectTMScopes will show the standard token type.

I would recommend using something like "name": "meta.embedded.docblock.amx.pawn" in "docblock".

BTW that surrounding "patterns" in "docblock" is invalid.
and also the trailing comma ,.

enter image description here

Upvotes: 0

Arda G.
Arda G.

Reputation: 11

I was experiencing the same issue and solved it by defining a language configuration that included a definition for brackets.

// package.json
{
  "name": "langname",
  // other stuff ...
  "contributes": {
    "languages": [
      {
        "id": "langid",
        // other stuff...
        "configuration": "./language-configuration.json"
      }
    ],
    // other stuff ...
  }
}
// language-configuration.json
{
  "brackets": [
    ["{", "}"],
    ["[", "]"],
    ["(", ")"],
    // other tokens you want to colorize
  ],
  // other stuff...
}

Upvotes: 1

Related Questions