Search code examples
dslcodemirror

CodeMirror: ad-hoc syntax highlighting


Can I use CodeMirror in more ad-hoc mode, where the "grammar", or, rather, the highlighting, emerges as the user types the code?

Assume, for example, that I would like to highlight similarities between sentences. Then, assume that the user types:

this is a sentence
this is another sentence

Assume, further, that the user then puts the cursor on one of the sentences. I'd like all the common subsequences to be highlighted between the two sentences, like so:

|this is| a |sentence|
|this is| another |sentence|

Is CodeMirror the right answer for these kind of ad-hoc syntax highlighting tasks, or do I have to roll my own?


Solution

  • If you can create your dynamic mode at run-time, you can use setOption to update the mode for a running editor. Alternatively, use markText to do the highlighting programmatically.