How to highlight the first word in each line in AC

2019-06-05 04:01发布

Suppose there's code like this:

ab cd
ef gh
  ij kl
mn

I want to highlight all words at line head: ab ef ij (yes, we have indentations) mn, how to write the regex:?

I had a try at http://ace.c9.io/tool/mode_creator.html but /^\s*/ /\n/ was not working as expected. How actually do they work?

2条回答
forever°为你锁心
2楼-- · 2019-06-05 04:14

Ace concatenates all the regular expressions in rules in a group. So if you have a group like

[{token: .., regex: "regex1"}, {token: .., regex: /regex2/}, {defaultToken: ..}]

Resulting regular expression will be /(regex1)(regex2)($)/ (see here) Every line in document is repeatedly matched to the resulting regex and for each match token with the type of corresponding rule is created. For unmatched text defaultToken is used.

Since regular expressions are matched line by line /\n/ in rules will not match anything.

As for your first question I think {token : "comment", regex : "^\\s*\\w+"} should work.

查看更多
干净又极端
3楼-- · 2019-06-05 04:38

Here's my solution, I have to put /^\s*/ before parameter, and it works.

this.$rules = {
    start: [{
        token: 'support.function',
        regex: /[^\(\)\"\s]+/,
        next: 'line'
    }],
    line: [{
        token: 'markup.raw',
        regex: /^\s*/,
        next: 'start',
    }, {
        token: 'variable.parameter',
        regex: /[^\(\)\"\s]+/
    }, {
        token: 'markup.raw',
        regex: /^\ */,
        next: 'start',
    }]
}

Full code here: https://github.com/Cirru/ace/blob/master/lib/ace/mode/cirru_highlight_rules.js

查看更多
登录 后发表回答