I'm using Codemirror 5.3.
I'm tag matching html in a mixed-mode document to highlight the start and end tags - this works well. (https://codemirror.net/demo/matchtags.html)
I'm trying to capture the content between markers (in my case using a context-menu action where I right-click on tags), so I can send it off to an external process.
I use var tm = doc.getAllMarks();
and because I'm tag matching and not bookmarking, I pretty much know there's only going to be two items in the array. However, the TextMarker array that this returns doesn't (as far as I can tell) contain the {line, ch}
cursors for the marks.
Is there a proper way to get the starting and ending positions of the marks - either directly or as lines and character positions? The best I can think of is iterating each:
[].lines[0].parent.lines
and looking to see if each instance of CodeMirror.Line has a markedSpans
object, which would the give me the line index, and then use [].lines[0].markedSpans[0].from
and [].lines[0].markedSpans[0].to
to find the positions of the characters in the mark. And then use doc.getRange
to grab the content and shuffle it off for processing... something like this:
var tm = doc.getAllMarks(),
lines = tm[0].lines[0].parent.lines,
range = {
from: { line: 0, ch: 0},
to: { line: 0, ch: 0 }
},
hack = 0,
textContent = "";
for (var i=0,j=lines.length;i<j;i++) {
if (lines[i].hasOwnProperty("markedSpans")) {
if (hack==0) { // sorry, i'm in a hurry
range.from.line = i;
range.from.ch = lines[i].markedSpans.from;
hack=1;
} else {
range.to.line = i;
range.to.ch = lines[i].markedSpans.to;
}
}
}
textContent = doc.getRange(range.from,range.to);
All this sounds pretty glitchy, and I'm looking for a better way.
You can call .find()
on an object returned by markText
, it will return the {from, to}
position of the marker (or null
if the marker was cleared).