scan_op.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // @ts-check
  2. const { containsNonBmpChars } = require('../util')
  3. const {
  4. ApplyError,
  5. InvalidInsertionError,
  6. UnprocessableError,
  7. } = require('../errors')
  8. const ClearTrackingProps = require('../file_data/clear_tracking_props')
  9. const TrackingProps = require('../file_data/tracking_props')
  10. /**
  11. * @import { RawScanOp, RawInsertOp, RawRetainOp, RawRemoveOp, TrackingDirective } from '../types'
  12. *
  13. * @typedef {{ length: number, inputCursor: number, readonly inputLength: number}} LengthApplyContext
  14. */
  15. class ScanOp {
  16. constructor() {
  17. if (this.constructor === ScanOp) {
  18. throw new Error('Cannot instantiate abstract class')
  19. }
  20. }
  21. /**
  22. * Applies an operation to a length
  23. * @param {LengthApplyContext} current
  24. * @returns {LengthApplyContext}
  25. */
  26. applyToLength(current) {
  27. throw new Error('abstract method')
  28. }
  29. /**
  30. * @returns {RawScanOp}
  31. */
  32. toJSON() {
  33. throw new Error('abstract method')
  34. }
  35. /**
  36. * @param {RawScanOp} raw
  37. * @returns {ScanOp}
  38. */
  39. static fromJSON(raw) {
  40. if (isRetain(raw)) {
  41. return RetainOp.fromJSON(raw)
  42. } else if (isInsert(raw)) {
  43. return InsertOp.fromJSON(raw)
  44. } else if (isRemove(raw)) {
  45. return RemoveOp.fromJSON(raw)
  46. }
  47. throw new UnprocessableError(`Invalid ScanOp ${JSON.stringify(raw)}`)
  48. }
  49. /**
  50. * Tests whether two ScanOps are equal
  51. * @param {ScanOp} _other
  52. * @returns {boolean}
  53. */
  54. equals(_other) {
  55. return false
  56. }
  57. /**
  58. * Tests whether two ScanOps can be merged into a single operation
  59. * @param {ScanOp} other
  60. * @returns
  61. */
  62. canMergeWith(other) {
  63. return false
  64. }
  65. /**
  66. * Merge two ScanOps into a single operation
  67. * @param {ScanOp} _other
  68. * @returns {void}
  69. */
  70. mergeWith(_other) {
  71. throw new Error('abstract method')
  72. }
  73. toString() {
  74. 'ScanOp'
  75. }
  76. }
  77. class InsertOp extends ScanOp {
  78. /**
  79. *
  80. * @param {string} insertion
  81. * @param {TrackingProps | undefined} tracking
  82. * @param {string[] | undefined} commentIds
  83. */
  84. constructor(insertion, tracking = undefined, commentIds = undefined) {
  85. super()
  86. if (typeof insertion !== 'string') {
  87. throw new InvalidInsertionError('insertion must be a string')
  88. }
  89. if (containsNonBmpChars(insertion)) {
  90. throw new InvalidInsertionError('insertion contains non-BMP characters')
  91. }
  92. /** @type {string} */
  93. this.insertion = insertion
  94. /** @type {TrackingProps | undefined} */
  95. this.tracking = tracking
  96. /** @type {string[] | undefined} */
  97. this.commentIds = commentIds
  98. }
  99. /**
  100. *
  101. * @param {RawInsertOp} op
  102. * @returns {InsertOp}
  103. */
  104. static fromJSON(op) {
  105. if (typeof op === 'string') {
  106. return new InsertOp(op)
  107. }
  108. // It must be an object with an 'i' property.
  109. if (typeof op.i !== 'string') {
  110. throw new InvalidInsertionError(
  111. 'insert operation must have a string property'
  112. )
  113. }
  114. return new InsertOp(
  115. op.i,
  116. op.tracking && TrackingProps.fromRaw(op.tracking),
  117. op.commentIds
  118. )
  119. }
  120. /**
  121. * @inheritdoc
  122. * @param {LengthApplyContext} current
  123. * @returns {LengthApplyContext}
  124. */
  125. applyToLength(current) {
  126. current.length += this.insertion.length
  127. return current
  128. }
  129. /** @inheritdoc
  130. * @param {ScanOp} other
  131. */
  132. equals(other) {
  133. if (!(other instanceof InsertOp)) {
  134. return false
  135. }
  136. if (this.insertion !== other.insertion) {
  137. return false
  138. }
  139. if (this.tracking) {
  140. if (!this.tracking.equals(other.tracking)) {
  141. return false
  142. }
  143. } else if (other.tracking) {
  144. return false
  145. }
  146. if (this.commentIds) {
  147. return (
  148. this.commentIds.length === other.commentIds?.length &&
  149. this.commentIds.every(id => other.commentIds?.includes(id))
  150. )
  151. }
  152. return !other.commentIds
  153. }
  154. /**
  155. * @param {ScanOp} other
  156. * @return {other is InsertOp}
  157. */
  158. canMergeWith(other) {
  159. if (!(other instanceof InsertOp)) {
  160. return false
  161. }
  162. if (this.tracking) {
  163. if (!this.tracking.equals(other.tracking)) {
  164. return false
  165. }
  166. } else if (other.tracking) {
  167. return false
  168. }
  169. if (this.commentIds) {
  170. return (
  171. this.commentIds.length === other.commentIds?.length &&
  172. this.commentIds.every(id => other.commentIds?.includes(id))
  173. )
  174. }
  175. return !other.commentIds
  176. }
  177. /**
  178. * @param {ScanOp} other
  179. */
  180. mergeWith(other) {
  181. if (!this.canMergeWith(other)) {
  182. throw new Error('Cannot merge with incompatible operation')
  183. }
  184. this.insertion += other.insertion
  185. // We already have the same tracking info and commentIds
  186. }
  187. /**
  188. * @returns {RawInsertOp}
  189. */
  190. toJSON() {
  191. if (!this.tracking && !this.commentIds) {
  192. return this.insertion
  193. }
  194. /** @type RawInsertOp */
  195. const obj = { i: this.insertion }
  196. if (this.tracking) {
  197. obj.tracking = this.tracking.toRaw()
  198. }
  199. if (this.commentIds) {
  200. obj.commentIds = this.commentIds
  201. }
  202. return obj
  203. }
  204. toString() {
  205. return `insert '${this.insertion}'`
  206. }
  207. }
  208. class RetainOp extends ScanOp {
  209. /**
  210. * @param {number} length
  211. * @param {TrackingDirective | undefined} tracking
  212. */
  213. constructor(length, tracking = undefined) {
  214. super()
  215. if (length < 0) {
  216. throw new Error('length must be non-negative')
  217. }
  218. /** @type {number} */
  219. this.length = length
  220. /** @type {TrackingDirective | undefined} */
  221. this.tracking = tracking
  222. }
  223. /**
  224. * @inheritdoc
  225. * @param {LengthApplyContext} current
  226. * @returns {LengthApplyContext}
  227. */
  228. applyToLength(current) {
  229. if (current.inputCursor + this.length > current.inputLength) {
  230. throw new ApplyError(
  231. "Operation can't retain more chars than are left in the string.",
  232. this.toJSON(),
  233. current.inputLength
  234. )
  235. }
  236. current.length += this.length
  237. current.inputCursor += this.length
  238. return current
  239. }
  240. /**
  241. *
  242. * @param {RawRetainOp} op
  243. * @returns {RetainOp}
  244. */
  245. static fromJSON(op) {
  246. if (typeof op === 'number') {
  247. return new RetainOp(op)
  248. }
  249. // It must be an object with a 'r' property.
  250. if (typeof op.r !== 'number') {
  251. throw new Error('retain operation must have a number property')
  252. }
  253. if (op.tracking) {
  254. const tracking =
  255. op.tracking.type === 'none'
  256. ? new ClearTrackingProps()
  257. : TrackingProps.fromRaw(op.tracking)
  258. return new RetainOp(op.r, tracking)
  259. }
  260. return new RetainOp(op.r)
  261. }
  262. /** @inheritdoc
  263. * @param {ScanOp} other
  264. */
  265. equals(other) {
  266. if (!(other instanceof RetainOp)) {
  267. return false
  268. }
  269. if (this.length !== other.length) {
  270. return false
  271. }
  272. if (this.tracking) {
  273. return this.tracking.equals(other.tracking)
  274. }
  275. return !other.tracking
  276. }
  277. /**
  278. * @param {ScanOp} other
  279. * @return {other is RetainOp}
  280. */
  281. canMergeWith(other) {
  282. if (!(other instanceof RetainOp)) {
  283. return false
  284. }
  285. if (this.tracking) {
  286. return this.tracking.equals(other.tracking)
  287. }
  288. return !other.tracking
  289. }
  290. /**
  291. * @param {ScanOp} other
  292. */
  293. mergeWith(other) {
  294. if (!this.canMergeWith(other)) {
  295. throw new Error('Cannot merge with incompatible operation')
  296. }
  297. this.length += other.length
  298. }
  299. /**
  300. * @returns {RawRetainOp}
  301. */
  302. toJSON() {
  303. if (!this.tracking) {
  304. return this.length
  305. }
  306. return { r: this.length, tracking: this.tracking.toRaw() }
  307. }
  308. toString() {
  309. return `retain ${this.length}`
  310. }
  311. }
  312. class RemoveOp extends ScanOp {
  313. /**
  314. * @param {number} length
  315. */
  316. constructor(length) {
  317. super()
  318. if (length < 0) {
  319. throw new Error('length must be non-negative')
  320. }
  321. /** @type {number} */
  322. this.length = length
  323. }
  324. /**
  325. * @inheritdoc
  326. * @param {LengthApplyContext} current
  327. * @returns {LengthApplyContext}
  328. */
  329. applyToLength(current) {
  330. current.inputCursor += this.length
  331. return current
  332. }
  333. /**
  334. *
  335. * @param {RawRemoveOp} op
  336. * @returns {RemoveOp}
  337. */
  338. static fromJSON(op) {
  339. if (typeof op !== 'number' || op > 0) {
  340. throw new Error('delete operation must be a negative number')
  341. }
  342. return new RemoveOp(-op)
  343. }
  344. /**
  345. * @inheritdoc
  346. * @param {ScanOp} other
  347. * @return {boolean}
  348. */
  349. equals(other) {
  350. if (!(other instanceof RemoveOp)) {
  351. return false
  352. }
  353. return this.length === other.length
  354. }
  355. /**
  356. * @param {ScanOp} other
  357. * @return {other is RemoveOp}
  358. */
  359. canMergeWith(other) {
  360. return other instanceof RemoveOp
  361. }
  362. /**
  363. * @param {ScanOp} other
  364. */
  365. mergeWith(other) {
  366. if (!this.canMergeWith(other)) {
  367. throw new Error('Cannot merge with incompatible operation')
  368. }
  369. this.length += other.length
  370. }
  371. /**
  372. * @returns {RawRemoveOp}
  373. */
  374. toJSON() {
  375. return -this.length
  376. }
  377. toString() {
  378. return `remove ${this.length}`
  379. }
  380. }
  381. /**
  382. * @param {RawScanOp} op
  383. * @returns {op is RawRetainOp}
  384. */
  385. function isRetain(op) {
  386. return (
  387. (typeof op === 'number' && op > 0) ||
  388. (typeof op === 'object' &&
  389. 'r' in op &&
  390. typeof op.r === 'number' &&
  391. op.r > 0)
  392. )
  393. }
  394. /**
  395. * @param {RawScanOp} op
  396. * @returns {op is RawInsertOp}
  397. */
  398. function isInsert(op) {
  399. return (
  400. typeof op === 'string' ||
  401. (typeof op === 'object' && 'i' in op && typeof op.i === 'string')
  402. )
  403. }
  404. /**
  405. * @param {RawScanOp} op
  406. * @returns {op is RawRemoveOp}
  407. */
  408. function isRemove(op) {
  409. return typeof op === 'number' && op < 0
  410. }
  411. module.exports = {
  412. ScanOp,
  413. InsertOp,
  414. RetainOp,
  415. RemoveOp,
  416. isRetain,
  417. isInsert,
  418. isRemove,
  419. }