MCPcopy
hub / github.com/csev/py4e / updateDoc

Function updateDoc

tools/pythonauto/static/codemirrorepl/codemirror.js:5877–5920  ·  view source on GitHub ↗
(doc, change, markedSpans, estimateHeight)

Source from the content-addressed store, hash-verified

5875
5876 // Perform a change on the document data structure.
5877 function updateDoc(doc, change, markedSpans, estimateHeight) {
5878 function spansFor(n) {return markedSpans ? markedSpans[n] : null;}
5879 function update(line, text, spans) {
5880 updateLine(line, text, spans, estimateHeight);
5881 signalLater(line, "change", line, change);
5882 }
5883
5884 var from = change.from, to = change.to, text = change.text;
5885 var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line);
5886 var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line;
5887
5888 // Adjust the line structure
5889 if (isWholeLineUpdate(doc, change)) {
5890 // This is a whole-line replace. Treated specially to make
5891 // sure line objects move the way they are supposed to.
5892 for (var i = 0, added = []; i < text.length - 1; ++i)
5893 added.push(new Line(text[i], spansFor(i), estimateHeight));
5894 update(lastLine, lastLine.text, lastSpans);
5895 if (nlines) doc.remove(from.line, nlines);
5896 if (added.length) doc.insert(from.line, added);
5897 } else if (firstLine == lastLine) {
5898 if (text.length == 1) {
5899 update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans);
5900 } else {
5901 for (var added = [], i = 1; i < text.length - 1; ++i)
5902 added.push(new Line(text[i], spansFor(i), estimateHeight));
5903 added.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight));
5904 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
5905 doc.insert(from.line + 1, added);
5906 }
5907 } else if (text.length == 1) {
5908 update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0));
5909 doc.remove(from.line + 1, nlines);
5910 } else {
5911 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
5912 update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans);
5913 for (var i = 1, added = []; i < text.length - 1; ++i)
5914 added.push(new Line(text[i], spansFor(i), estimateHeight));
5915 if (nlines > 1) doc.remove(from.line + 1, nlines - 1);
5916 doc.insert(from.line + 1, added);
5917 }
5918
5919 signalLater(doc, "change", doc, change);
5920 }
5921
5922 // The document is represented as a BTree consisting of leaves, with
5923 // chunk of lines in them, and branches, with up to ten leaves or

Callers 3

makeChangeSingleDocFunction · 0.70
codemirror.jsFile · 0.70

Calls 8

isWholeLineUpdateFunction · 0.85
sliceMethod · 0.80
getLineFunction · 0.70
lstFunction · 0.70
spansForFunction · 0.70
updateFunction · 0.70
signalLaterFunction · 0.70
insertMethod · 0.45

Tested by

no test coverage detected