db1285ee601ac542b0c98b2c58ec25ba5c32a3333388feb43e5e39837b0d7891b711c44271e29df8a71d4be9824f084b45c689ff78054ebe7c3aa7b773a3c4 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { indexOf } from "../util/misc.js"
  2. // Find the line object corresponding to the given line number.
  3. export function getLine(doc, n) {
  4. n -= doc.first
  5. if (n < 0 || n >= doc.size) throw new Error("There is no line " + (n + doc.first) + " in the document.")
  6. let chunk = doc
  7. while (!chunk.lines) {
  8. for (let i = 0;; ++i) {
  9. let child = chunk.children[i], sz = child.chunkSize()
  10. if (n < sz) { chunk = child; break }
  11. n -= sz
  12. }
  13. }
  14. return chunk.lines[n]
  15. }
  16. // Get the part of a document between two positions, as an array of
  17. // strings.
  18. export function getBetween(doc, start, end) {
  19. let out = [], n = start.line
  20. doc.iter(start.line, end.line + 1, line => {
  21. let text = line.text
  22. if (n == end.line) text = text.slice(0, end.ch)
  23. if (n == start.line) text = text.slice(start.ch)
  24. out.push(text)
  25. ++n
  26. })
  27. return out
  28. }
  29. // Get the lines between from and to, as array of strings.
  30. export function getLines(doc, from, to) {
  31. let out = []
  32. doc.iter(from, to, line => { out.push(line.text) }) // iter aborts when callback returns truthy value
  33. return out
  34. }
  35. // Update the height of a line, propagating the height change
  36. // upwards to parent nodes.
  37. export function updateLineHeight(line, height) {
  38. let diff = height - line.height
  39. if (diff) for (let n = line; n; n = n.parent) n.height += diff
  40. }
  41. // Given a line object, find its line number by walking up through
  42. // its parent links.
  43. export function lineNo(line) {
  44. if (line.parent == null) return null
  45. let cur = line.parent, no = indexOf(cur.lines, line)
  46. for (let chunk = cur.parent; chunk; cur = chunk, chunk = chunk.parent) {
  47. for (let i = 0;; ++i) {
  48. if (chunk.children[i] == cur) break
  49. no += chunk.children[i].chunkSize()
  50. }
  51. }
  52. return no + cur.first
  53. }
  54. // Find the line at the given vertical position, using the height
  55. // information in the document tree.
  56. export function lineAtHeight(chunk, h) {
  57. let n = chunk.first
  58. outer: do {
  59. for (let i = 0; i < chunk.children.length; ++i) {
  60. let child = chunk.children[i], ch = child.height
  61. if (h < ch) { chunk = child; continue outer }
  62. h -= ch
  63. n += child.chunkSize()
  64. }
  65. return n
  66. } while (!chunk.lines)
  67. let i = 0
  68. for (; i < chunk.lines.length; ++i) {
  69. let line = chunk.lines[i], lh = line.height
  70. if (h < lh) break
  71. h -= lh
  72. }
  73. return n + i
  74. }
  75. export function isLine(doc, l) {return l >= doc.first && l < doc.first + doc.size}
  76. export function lineNumberFor(options, i) {
  77. return String(options.lineNumberFormatter(i + options.firstLineNumber))
  78. }