(doc, change, markedSpans, estimateHeight)
| 7160 | |
| 7161 | // Perform a change on the document data structure. |
| 7162 | function updateDoc(doc, change, markedSpans, estimateHeight) { |
| 7163 | function spansFor(n) {return markedSpans ? markedSpans[n] : null;} |
| 7164 | function update(line, text, spans) { |
| 7165 | updateLine(line, text, spans, estimateHeight); |
| 7166 | signalLater(line, "change", line, change); |
| 7167 | } |
| 7168 | function linesFor(start, end) { |
| 7169 | for (var i = start, result = []; i < end; ++i) |
| 7170 | result.push(new Line(text[i], spansFor(i), estimateHeight)); |
| 7171 | return result; |
| 7172 | } |
| 7173 | |
| 7174 | var from = change.from, to = change.to, text = change.text; |
| 7175 | var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line); |
| 7176 | var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line; |
| 7177 | |
| 7178 | // Adjust the line structure |
| 7179 | if (change.full) { |
| 7180 | doc.insert(0, linesFor(0, text.length)); |
| 7181 | doc.remove(text.length, doc.size - text.length); |
| 7182 | } else if (isWholeLineUpdate(doc, change)) { |
| 7183 | // This is a whole-line replace. Treated specially to make |
| 7184 | // sure line objects move the way they are supposed to. |
| 7185 | var added = linesFor(0, text.length - 1); |
| 7186 | update(lastLine, lastLine.text, lastSpans); |
| 7187 | if (nlines) doc.remove(from.line, nlines); |
| 7188 | if (added.length) doc.insert(from.line, added); |
| 7189 | } else if (firstLine == lastLine) { |
| 7190 | if (text.length == 1) { |
| 7191 | update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans); |
| 7192 | } else { |
| 7193 | var added = linesFor(1, text.length - 1); |
| 7194 | added.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight)); |
| 7195 | update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0)); |
| 7196 | doc.insert(from.line + 1, added); |
| 7197 | } |
| 7198 | } else if (text.length == 1) { |
| 7199 | update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0)); |
| 7200 | doc.remove(from.line + 1, nlines); |
| 7201 | } else { |
| 7202 | update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0)); |
| 7203 | update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans); |
| 7204 | var added = linesFor(1, text.length - 1); |
| 7205 | if (nlines > 1) doc.remove(from.line + 1, nlines - 1); |
| 7206 | doc.insert(from.line + 1, added); |
| 7207 | } |
| 7208 | |
| 7209 | signalLater(doc, "change", doc, change); |
| 7210 | } |
| 7211 | |
| 7212 | // The document is represented as a BTree consisting of leaves, with |
| 7213 | // chunk of lines in them, and branches, with up to ten leaves or |
no test coverage detected