Search code examples
syntax-highlightingvscode-extensions

VS Code Color Highlighting Tokenization for certain chars not working


I'm trying to create a syntax highlighting file for VS Code.
While doing this, I wanted to colorize the + sign, however, this won't work somehow.
I tried matching the standard operators like this:
"match": "\b(\+|-|\*|\/)\b"
But Addition and Multiplication don't work. When I remove the \ in front of them, it also doesn't work. The same problem occurs with ||.
Does someone know, how you can match these for the syntax highlighting?

Also, when inspecting the tokens,

"operators": {
            "name": "keyword.operator.fwcsv",
            "match": "\\b(-|\/)\\b"
        },

doesn't result in a - being shown as a keyword.operator.


Solution

  • The json format used by your tmLanguage grammar file escapes the character after a \. Therefore, \+ is a special character like \n, which (I think) does not exist. You have to double the \ in order to do what you want, so your regex would look like this:

    "match": "\\b(\\+|-|\\*|\\/)\\b"

    Once the grammar has been built, this is equivalent to:

    \b(\+|-|\*|\/)\b