scan_op.js 9.6 KB

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