MCPcopy
hub / github.com/witheve/Eve / updateDoc

Function updateDoc

src/codemirror.js:7188–7236  ·  view source on GitHub ↗
(doc, change, markedSpans, estimateHeight)

Source from the content-addressed store, hash-verified

7186
7187 // Perform a change on the document data structure.
7188 function updateDoc(doc, change, markedSpans, estimateHeight) {
7189 function spansFor(n) {return markedSpans ? markedSpans[n] : null;}
7190 function update(line, text, spans) {
7191 updateLine(line, text, spans, estimateHeight);
7192 signalLater(line, "change", line, change);
7193 }
7194 function linesFor(start, end) {
7195 for (var i = start, result = []; i < end; ++i)
7196 result.push(new Line(text[i], spansFor(i), estimateHeight));
7197 return result;
7198 }
7199
7200 var from = change.from, to = change.to, text = change.text;
7201 var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line);
7202 var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line;
7203
7204 // Adjust the line structure
7205 if (change.full) {
7206 doc.insert(0, linesFor(0, text.length));
7207 doc.remove(text.length, doc.size - text.length);
7208 } else if (isWholeLineUpdate(doc, change)) {
7209 // This is a whole-line replace. Treated specially to make
7210 // sure line objects move the way they are supposed to.
7211 var added = linesFor(0, text.length - 1);
7212 update(lastLine, lastLine.text, lastSpans);
7213 if (nlines) doc.remove(from.line, nlines);
7214 if (added.length) doc.insert(from.line, added);
7215 } else if (firstLine == lastLine) {
7216 if (text.length == 1) {
7217 update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans);
7218 } else {
7219 var added = linesFor(1, text.length - 1);
7220 added.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight));
7221 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
7222 doc.insert(from.line + 1, added);
7223 }
7224 } else if (text.length == 1) {
7225 update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0));
7226 doc.remove(from.line + 1, nlines);
7227 } else {
7228 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
7229 update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans);
7230 var added = linesFor(1, text.length - 1);
7231 if (nlines > 1) doc.remove(from.line + 1, nlines - 1);
7232 doc.insert(from.line + 1, added);
7233 }
7234
7235 signalLater(doc, "change", doc, change);
7236 }
7237
7238 // The document is represented as a BTree consisting of leaves, with
7239 // chunk of lines in them, and branches, with up to ten leaves or

Callers 3

makeChangeSingleDocFunction · 0.85
codemirror.jsFile · 0.85

Calls 9

getLineFunction · 0.85
lstFunction · 0.85
spansForFunction · 0.85
linesForFunction · 0.85
isWholeLineUpdateFunction · 0.85
updateFunction · 0.85
signalLaterFunction · 0.85
insertMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected