Search code examples
javascriptcodemirror

CodeMirror - Updating selections


So in CodeMirror you can get a selection by calling getCursor() doing the following:

cm.getCursor(true) // start selection
cm.getCursor(false) // end selection

This gives you the an object that holds the line and ch position of the cursor. With that you can mark text:

cm.markText(startPos, endPos, options)

Once I have marked the text, is it possible to update it? (without actually deleting it and adding a new mark)
Ex:

var t = cm.markText({ line:0, ch:0 }, { line:0, ch:10 });
t.setEnd({ line:0, ch:5 });

EDIT Since some people might look for it. This is how you modify marked text:

var t = editor.markText({ line:0, ch:0 }, { line:0, ch:10 }, {className: someClass});
t.clear();
t = editor.markText({ line:0, ch:0 }, { line:0, ch:5 }, {className: someClass});

Solution

  • No. The way the markers work is actually reversed from what you might think — the lines point to and remember their markers, not vice-versa.

    See the documentation about TextMarker from the source code.

    Here's the relevant quote:

    Line objects hold arrays (markedSpans) containing {from, to, marker} object pointing to such marker objects, and indicating that such a marker is present on that line.