MCPcopy Create free account
hub / github.com/breck7/scroll / stretchSpansOverChange

Function stretchSpansOverChange

external/.scrollLibs.js:1235–1318  ·  view source on GitHub ↗
(doc, change)

Source from the content-addressed store, hash-verified

1233 // spans partially within the change. Returns an array of span
1234 // arrays with one element for each line in (after) the change.
1235 function stretchSpansOverChange(doc, change) {
1236 if (change.full) {
1237 return null
1238 }
1239 var oldFirst = isLine(doc, change.from.line) && getLine(doc, change.from.line).markedSpans
1240 var oldLast = isLine(doc, change.to.line) && getLine(doc, change.to.line).markedSpans
1241 if (!oldFirst && !oldLast) {
1242 return null
1243 }
1244
1245 var startCh = change.from.ch,
1246 endCh = change.to.ch,
1247 isInsert = cmp(change.from, change.to) == 0
1248 // Get the spans that 'stick out' on both sides
1249 var first = markedSpansBefore(oldFirst, startCh, isInsert)
1250 var last = markedSpansAfter(oldLast, endCh, isInsert)
1251
1252 // Next, merge those two ends
1253 var sameLine = change.text.length == 1,
1254 offset = lst(change.text).length + (sameLine ? startCh : 0)
1255 if (first) {
1256 // Fix up .to properties of first
1257 for (var i = 0; i < first.length; ++i) {
1258 var span = first[i]
1259 if (span.to == null) {
1260 var found = getMarkedSpanFor(last, span.marker)
1261 if (!found) {
1262 span.to = startCh
1263 } else if (sameLine) {
1264 span.to = found.to == null ? null : found.to + offset
1265 }
1266 }
1267 }
1268 }
1269 if (last) {
1270 // Fix up .from in last (or move them into first in case of sameLine)
1271 for (var i$1 = 0; i$1 < last.length; ++i$1) {
1272 var span$1 = last[i$1]
1273 if (span$1.to != null) {
1274 span$1.to += offset
1275 }
1276 if (span$1.from == null) {
1277 var found$1 = getMarkedSpanFor(first, span$1.marker)
1278 if (!found$1) {
1279 span$1.from = offset
1280 if (sameLine) {
1281 ;(first || (first = [])).push(span$1)
1282 }
1283 }
1284 } else {
1285 span$1.from += offset
1286 if (sameLine) {
1287 ;(first || (first = [])).push(span$1)
1288 }
1289 }
1290 }
1291 }
1292 // Make sure we didn't create any zero-length spans

Callers 2

mergeOldSpansFunction · 0.85
makeChangeInnerFunction · 0.85

Calls 8

isLineFunction · 0.85
getLineFunction · 0.85
cmpFunction · 0.85
markedSpansBeforeFunction · 0.85
markedSpansAfterFunction · 0.85
lstFunction · 0.85
getMarkedSpanForFunction · 0.85
clearEmptySpansFunction · 0.85

Tested by

no test coverage detected