Skip to content

Commit 431b443

Browse files
committed
Different approach to ensuring marker.clear works well with undo
The previous approach was quite misguided.
1 parent ac059ff commit 431b443

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

lib/codemirror.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,22 +1508,17 @@ window.CodeMirror = (function() {
15081508

15091509
function TextMarker(type, style) { this.lines = []; this.type = type; if (style) this.style = style; }
15101510
TextMarker.prototype.clear = operation(function() {
1511-
var min, max, seen = {};
1511+
var min, max;
15121512
for (var i = 0; i < this.lines.length; ++i) {
1513-
var line = this.lines[i], lineN = lineNo(line);
1514-
seen[lineN] = newHL(line.text, line.markedSpans);
1513+
var line = this.lines[i];
15151514
var span = getMarkedSpanFor(line.markedSpans, this);
1516-
if (span.from != null) min = lineN;
1517-
if (span.to != null) max = lineN;
1515+
if (span.from != null) min = lineNo(line);
1516+
if (span.to != null) max = lineNo(line);
15181517
line.markedSpans = removeMarkedSpan(line.markedSpans, span);
15191518
}
1520-
if (min != null) {
1521-
changes.push({from: min, to: max + 1});
1522-
var old = [];
1523-
for (var i = min; i <= max; ++i) old.push(seen[i]);
1524-
history.addChange(min, old.length, old, true);
1525-
}
1519+
if (min != null) changes.push({from: min, to: max + 1});
15261520
this.lines.length = 0;
1521+
this.explicitlyCleared = true;
15271522
});
15281523
TextMarker.prototype.find = function() {
15291524
var from, to;
@@ -1545,18 +1540,16 @@ window.CodeMirror = (function() {
15451540
var marker = new TextMarker("range", className);
15461541
if (options) for (var opt in options) if (options.hasOwnProperty(opt))
15471542
marker[opt] = options[opt];
1548-
var curLine = from.line, old = [];
1543+
var curLine = from.line;
15491544
doc.iter(curLine, to.line + 1, function(line) {
15501545
var span = {from: curLine == from.line ? from.ch : null,
15511546
to: curLine == to.line ? to.ch : null,
15521547
marker: marker};
1553-
old.push(newHL(line.text, line.markedSpans));
15541548
line.markedSpans = (line.markedSpans || []).concat([span]);
15551549
marker.lines.push(line);
15561550
++curLine;
15571551
});
15581552
changes.push({from: from.line, to: to.line + 1});
1559-
history.addChange(from.line, old.length, old, true);
15601553
return marker;
15611554
}
15621555

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

24692470
function detachMarkedSpans(line) {
@@ -2888,12 +2889,12 @@ window.CodeMirror = (function() {
28882889
this.closed = false;
28892890
}
28902891
History.prototype = {
2891-
addChange: function(start, added, old, minor) {
2892+
addChange: function(start, added, old) {
28922893
this.undone.length = 0;
28932894
var time = +new Date, cur = lst(this.done), last = cur && lst(cur);
28942895
var dtime = time - this.time;
28952896

2896-
if (cur && !this.closed && (this.compound || minor)) {
2897+
if (cur && !this.closed && this.compound) {
28972898
cur.push({start: start, added: added, old: old});
28982899
} else if (dtime > 400 || !last || this.closed ||
28992900
last.start > start + old.length || last.start + last.added < start) {

0 commit comments

Comments
 (0)