Match word only in codemirror simplemode

184 views Asked by At

I am trying to write a very generic editor, and wanted to highlight any known keywords, no context awareness required.

I created the following regex

    var commonAttributes = ["var", "val", "let", "if", "else", "export", "import", "return", "static", "fun", "function", "func", "class", "open", "new", "as", "where", "select", "delete", "add", "limit", "update", "insert"]
    let standalonePrefix = "(?<=[\\s]|^|[\\(,:])"
    let standaloneSuffix = "(?=[\\s\\?\\!,:\\)\\();]|$)"

and the following state.

    {
          regex: new RegExp(standalonePrefix+"("+commonAttributes.join("|")+")"+standaloneSuffix, "i"),
          token: "keyword"
    },

I understand that to match at line beginning , I would have to use sol: true, as ^ has no meaning in our context. But this causes problems for me. without sol: true, writing

let leaflet let

will highlight all lets. with sol: true, only first let will match

let leaflet let

My desired outcome is that i get,

let leaflet let

How can I do so?

1

There are 1 answers

0
Blaine On

Since I couldnt find anything to work around this, I ended up using a workaround.

I removed ^ from my prefixes, and created another state with ^ and sol: true.

let standalonePrefix = "(?<=[\\s]|[\\(,:])"
{
        regex: new RegExp(standalonePrefix+"("+commonAttributes.join("|")+")"+standaloneSuffix, "i"),
        token: "keyword"
},
{
        regex: new RegExp("(?:^)("+commonAttributes.join("|")+")"+standaloneSuffix, "i"),
        sol: true,
        token: "keyword"
},