spans.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import { indexOf, lst } from "../util/misc.js"
  2. import { cmp } from "./pos.js"
  3. import { sawCollapsedSpans } from "./saw_special_spans.js"
  4. import { getLine, isLine, lineNo } from "./utils_line.js"
  5. // TEXTMARKER SPANS
  6. export function MarkedSpan(marker, from, to) {
  7. this.marker = marker
  8. this.from = from; this.to = to
  9. }
  10. // Search an array of spans for a span matching the given marker.
  11. export function getMarkedSpanFor(spans, marker) {
  12. if (spans) for (let i = 0; i < spans.length; ++i) {
  13. let span = spans[i]
  14. if (span.marker == marker) return span
  15. }
  16. }
  17. // Remove a span from an array, returning undefined if no spans are
  18. // left (we don't store arrays for lines without spans).
  19. export function removeMarkedSpan(spans, span) {
  20. let r
  21. for (let i = 0; i < spans.length; ++i)
  22. if (spans[i] != span) (r || (r = [])).push(spans[i])
  23. return r
  24. }
  25. // Add a span to a line.
  26. export function addMarkedSpan(line, span) {
  27. line.markedSpans = line.markedSpans ? line.markedSpans.concat([span]) : [span]
  28. span.marker.attachLine(line)
  29. }
  30. // Used for the algorithm that adjusts markers for a change in the
  31. // document. These functions cut an array of spans at a given
  32. // character position, returning an array of remaining chunks (or
  33. // undefined if nothing remains).
  34. function markedSpansBefore(old, startCh, isInsert) {
  35. let nw
  36. if (old) for (let i = 0; i < old.length; ++i) {
  37. let span = old[i], marker = span.marker
  38. let startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= startCh : span.from < startCh)
  39. if (startsBefore || span.from == startCh && marker.type == "bookmark" && (!isInsert || !span.marker.insertLeft)) {
  40. let endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= startCh : span.to > startCh)
  41. ;(nw || (nw = [])).push(new MarkedSpan(marker, span.from, endsAfter ? null : span.to))
  42. }
  43. }
  44. return nw
  45. }
  46. function markedSpansAfter(old, endCh, isInsert) {
  47. let nw
  48. if (old) for (let i = 0; i < old.length; ++i) {
  49. let span = old[i], marker = span.marker
  50. let endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= endCh : span.to > endCh)
  51. if (endsAfter || span.from == endCh && marker.type == "bookmark" && (!isInsert || span.marker.insertLeft)) {
  52. let startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= endCh : span.from < endCh)
  53. ;(nw || (nw = [])).push(new MarkedSpan(marker, startsBefore ? null : span.from - endCh,
  54. span.to == null ? null : span.to - endCh))
  55. }
  56. }
  57. return nw
  58. }
  59. // Given a change object, compute the new set of marker spans that
  60. // cover the line in which the change took place. Removes spans
  61. // entirely within the change, reconnects spans belonging to the
  62. // same marker that appear on both sides of the change, and cuts off
  63. // spans partially within the change. Returns an array of span
  64. // arrays with one element for each line in (after) the change.
  65. export function stretchSpansOverChange(doc, change) {
  66. if (change.full) return null
  67. let oldFirst = isLine(doc, change.from.line) && getLine(doc, change.from.line).markedSpans
  68. let oldLast = isLine(doc, change.to.line) && getLine(doc, change.to.line).markedSpans
  69. if (!oldFirst && !oldLast) return null
  70. let startCh = change.from.ch, endCh = change.to.ch, isInsert = cmp(change.from, change.to) == 0
  71. // Get the spans that 'stick out' on both sides
  72. let first = markedSpansBefore(oldFirst, startCh, isInsert)
  73. let last = markedSpansAfter(oldLast, endCh, isInsert)
  74. // Next, merge those two ends
  75. let sameLine = change.text.length == 1, offset = lst(change.text).length + (sameLine ? startCh : 0)
  76. if (first) {
  77. // Fix up .to properties of first
  78. for (let i = 0; i < first.length; ++i) {
  79. let span = first[i]
  80. if (span.to == null) {
  81. let found = getMarkedSpanFor(last, span.marker)
  82. if (!found) span.to = startCh
  83. else if (sameLine) span.to = found.to == null ? null : found.to + offset
  84. }
  85. }
  86. }
  87. if (last) {
  88. // Fix up .from in last (or move them into first in case of sameLine)
  89. for (let i = 0; i < last.length; ++i) {
  90. let span = last[i]
  91. if (span.to != null) span.to += offset
  92. if (span.from == null) {
  93. let found = getMarkedSpanFor(first, span.marker)
  94. if (!found) {
  95. span.from = offset
  96. if (sameLine) (first || (first = [])).push(span)
  97. }
  98. } else {
  99. span.from += offset
  100. if (sameLine) (first || (first = [])).push(span)
  101. }
  102. }
  103. }
  104. // Make sure we didn't create any zero-length spans
  105. if (first) first = clearEmptySpans(first)
  106. if (last && last != first) last = clearEmptySpans(last)
  107. let newMarkers = [first]
  108. if (!sameLine) {
  109. // Fill gap with whole-line-spans
  110. let gap = change.text.length - 2, gapMarkers
  111. if (gap > 0 && first)
  112. for (let i = 0; i < first.length; ++i)
  113. if (first[i].to == null)
  114. (gapMarkers || (gapMarkers = [])).push(new MarkedSpan(first[i].marker, null, null))
  115. for (let i = 0; i < gap; ++i)
  116. newMarkers.push(gapMarkers)
  117. newMarkers.push(last)
  118. }
  119. return newMarkers
  120. }
  121. // Remove spans that are empty and don't have a clearWhenEmpty
  122. // option of false.
  123. function clearEmptySpans(spans) {
  124. for (let i = 0; i < spans.length; ++i) {
  125. let span = spans[i]
  126. if (span.from != null && span.from == span.to && span.marker.clearWhenEmpty !== false)
  127. spans.splice(i--, 1)
  128. }
  129. if (!spans.length) return null
  130. return spans
  131. }
  132. // Used to 'clip' out readOnly ranges when making a change.
  133. export function removeReadOnlyRanges(doc, from, to) {
  134. let markers = null
  135. doc.iter(from.line, to.line + 1, line => {
  136. if (line.markedSpans) for (let i = 0; i < line.markedSpans.length; ++i) {
  137. let mark = line.markedSpans[i].marker
  138. if (mark.readOnly && (!markers || indexOf(markers, mark) == -1))
  139. (markers || (markers = [])).push(mark)
  140. }
  141. })
  142. if (!markers) return null
  143. let parts = [{from: from, to: to}]
  144. for (let i = 0; i < markers.length; ++i) {
  145. let mk = markers[i], m = mk.find(0)
  146. for (let j = 0; j < parts.length; ++j) {
  147. let p = parts[j]
  148. if (cmp(p.to, m.from) < 0 || cmp(p.from, m.to) > 0) continue
  149. let newParts = [j, 1], dfrom = cmp(p.from, m.from), dto = cmp(p.to, m.to)
  150. if (dfrom < 0 || !mk.inclusiveLeft && !dfrom)
  151. newParts.push({from: p.from, to: m.from})
  152. if (dto > 0 || !mk.inclusiveRight && !dto)
  153. newParts.push({from: m.to, to: p.to})
  154. parts.splice.apply(parts, newParts)
  155. j += newParts.length - 3
  156. }
  157. }
  158. return parts
  159. }
  160. // Connect or disconnect spans from a line.
  161. export function detachMarkedSpans(line) {
  162. let spans = line.markedSpans
  163. if (!spans) return
  164. for (let i = 0; i < spans.length; ++i)
  165. spans[i].marker.detachLine(line)
  166. line.markedSpans = null
  167. }
  168. export function attachMarkedSpans(line, spans) {
  169. if (!spans) return
  170. for (let i = 0; i < spans.length; ++i)
  171. spans[i].marker.attachLine(line)
  172. line.markedSpans = spans
  173. }
  174. // Helpers used when computing which overlapping collapsed span
  175. // counts as the larger one.
  176. function extraLeft(marker) { return marker.inclusiveLeft ? -1 : 0 }
  177. function extraRight(marker) { return marker.inclusiveRight ? 1 : 0 }
  178. // Returns a number indicating which of two overlapping collapsed
  179. // spans is larger (and thus includes the other). Falls back to
  180. // comparing ids when the spans cover exactly the same range.
  181. export function compareCollapsedMarkers(a, b) {
  182. let lenDiff = a.lines.length - b.lines.length
  183. if (lenDiff != 0) return lenDiff
  184. let aPos = a.find(), bPos = b.find()
  185. let fromCmp = cmp(aPos.from, bPos.from) || extraLeft(a) - extraLeft(b)
  186. if (fromCmp) return -fromCmp
  187. let toCmp = cmp(aPos.to, bPos.to) || extraRight(a) - extraRight(b)
  188. if (toCmp) return toCmp
  189. return b.id - a.id
  190. }
  191. // Find out whether a line ends or starts in a collapsed span. If
  192. // so, return the marker for that span.
  193. function collapsedSpanAtSide(line, start) {
  194. let sps = sawCollapsedSpans && line.markedSpans, found
  195. if (sps) for (let sp, i = 0; i < sps.length; ++i) {
  196. sp = sps[i]
  197. if (sp.marker.collapsed && (start ? sp.from : sp.to) == null &&
  198. (!found || compareCollapsedMarkers(found, sp.marker) < 0))
  199. found = sp.marker
  200. }
  201. return found
  202. }
  203. export function collapsedSpanAtStart(line) { return collapsedSpanAtSide(line, true) }
  204. export function collapsedSpanAtEnd(line) { return collapsedSpanAtSide(line, false) }
  205. export function collapsedSpanAround(line, ch) {
  206. let sps = sawCollapsedSpans && line.markedSpans, found
  207. if (sps) for (let i = 0; i < sps.length; ++i) {
  208. let sp = sps[i]
  209. if (sp.marker.collapsed && (sp.from == null || sp.from < ch) && (sp.to == null || sp.to > ch) &&
  210. (!found || compareCollapsedMarkers(found, sp.marker) < 0)) found = sp.marker
  211. }
  212. return found
  213. }
  214. // Test whether there exists a collapsed span that partially
  215. // overlaps (covers the start or end, but not both) of a new span.
  216. // Such overlap is not allowed.
  217. export function conflictingCollapsedRange(doc, lineNo, from, to, marker) {
  218. let line = getLine(doc, lineNo)
  219. let sps = sawCollapsedSpans && line.markedSpans
  220. if (sps) for (let i = 0; i < sps.length; ++i) {
  221. let sp = sps[i]
  222. if (!sp.marker.collapsed) continue
  223. let found = sp.marker.find(0)
  224. let fromCmp = cmp(found.from, from) || extraLeft(sp.marker) - extraLeft(marker)
  225. let toCmp = cmp(found.to, to) || extraRight(sp.marker) - extraRight(marker)
  226. if (fromCmp >= 0 && toCmp <= 0 || fromCmp <= 0 && toCmp >= 0) continue
  227. if (fromCmp <= 0 && (sp.marker.inclusiveRight && marker.inclusiveLeft ? cmp(found.to, from) >= 0 : cmp(found.to, from) > 0) ||
  228. fromCmp >= 0 && (sp.marker.inclusiveRight && marker.inclusiveLeft ? cmp(found.from, to) <= 0 : cmp(found.from, to) < 0))
  229. return true
  230. }
  231. }
  232. // A visual line is a line as drawn on the screen. Folding, for
  233. // example, can cause multiple logical lines to appear on the same
  234. // visual line. This finds the start of the visual line that the
  235. // given line is part of (usually that is the line itself).
  236. export function visualLine(line) {
  237. let merged
  238. while (merged = collapsedSpanAtStart(line))
  239. line = merged.find(-1, true).line
  240. return line
  241. }
  242. export function visualLineEnd(line) {
  243. let merged
  244. while (merged = collapsedSpanAtEnd(line))
  245. line = merged.find(1, true).line
  246. return line
  247. }
  248. // Returns an array of logical lines that continue the visual line
  249. // started by the argument, or undefined if there are no such lines.
  250. export function visualLineContinued(line) {
  251. let merged, lines
  252. while (merged = collapsedSpanAtEnd(line)) {
  253. line = merged.find(1, true).line
  254. ;(lines || (lines = [])).push(line)
  255. }
  256. return lines
  257. }
  258. // Get the line number of the start of the visual line that the
  259. // given line number is part of.
  260. export function visualLineNo(doc, lineN) {
  261. let line = getLine(doc, lineN), vis = visualLine(line)
  262. if (line == vis) return lineN
  263. return lineNo(vis)
  264. }
  265. // Get the line number of the start of the next visual line after
  266. // the given line.
  267. export function visualLineEndNo(doc, lineN) {
  268. if (lineN > doc.lastLine()) return lineN
  269. let line = getLine(doc, lineN), merged
  270. if (!lineIsHidden(doc, line)) return lineN
  271. while (merged = collapsedSpanAtEnd(line))
  272. line = merged.find(1, true).line
  273. return lineNo(line) + 1
  274. }
  275. // Compute whether a line is hidden. Lines count as hidden when they
  276. // are part of a visual line that starts with another line, or when
  277. // they are entirely covered by collapsed, non-widget span.
  278. export function lineIsHidden(doc, line) {
  279. let sps = sawCollapsedSpans && line.markedSpans
  280. if (sps) for (let sp, i = 0; i < sps.length; ++i) {
  281. sp = sps[i]
  282. if (!sp.marker.collapsed) continue
  283. if (sp.from == null) return true
  284. if (sp.marker.widgetNode) continue
  285. if (sp.from == 0 && sp.marker.inclusiveLeft && lineIsHiddenInner(doc, line, sp))
  286. return true
  287. }
  288. }
  289. function lineIsHiddenInner(doc, line, span) {
  290. if (span.to == null) {
  291. let end = span.marker.find(1, true)
  292. return lineIsHiddenInner(doc, end.line, getMarkedSpanFor(end.line.markedSpans, span.marker))
  293. }
  294. if (span.marker.inclusiveRight && span.to == line.text.length)
  295. return true
  296. for (let sp, i = 0; i < line.markedSpans.length; ++i) {
  297. sp = line.markedSpans[i]
  298. if (sp.marker.collapsed && !sp.marker.widgetNode && sp.from == span.to &&
  299. (sp.to == null || sp.to != span.from) &&
  300. (sp.marker.inclusiveLeft || span.marker.inclusiveRight) &&
  301. lineIsHiddenInner(doc, line, sp)) return true
  302. }
  303. }
  304. // Find the height above the given line.
  305. export function heightAtLine(lineObj) {
  306. lineObj = visualLine(lineObj)
  307. let h = 0, chunk = lineObj.parent
  308. for (let i = 0; i < chunk.lines.length; ++i) {
  309. let line = chunk.lines[i]
  310. if (line == lineObj) break
  311. else h += line.height
  312. }
  313. for (let p = chunk.parent; p; chunk = p, p = chunk.parent) {
  314. for (let i = 0; i < p.children.length; ++i) {
  315. let cur = p.children[i]
  316. if (cur == chunk) break
  317. else h += cur.height
  318. }
  319. }
  320. return h
  321. }
  322. // Compute the character length of a line, taking into account
  323. // collapsed ranges (see markText) that might hide parts, and join
  324. // other lines onto it.
  325. export function lineLength(line) {
  326. if (line.height == 0) return 0
  327. let len = line.text.length, merged, cur = line
  328. while (merged = collapsedSpanAtStart(cur)) {
  329. let found = merged.find(0, true)
  330. cur = found.from.line
  331. len += found.from.ch - found.to.ch
  332. }
  333. cur = line
  334. while (merged = collapsedSpanAtEnd(cur)) {
  335. let found = merged.find(0, true)
  336. len -= cur.text.length - found.from.ch
  337. cur = found.to.line
  338. len += cur.text.length - found.to.ch
  339. }
  340. return len
  341. }
  342. // Find the longest line in the document.
  343. export function findMaxLine(cm) {
  344. let d = cm.display, doc = cm.doc
  345. d.maxLine = getLine(doc, doc.first)
  346. d.maxLineLength = lineLength(d.maxLine)
  347. d.maxLineChanged = true
  348. doc.iter(line => {
  349. let len = lineLength(line)
  350. if (len > d.maxLineLength) {
  351. d.maxLineLength = len
  352. d.maxLine = line
  353. }
  354. })
  355. }