Skip to content

Commit

Permalink
Different approach to ensuring marker.clear works well with undo
Browse files Browse the repository at this point in the history
The previous approach was quite misguided.
  • Loading branch information
marijnh committed Oct 18, 2012
1 parent ac059ff commit 431b443
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions lib/codemirror.js
Expand Up @@ -1508,22 +1508,17 @@ window.CodeMirror = (function() {

function TextMarker(type, style) { this.lines = []; this.type = type; if (style) this.style = style; }
TextMarker.prototype.clear = operation(function() {
var min, max, seen = {};
var min, max;
for (var i = 0; i < this.lines.length; ++i) {
var line = this.lines[i], lineN = lineNo(line);
seen[lineN] = newHL(line.text, line.markedSpans);
var line = this.lines[i];
var span = getMarkedSpanFor(line.markedSpans, this);
if (span.from != null) min = lineN;
if (span.to != null) max = lineN;
if (span.from != null) min = lineNo(line);
if (span.to != null) max = lineNo(line);
line.markedSpans = removeMarkedSpan(line.markedSpans, span);
}
if (min != null) {
changes.push({from: min, to: max + 1});
var old = [];
for (var i = min; i <= max; ++i) old.push(seen[i]);
history.addChange(min, old.length, old, true);
}
if (min != null) changes.push({from: min, to: max + 1});
this.lines.length = 0;
this.explicitlyCleared = true;
});
TextMarker.prototype.find = function() {
var from, to;
Expand All @@ -1545,18 +1540,16 @@ window.CodeMirror = (function() {
var marker = new TextMarker("range", className);
if (options) for (var opt in options) if (options.hasOwnProperty(opt))
marker[opt] = options[opt];
var curLine = from.line, old = [];
var curLine = from.line;
doc.iter(curLine, to.line + 1, function(line) {
var span = {from: curLine == from.line ? from.ch : null,
to: curLine == to.line ? to.ch : null,
marker: marker};
old.push(newHL(line.text, line.markedSpans));
line.markedSpans = (line.markedSpans || []).concat([span]);
marker.lines.push(line);
++curLine;
});
changes.push({from: from.line, to: to.line + 1});
history.addChange(from.line, old.length, old, true);
return marker;
}

Expand Down Expand Up @@ -2463,7 +2456,15 @@ window.CodeMirror = (function() {
// hl stands for history-line, a data structure that can be either a
// string (line without markers) or a {text, markedSpans} object.
function hlText(val) { return typeof val == "string" ? val : val.text; }
function hlSpans(val) { return typeof val == "string" ? null : val.markedSpans; }
function hlSpans(val) {
if (typeof val == "string") return null;
var spans = val.markedSpans, out = null;
for (var i = 0; i < spans.length; ++i) {
if (spans[i].marker.explicitlyCleared) { if (!out) out = spans.slice(0, i); }
else if (out) out.push(spans[i]);
}
return !out ? spans : out.length ? out : null;
}
function newHL(text, spans) { return spans ? {text: text, markedSpans: spans} : text; }

function detachMarkedSpans(line) {
Expand Down Expand Up @@ -2888,12 +2889,12 @@ window.CodeMirror = (function() {
this.closed = false;
}
History.prototype = {
addChange: function(start, added, old, minor) {
addChange: function(start, added, old) {
this.undone.length = 0;
var time = +new Date, cur = lst(this.done), last = cur && lst(cur);
var dtime = time - this.time;

if (cur && !this.closed && (this.compound || minor)) {
if (cur && !this.closed && this.compound) {
cur.push({start: start, added: added, old: old});
} else if (dtime > 400 || !last || this.closed ||
last.start > start + old.length || last.start + last.added < start) {
Expand Down

0 comments on commit 431b443

Please sign in to comment.