Search code examples
javascriptsyntax-highlightingace-editor

What is the difference between .addRules() and .embedRules() in AceJS?


From Ace documentation:

Like addRules, embedRules adds on to the existing this.$rules object.

And from the given examples I see no difference between these two methods.

So,

What is the difference between these two methods? Can you provide examples of using them?


Solution

  • see https://github.com/ajaxorg/ace/blob/master/lib/ace/mode/text_highlight_rules.js#L53-L104 embedRules calls addRules internally, but takes a different set of parameters.

    Basically embedRules allows to take highlight rules constructor of a language add escape rules and add resulting rules to the language

    this.embedTagRules(
       JavaScriptHighlightRules,
       "js-", 
       [{regex: "</script>", token: "tag"}], // token that should return to initial state
       undefined, // add escape rules to all js states
       false      // escape rules should match before js rules
    )