(doc, change, markedSpans, estimateHeight)
| 5925 | |
| 5926 | // Perform a change on the document data structure. |
| 5927 | function updateDoc(doc, change, markedSpans, estimateHeight) { |
| 5928 | function spansFor(n) {return markedSpans ? markedSpans[n] : null;} |
| 5929 | function update(line, text, spans) { |
| 5930 | updateLine(line, text, spans, estimateHeight); |
| 5931 | signalLater(line, "change", line, change); |
| 5932 | } |
| 5933 | |
| 5934 | var from = change.from, to = change.to, text = change.text; |
| 5935 | var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line); |
| 5936 | var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line; |
| 5937 | |
| 5938 | // Adjust the line structure |
| 5939 | if (isWholeLineUpdate(doc, change)) { |
| 5940 | // This is a whole-line replace. Treated specially to make |
| 5941 | // sure line objects move the way they are supposed to. |
| 5942 | for (var i = 0, added = []; i < text.length - 1; ++i) |
| 5943 | added.push(new Line(text[i], spansFor(i), estimateHeight)); |
| 5944 | update(lastLine, lastLine.text, lastSpans); |
| 5945 | if (nlines) doc.remove(from.line, nlines); |
| 5946 | if (added.length) doc.insert(from.line, added); |
| 5947 | } else if (firstLine == lastLine) { |
| 5948 | if (text.length == 1) { |
| 5949 | update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans); |
| 5950 | } else { |
| 5951 | for (var added = [], i = 1; i < text.length - 1; ++i) |
| 5952 | added.push(new Line(text[i], spansFor(i), estimateHeight)); |
| 5953 | added.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight)); |
| 5954 | update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0)); |
| 5955 | doc.insert(from.line + 1, added); |
| 5956 | } |
| 5957 | } else if (text.length == 1) { |
| 5958 | update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0)); |
| 5959 | doc.remove(from.line + 1, nlines); |
| 5960 | } else { |
| 5961 | update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0)); |
| 5962 | update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans); |
| 5963 | for (var i = 1, added = []; i < text.length - 1; ++i) |
| 5964 | added.push(new Line(text[i], spansFor(i), estimateHeight)); |
| 5965 | if (nlines > 1) doc.remove(from.line + 1, nlines - 1); |
| 5966 | doc.insert(from.line + 1, added); |
| 5967 | } |
| 5968 | |
| 5969 | signalLater(doc, "change", doc, change); |
| 5970 | } |
| 5971 | |
| 5972 | // The document is represented as a BTree consisting of leaves, with |
| 5973 | // chunk of lines in them, and branches, with up to ten leaves or |
no test coverage detected
searching dependent graphs…