scan_op.js 8.8 KB

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