scan_op.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. equals(other) {
  134. if (!(other instanceof InsertOp)) {
  135. return false
  136. }
  137. if (this.insertion !== other.insertion) {
  138. return false
  139. }
  140. if (this.tracking) {
  141. if (!this.tracking.equals(other.tracking)) {
  142. return false
  143. }
  144. } else if (other.tracking) {
  145. return false
  146. }
  147. if (this.commentIds) {
  148. return (
  149. this.commentIds.length === other.commentIds?.length &&
  150. this.commentIds.every(id => other.commentIds?.includes(id))
  151. )
  152. }
  153. return !other.commentIds
  154. }
  155. canMergeWith(other) {
  156. if (!(other instanceof InsertOp)) {
  157. return false
  158. }
  159. if (this.tracking) {
  160. if (!this.tracking.equals(other.tracking)) {
  161. return false
  162. }
  163. } else if (other.tracking) {
  164. return false
  165. }
  166. if (this.commentIds) {
  167. return (
  168. this.commentIds.length === other.commentIds?.length &&
  169. this.commentIds.every(id => other.commentIds?.includes(id))
  170. )
  171. }
  172. return !other.commentIds
  173. }
  174. mergeWith(other) {
  175. if (!this.canMergeWith(other)) {
  176. throw new Error('Cannot merge with incompatible operation')
  177. }
  178. this.insertion += other.insertion
  179. // We already have the same tracking info and commentIds
  180. }
  181. /**
  182. * @returns {RawInsertOp}
  183. */
  184. toJSON() {
  185. if (!this.tracking && !this.commentIds) {
  186. return this.insertion
  187. }
  188. const obj = { i: this.insertion }
  189. if (this.tracking) {
  190. obj.tracking = this.tracking.toRaw()
  191. }
  192. if (this.commentIds) {
  193. obj.commentIds = this.commentIds
  194. }
  195. return obj
  196. }
  197. toString() {
  198. return `insert '${this.insertion}'`
  199. }
  200. }
  201. class RetainOp extends ScanOp {
  202. /**
  203. * @param {number} length
  204. * @param {TrackingDirective | undefined} tracking
  205. */
  206. constructor(length, tracking = undefined) {
  207. super()
  208. if (length < 0) {
  209. throw new Error('length must be non-negative')
  210. }
  211. /** @type {number} */
  212. this.length = length
  213. /** @type {TrackingDirective | undefined} */
  214. this.tracking = tracking
  215. }
  216. /**
  217. * @inheritdoc
  218. * @param {LengthApplyContext} current
  219. * @returns {LengthApplyContext}
  220. */
  221. applyToLength(current) {
  222. if (current.inputCursor + this.length > current.inputLength) {
  223. throw new ApplyError(
  224. "Operation can't retain more chars than are left in the string.",
  225. this.toJSON(),
  226. current.inputLength
  227. )
  228. }
  229. current.length += this.length
  230. current.inputCursor += this.length
  231. return current
  232. }
  233. /**
  234. *
  235. * @param {RawRetainOp} op
  236. * @returns {RetainOp}
  237. */
  238. static fromJSON(op) {
  239. if (typeof op === 'number') {
  240. return new RetainOp(op)
  241. }
  242. // It must be an object with a 'r' property.
  243. if (typeof op.r !== 'number') {
  244. throw new Error('retain operation must have a number property')
  245. }
  246. if (op.tracking) {
  247. const tracking =
  248. op.tracking.type === 'none'
  249. ? new ClearTrackingProps()
  250. : TrackingProps.fromRaw(op.tracking)
  251. return new RetainOp(op.r, tracking)
  252. }
  253. return new RetainOp(op.r)
  254. }
  255. /** @inheritdoc */
  256. equals(other) {
  257. if (!(other instanceof RetainOp)) {
  258. return false
  259. }
  260. if (this.length !== other.length) {
  261. return false
  262. }
  263. if (this.tracking) {
  264. return this.tracking.equals(other.tracking)
  265. }
  266. return !other.tracking
  267. }
  268. canMergeWith(other) {
  269. if (!(other instanceof RetainOp)) {
  270. return false
  271. }
  272. if (this.tracking) {
  273. return this.tracking.equals(other.tracking)
  274. }
  275. return !other.tracking
  276. }
  277. mergeWith(other) {
  278. if (!this.canMergeWith(other)) {
  279. throw new Error('Cannot merge with incompatible operation')
  280. }
  281. this.length += other.length
  282. }
  283. /**
  284. * @returns {RawRetainOp}
  285. */
  286. toJSON() {
  287. if (!this.tracking) {
  288. return this.length
  289. }
  290. return { r: this.length, tracking: this.tracking.toRaw() }
  291. }
  292. toString() {
  293. return `retain ${this.length}`
  294. }
  295. }
  296. class RemoveOp extends ScanOp {
  297. constructor(length) {
  298. super()
  299. if (length < 0) {
  300. throw new Error('length must be non-negative')
  301. }
  302. /** @type {number} */
  303. this.length = length
  304. }
  305. /**
  306. * @inheritdoc
  307. * @param {LengthApplyContext} current
  308. * @returns {LengthApplyContext}
  309. */
  310. applyToLength(current) {
  311. current.inputCursor += this.length
  312. return current
  313. }
  314. /**
  315. *
  316. * @param {RawRemoveOp} op
  317. * @returns {RemoveOp}
  318. */
  319. static fromJSON(op) {
  320. if (typeof op !== 'number' || op > 0) {
  321. throw new Error('delete operation must be a negative number')
  322. }
  323. return new RemoveOp(-op)
  324. }
  325. /** @inheritdoc */
  326. equals(other) {
  327. if (!(other instanceof RemoveOp)) {
  328. return false
  329. }
  330. return this.length === other.length
  331. }
  332. canMergeWith(other) {
  333. return other instanceof RemoveOp
  334. }
  335. mergeWith(other) {
  336. if (!this.canMergeWith(other)) {
  337. throw new Error('Cannot merge with incompatible operation')
  338. }
  339. this.length += other.length
  340. }
  341. /**
  342. * @returns {RawRemoveOp}
  343. */
  344. toJSON() {
  345. return -this.length
  346. }
  347. toString() {
  348. return `remove ${this.length}`
  349. }
  350. }
  351. /**
  352. * @param {RawScanOp} op
  353. * @returns {op is RawRetainOp}
  354. */
  355. function isRetain(op) {
  356. return (
  357. (typeof op === 'number' && op > 0) ||
  358. (typeof op === 'object' &&
  359. 'r' in op &&
  360. typeof op.r === 'number' &&
  361. op.r > 0)
  362. )
  363. }
  364. /**
  365. * @param {RawScanOp} op
  366. * @returns {op is RawInsertOp}
  367. */
  368. function isInsert(op) {
  369. return (
  370. typeof op === 'string' ||
  371. (typeof op === 'object' && 'i' in op && typeof op.i === 'string')
  372. )
  373. }
  374. /**
  375. * @param {RawScanOp} op
  376. * @returns {op is RawRemoveOp}
  377. */
  378. function isRemove(op) {
  379. return typeof op === 'number' && op < 0
  380. }
  381. module.exports = {
  382. ScanOp,
  383. InsertOp,
  384. RetainOp,
  385. RemoveOp,
  386. isRetain,
  387. isInsert,
  388. isRemove,
  389. }