scan_op.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 (!other.tracking || !this.tracking.canMergeWith(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. if (this.tracking != null && other.tracking != null) {
  186. this.tracking = this.tracking.mergeWith(other.tracking)
  187. }
  188. // We already have the same 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. if (!other.tracking || !this.tracking.canMergeWith(other.tracking)) {
  290. return false
  291. }
  292. } else if (other.tracking) {
  293. return false
  294. }
  295. return true
  296. }
  297. /**
  298. * @param {ScanOp} other
  299. */
  300. mergeWith(other) {
  301. if (!this.canMergeWith(other)) {
  302. throw new Error('Cannot merge with incompatible operation')
  303. }
  304. this.length += other.length
  305. if (this.tracking != null && other.tracking != null) {
  306. this.tracking = this.tracking.mergeWith(other.tracking)
  307. }
  308. }
  309. /**
  310. * @returns {RawRetainOp}
  311. */
  312. toJSON() {
  313. if (!this.tracking) {
  314. return this.length
  315. }
  316. return { r: this.length, tracking: this.tracking.toRaw() }
  317. }
  318. toString() {
  319. return `retain ${this.length}`
  320. }
  321. }
  322. class RemoveOp extends ScanOp {
  323. /**
  324. * @param {number} length
  325. */
  326. constructor(length) {
  327. super()
  328. if (length < 0) {
  329. throw new Error('length must be non-negative')
  330. }
  331. /** @type {number} */
  332. this.length = length
  333. }
  334. /**
  335. * @inheritdoc
  336. * @param {LengthApplyContext} current
  337. * @returns {LengthApplyContext}
  338. */
  339. applyToLength(current) {
  340. current.inputCursor += this.length
  341. return current
  342. }
  343. /**
  344. *
  345. * @param {RawRemoveOp} op
  346. * @returns {RemoveOp}
  347. */
  348. static fromJSON(op) {
  349. if (typeof op !== 'number' || op > 0) {
  350. throw new Error('delete operation must be a negative number')
  351. }
  352. return new RemoveOp(-op)
  353. }
  354. /**
  355. * @inheritdoc
  356. * @param {ScanOp} other
  357. * @return {boolean}
  358. */
  359. equals(other) {
  360. if (!(other instanceof RemoveOp)) {
  361. return false
  362. }
  363. return this.length === other.length
  364. }
  365. /**
  366. * @param {ScanOp} other
  367. * @return {other is RemoveOp}
  368. */
  369. canMergeWith(other) {
  370. return other instanceof RemoveOp
  371. }
  372. /**
  373. * @param {ScanOp} other
  374. */
  375. mergeWith(other) {
  376. if (!this.canMergeWith(other)) {
  377. throw new Error('Cannot merge with incompatible operation')
  378. }
  379. this.length += other.length
  380. }
  381. /**
  382. * @returns {RawRemoveOp}
  383. */
  384. toJSON() {
  385. return -this.length
  386. }
  387. toString() {
  388. return `remove ${this.length}`
  389. }
  390. }
  391. /**
  392. * @param {RawScanOp} op
  393. * @returns {op is RawRetainOp}
  394. */
  395. function isRetain(op) {
  396. return (
  397. (typeof op === 'number' && op > 0) ||
  398. (typeof op === 'object' &&
  399. 'r' in op &&
  400. typeof op.r === 'number' &&
  401. op.r > 0)
  402. )
  403. }
  404. /**
  405. * @param {RawScanOp} op
  406. * @returns {op is RawInsertOp}
  407. */
  408. function isInsert(op) {
  409. return (
  410. typeof op === 'string' ||
  411. (typeof op === 'object' && 'i' in op && typeof op.i === 'string')
  412. )
  413. }
  414. /**
  415. * @param {RawScanOp} op
  416. * @returns {op is RawRemoveOp}
  417. */
  418. function isRemove(op) {
  419. return typeof op === 'number' && op < 0
  420. }
  421. module.exports = {
  422. ScanOp,
  423. InsertOp,
  424. RetainOp,
  425. RemoveOp,
  426. isRetain,
  427. isInsert,
  428. isRemove,
  429. }