How to highlight the first word in each line in ACE editor? [closed]

江枫思渺然 提交于 2019-12-23 01:41:48

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/21300764/how-to-highlight-the-first-word-in-each-line-in-ace-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!