Search code examples
monaco-editor

How to create rule for "todo" in a comment


I tried these rules for a custom language, in which comments are C# like.

I want to match word "todo" when it is in a comment (especially single line comment). My attempt here fails.

Can anyone guide me?

const options = {
    defaultToken: "",
    tokenPostfix: ".dbl",

    brackets: [
        { open: "{", close: "}", token: "delimiter.curly" },
        { open: "[", close: "]", token: "delimiter.square" },
        { open: "(", close: ")", token: "delimiter.parenthesis" },
        { open: "<", close: ">", token: "delimiter.angle" },
    ],

    keywords: [
        "Indexes",
        // ...
    ],

    symbols: /[=><!~?:&|+\-*\/\^%]+/,

    // escape sequences
    escapes: /\\(?:[\\"'`])/,

    // The main tokenizer for our languages
    tokenizer: {
        root: [
            { include: '@comments' },
            { include: '@whitespace' },
            { include: '@strings_single' },
            { include: '@strings_double' },

            // identifiers and keywords
            [
                /[a-zA-Z_]\w*/i,
                {
                    cases: {
                        "@keywords": { token: "keyword.$0", next: "@qualified" },
                        "@default": { token: "identifier.$0", next: "@qualified" },
                    },
                },
            ],

            // delimiters and operators
            [
                /}/,
                {
                    cases: {
                        "@default": "@brackets",
                    },
                },
            ],

            [/[{}()\[\]]/, "@brackets"],

            [
                /@symbols/,
                {
                    cases: {
                        "@operators": "delimiter",
                        "@default": "",
                    },
                },
            ],

            // numbers
            [/[0-9_]*\.[0-9_]+([eE][\-+]?\d+)?[fFdD]?/, "number.float"],
            [/0[xX][0-9a-fA-F_]+/, "number.hex"],
            [/0[bB][01_]+/, "number.hex"], // binary: use same theme style as hex
            [/[0-9_]+/, "number"],

            // delimiter: after number because of .\d floats
            [/[;,.]/, "delimiter"],
        ],

        whitespace: [
            [/\s+/, "white-spaces"],
        ],

        qualified: [
            [
                /[a-zA-Z_][\w]*/,
                {
                    cases: {
                        "@default": { token: "identifier.$0", next: "@pop" },
                    },
                },
            ],
            [/\./, "delimiter"],
            ["", "", "@pop"],
        ],

        comments: [
            [/\/\//, "comment", "@comment_single"],
            [/\/\*/, 'comment.quote', '@comment_quote']
        ],

        comment_single: [
            [/.*/, 'comment', '@pop'],
            [/todo/i, 'keyword.todo'],
        ],
   //....

Solution

  • I devised a solution.

            comments: [
                [/\/\//, "comment", "@comment_single"],
                [/\/\*/, 'comment.quote', '@comment_quote']
            ],
    
            comment_single: [
                [/\btodo\b/, 'keyword.todo'],
                [/.$/, 'comment', "@pop"],
                [/./, 'comment'],
            ],
    
            comment_quote: [
                [/\*\//, 'comment.quote', '@pop'],
                [/\btodo\b/, 'keyword.todo'],
                [/((?!\btodo\b).)*/, 'comment.quote'],
                [/[^*/]+/, 'comment.quote'],
                [/./, 'comment.quote'],
            ],