Skip to content

Commit

Permalink
[markselection and activeline addons] Adjust to multi-selections
Browse files Browse the repository at this point in the history
Issue #778
  • Loading branch information
marijnh committed Jan 16, 2014
1 parent d206fb8 commit d3412a4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
44 changes: 29 additions & 15 deletions addon/selection/active-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
})();
14 changes: 7 additions & 7 deletions addon/selection/mark-selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit d3412a4

Please sign in to comment.