line-tracker.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import OError from '@overleaf/o-error'
  2. import { Text } from '@codemirror/state'
  3. import { Word } from './spellchecker'
  4. import { ViewUpdate } from '@codemirror/view'
  5. export class LineTracker {
  6. private _lines: boolean[]
  7. constructor(doc: Text) {
  8. /*
  9. * Maintain an array of booleans, one for each line of the document.
  10. * `true` means a line has changed
  11. */
  12. this._lines = new Array(doc.lines).fill(true)
  13. }
  14. dump() {
  15. return [...this._lines]
  16. }
  17. count() {
  18. return this._lines.length
  19. }
  20. lineHasChanged(lineNumber: number) {
  21. return this._lines[lineNumber - 1] === true
  22. }
  23. /*
  24. * Given a list of words, clear the 'changed' mark
  25. * on the lines the words are on
  26. */
  27. clearChangedLinesForWords(words: Word[]) {
  28. words.forEach(word => {
  29. this.clearLine(word.lineNumber)
  30. })
  31. }
  32. clearLine(lineNumber: number) {
  33. this._lines[lineNumber - 1] = false
  34. }
  35. clearAllLines() {
  36. this._lines = this._lines.map(() => false)
  37. }
  38. resetAllLines() {
  39. this._lines = this._lines.map(() => true)
  40. }
  41. markLineAsUpdated(lineNumber: number) {
  42. this._lines[lineNumber - 1] = true
  43. }
  44. /*
  45. * On update, for all changes, mark the affected lines
  46. * as changed
  47. */
  48. applyUpdate(update: ViewUpdate) {
  49. for (const transaction of update.transactions) {
  50. if (transaction.docChanged) {
  51. let lineShift = 0
  52. transaction.changes.iterChanges(
  53. (fromA, toA, fromB, toB, insertedText) => {
  54. const insertedLength = insertedText.length
  55. const removedLength = toA - fromA
  56. const hasInserted = insertedLength > 0
  57. const hasRemoved = removedLength > 0
  58. const oldDoc = transaction.startState.doc
  59. if (hasRemoved) {
  60. const startLine = oldDoc.lineAt(fromA).number
  61. const endLine = oldDoc.lineAt(toA).number
  62. /* Mark start line as changed, and remove deleted lines
  63. * Example:
  64. * with this text:
  65. * |1|aaaa|
  66. * |2|bbbb| => [false, false, false, false]
  67. * |3|cccc|
  68. * |4|dddd|
  69. *
  70. * with a selection covering 'bbcccc' across lines 2 and 3,
  71. * press backspace,
  72. * resulting in:
  73. * |1|aaaa|
  74. * |2|bb| => [false, true, false]
  75. * |3|dddd|
  76. */
  77. this._lines.splice(
  78. startLine - 1 + lineShift,
  79. endLine - startLine + 1,
  80. true
  81. )
  82. lineShift -= endLine - startLine
  83. }
  84. if (hasInserted) {
  85. const startLine = oldDoc.lineAt(fromA).number
  86. /* Mark start line as changed, and insert new (changed) lines after.
  87. * Example:
  88. * with this text:
  89. * |1|aaaa|
  90. * |2|bbbb| => [false, false, false]
  91. * |3|cccc|
  92. *
  93. * with the cursor at the end of line 2,
  94. * insert the following text:
  95. * |1|xx|
  96. * |2|yy|
  97. *
  98. * results in:
  99. * |1|aaaa|
  100. * |2|bbbbxx| => [false, true, true, false]
  101. * |3|yy|
  102. * |4|cccc|
  103. */
  104. const changes = new Array(insertedText.lines).fill(true)
  105. this._lines.splice(startLine - 1 + lineShift, 1, ...changes)
  106. lineShift += changes.length - 1
  107. }
  108. }
  109. )
  110. }
  111. if (update.state.doc.lines !== this._lines.length) {
  112. throw new OError(
  113. 'LineTracker length does not match document line count'
  114. ).withInfo({
  115. documentLines: update.state.doc.lines,
  116. trackerLines: this._lines.length,
  117. })
  118. }
  119. }
  120. }
  121. }