MCPcopy Create free account
hub / github.com/OpenEndedGroup/Field2 / stretchSpansOverChange

Function stretchSpansOverChange

lib/web/CodeMirror/src/line/spans.js:71–129  ·  view source on GitHub ↗
(doc, change)

Source from the content-addressed store, hash-verified

69// spans partially within the change. Returns an array of span
70// arrays with one element for each line in (after) the change.
71export function stretchSpansOverChange(doc, change) {
72 if (change.full) return null
73 let oldFirst = isLine(doc, change.from.line) && getLine(doc, change.from.line).markedSpans
74 let oldLast = isLine(doc, change.to.line) && getLine(doc, change.to.line).markedSpans
75 if (!oldFirst && !oldLast) return null
76
77 let startCh = change.from.ch, endCh = change.to.ch, isInsert = cmp(change.from, change.to) == 0
78 // Get the spans that 'stick out' on both sides
79 let first = markedSpansBefore(oldFirst, startCh, isInsert)
80 let last = markedSpansAfter(oldLast, endCh, isInsert)
81
82 // Next, merge those two ends
83 let sameLine = change.text.length == 1, offset = lst(change.text).length + (sameLine ? startCh : 0)
84 if (first) {
85 // Fix up .to properties of first
86 for (let i = 0; i < first.length; ++i) {
87 let span = first[i]
88 if (span.to == null) {
89 let found = getMarkedSpanFor(last, span.marker)
90 if (!found) span.to = startCh
91 else if (sameLine) span.to = found.to == null ? null : found.to + offset
92 }
93 }
94 }
95 if (last) {
96 // Fix up .from in last (or move them into first in case of sameLine)
97 for (let i = 0; i < last.length; ++i) {
98 let span = last[i]
99 if (span.to != null) span.to += offset
100 if (span.from == null) {
101 let found = getMarkedSpanFor(first, span.marker)
102 if (!found) {
103 span.from = offset
104 if (sameLine) (first || (first = [])).push(span)
105 }
106 } else {
107 span.from += offset
108 if (sameLine) (first || (first = [])).push(span)
109 }
110 }
111 }
112 // Make sure we didn't create any zero-length spans
113 if (first) first = clearEmptySpans(first)
114 if (last && last != first) last = clearEmptySpans(last)
115
116 let newMarkers = [first]
117 if (!sameLine) {
118 // Fill gap with whole-line-spans
119 let gap = change.text.length - 2, gapMarkers
120 if (gap > 0 && first)
121 for (let i = 0; i < first.length; ++i)
122 if (first[i].to == null)
123 (gapMarkers || (gapMarkers = [])).push(new MarkedSpan(first[i].marker, null, null))
124 for (let i = 0; i < gap; ++i)
125 newMarkers.push(gapMarkers)
126 newMarkers.push(last)
127 }
128 return newMarkers

Callers 2

mergeOldSpansFunction · 0.90
makeChangeInnerFunction · 0.90

Calls 9

isLineFunction · 0.90
getLineFunction · 0.90
cmpFunction · 0.90
lstFunction · 0.90
markedSpansBeforeFunction · 0.70
markedSpansAfterFunction · 0.70
getMarkedSpanForFunction · 0.70
clearEmptySpansFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected