Skip to content
Snippets Groups Projects
Commit 4290648c authored by Marijn Haverbeke's avatar Marijn Haverbeke
Browse files

[markselection and activeline addons] Adjust to multi-selections

Issue #778
parent 31859376
No related branches found
No related tags found
No related merge requests found
......@@ -12,34 +12,48 @@
CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) {
var prev = old && old != CodeMirror.Init;
if (val && !prev) {
updateActiveLine(cm, cm.getCursor().line);
cm.state.activeLines = [];
updateActiveLines(cm, cm.listSelections());
cm.on("beforeSelectionChange", selectionChange);
} else if (!val && prev) {
cm.off("beforeSelectionChange", selectionChange);
clearActiveLine(cm);
delete cm.state.activeLine;
clearActiveLines(cm);
delete cm.state.activeLines;
}
});
function clearActiveLine(cm) {
if ("activeLine" in cm.state) {
cm.removeLineClass(cm.state.activeLine, "wrap", WRAP_CLASS);
cm.removeLineClass(cm.state.activeLine, "background", BACK_CLASS);
function clearActiveLines(cm) {
for (var i = 0; i < cm.state.activeLines.length; i++) {
cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS);
cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS);
}
}
function updateActiveLine(cm, selectedLine) {
var line = cm.getLineHandleVisualStart(selectedLine);
if (cm.state.activeLine == line) return;
function sameArray(a, b) {
if (a.length != b.length) return false;
for (var i = 0; i < a.length; i++)
if (a[i] != b[i]) return false;
return true;
}
function updateActiveLines(cm, ranges) {
var active = [];
for (var i = 0; i < ranges.length; i++) {
var line = cm.getLineHandleVisualStart(ranges[i].head.line);
if (active[active.length - 1] != line) active.push(line);
}
if (sameArray(cm.state.activeLines, active)) return;
cm.operation(function() {
clearActiveLine(cm);
cm.addLineClass(line, "wrap", WRAP_CLASS);
cm.addLineClass(line, "background", BACK_CLASS);
cm.state.activeLine = line;
clearActiveLines(cm);
for (var i = 0; i < active.length; i++) {
cm.addLineClass(active[i], "wrap", WRAP_CLASS);
cm.addLineClass(active[i], "background", BACK_CLASS);
}
cm.state.activeLines = active;
});
}
function selectionChange(cm, sel) {
updateActiveLine(cm, sel.head.line);
updateActiveLines(cm, sel.ranges);
}
})();
......@@ -34,10 +34,7 @@
var CHUNK_SIZE = 8;
var Pos = CodeMirror.Pos;
function cmp(pos1, pos2) {
return pos1.line - pos2.line || pos1.ch - pos2.ch;
}
var cmp = CodeMirror.cmpPos;
function coverRange(cm, from, to, addAt) {
if (cmp(from, to) == 0) return;
......@@ -63,13 +60,16 @@
function reset(cm) {
clear(cm);
var from = cm.getCursor("start"), to = cm.getCursor("end");
coverRange(cm, from, to);
var ranges = cm.listSelections();
for (var i = 0; i < ranges.length; i++)
coverRange(cm, ranges[i].from(), ranges[i].to());
}
function update(cm) {
if (!cm.somethingSelected()) return clear(cm);
if (cm.listSelections().length > 1) return reset(cm);
var from = cm.getCursor("start"), to = cm.getCursor("end");
if (cmp(from, to) == 0) return clear(cm);
var array = cm.state.markedSelection;
if (!array.length) return coverRange(cm, from, to);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment