(doc, change, markedSpans, selAfter, estimateHeight)
| 4124 | // DOCUMENT DATA STRUCTURE |
| 4125 | |
| 4126 | function updateDoc(doc, change, markedSpans, selAfter, estimateHeight) { |
| 4127 | function spansFor(n) {return markedSpans ? markedSpans[n] : null;} |
| 4128 | |
| 4129 | var from = change.from, to = change.to, text = change.text; |
| 4130 | var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line); |
| 4131 | var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line; |
| 4132 | |
| 4133 | // First adjust the line structure |
| 4134 | if (from.ch == 0 && to.ch == 0 && lastText == "") { |
| 4135 | // This is a whole-line replace. Treated specially to make |
| 4136 | // sure line objects move the way they are supposed to. |
| 4137 | for (var i = 0, e = text.length - 1, added = []; i < e; ++i) |
| 4138 | added.push(makeLine(text[i], spansFor(i), estimateHeight)); |
| 4139 | updateLine(lastLine, lastLine.text, lastSpans, estimateHeight); |
| 4140 | if (nlines) doc.remove(from.line, nlines); |
| 4141 | if (added.length) doc.insert(from.line, added); |
| 4142 | } else if (firstLine == lastLine) { |
| 4143 | if (text.length == 1) { |
| 4144 | updateLine(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), |
| 4145 | lastSpans, estimateHeight); |
| 4146 | } else { |
| 4147 | for (var added = [], i = 1, e = text.length - 1; i < e; ++i) |
| 4148 | added.push(makeLine(text[i], spansFor(i), estimateHeight)); |
| 4149 | added.push(makeLine(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight)); |
| 4150 | updateLine(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0), estimateHeight); |
| 4151 | doc.insert(from.line + 1, added); |
| 4152 | } |
| 4153 | } else if (text.length == 1) { |
| 4154 | updateLine(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), |
| 4155 | spansFor(0), estimateHeight); |
| 4156 | doc.remove(from.line + 1, nlines); |
| 4157 | } else { |
| 4158 | updateLine(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0), estimateHeight); |
| 4159 | updateLine(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans, estimateHeight); |
| 4160 | for (var i = 1, e = text.length - 1, added = []; i < e; ++i) |
| 4161 | added.push(makeLine(text[i], spansFor(i), estimateHeight)); |
| 4162 | if (nlines > 1) doc.remove(from.line + 1, nlines - 1); |
| 4163 | doc.insert(from.line + 1, added); |
| 4164 | } |
| 4165 | |
| 4166 | signalLater(doc, "change", doc, change); |
| 4167 | setSelection(doc, selAfter.anchor, selAfter.head, null, true); |
| 4168 | } |
| 4169 | |
| 4170 | function LeafChunk(lines) { |
| 4171 | this.lines = lines; |
no test coverage detected