This commit is contained in:
+78
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* @import {Event} from 'micromark-util-types'
|
||||
*/
|
||||
/**
|
||||
* @typedef {[number, number, Array<Event>]} Change
|
||||
* @typedef {[number, number, number]} Jump
|
||||
*/
|
||||
/**
|
||||
* Tracks a bunch of edits.
|
||||
*
|
||||
* Port of `edit_map.rs` from `markdown-rs`:
|
||||
* <https://github.com/wooorm/markdown-rs/blob/1506572f/src/util/edit_map.rs>.
|
||||
*
|
||||
* Deal with several changes in events, batching them together.
|
||||
*
|
||||
* Preferably, changes should be kept to a minimum.
|
||||
* Sometimes, it’s needed to change the list of events, because parsing can be
|
||||
* messy, and it helps to expose a cleaner interface of events to the compiler
|
||||
* and other users.
|
||||
* It can also help to merge many adjacent similar events.
|
||||
* And, in other cases, it’s needed to parse subcontent: pass some events
|
||||
* through another tokenizer and inject the result.
|
||||
*/
|
||||
export class EditMap {
|
||||
/**
|
||||
* Changes by index, so `add` does not need to scan `map` (which is
|
||||
* quadratic for documents with many edits at different places).
|
||||
*
|
||||
* @type {Map<number, Change>}
|
||||
*/
|
||||
index: Map<number, Change>;
|
||||
/**
|
||||
* Record of changes.
|
||||
*
|
||||
* @type {Array<Change>}
|
||||
*/
|
||||
map: Array<Change>;
|
||||
/**
|
||||
* Create an edit: a remove and/or add at a certain place.
|
||||
*
|
||||
* @param {number} index
|
||||
* Index at which to apply the edit.
|
||||
* @param {number} remove
|
||||
* Count of items to remove at the index.
|
||||
* @param {Array<Event>} add
|
||||
* Items to add at the index.
|
||||
* @returns {undefined}
|
||||
* Nothing.
|
||||
*/
|
||||
add(index: number, remove: number, add: Array<Event>): undefined;
|
||||
/**
|
||||
* Create an edit: but insert `add` before existing additions, instead of
|
||||
* after them.
|
||||
*
|
||||
* @param {number} index
|
||||
* Index at which to apply the edit.
|
||||
* @param {number} remove
|
||||
* Count of items to remove at the index.
|
||||
* @param {Array<Event>} add
|
||||
* Items to add at the index.
|
||||
* @returns {undefined}
|
||||
* Nothing.
|
||||
*/
|
||||
addBefore(index: number, remove: number, add: Array<Event>): undefined;
|
||||
/**
|
||||
* Done, change the events.
|
||||
*
|
||||
* @param {Array<Event>} events
|
||||
* List of events to apply the edits to.
|
||||
* @returns {undefined}
|
||||
* Nothing.
|
||||
*/
|
||||
consume(events: Array<Event>): undefined;
|
||||
}
|
||||
export type Change = [number, number, Array<Event>];
|
||||
export type Jump = [number, number, number];
|
||||
import type { Event } from 'micromark-util-types';
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;GAGG;AAEH;;;;;;;;;;;;;;;GAeG;AACH;IAQI;;;;;OAKG;IACH,OAFU,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAEP;IAEtB;;;;OAIG;IACH,KAFU,KAAK,CAAC,MAAM,CAAC,CAEV;IAGf;;;;;;;;;;;OAWG;IACH,WATW,MAAM,UAEN,MAAM,OAEN,KAAK,CAAC,KAAK,CAAC,GAEV,SAAS,CAKrB;IAED;;;;;;;;;;;;OAYG;IACH,iBATW,MAAM,UAEN,MAAM,OAEN,KAAK,CAAC,KAAK,CAAC,GAEV,SAAS,CAKrB;IAED;;;;;;;OAOG;IACH,gBALW,KAAK,CAAC,KAAK,CAAC,GAEV,SAAS,CA4DrB;CACF;qBA/IY,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;mBAC9B,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC;2BALb,sBAAsB"}
|
||||
+237
@@ -0,0 +1,237 @@
|
||||
/**
|
||||
* @import {Event} from 'micromark-util-types'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {[number, number, Array<Event>]} Change
|
||||
* @typedef {[number, number, number]} Jump
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tracks a bunch of edits.
|
||||
*
|
||||
* Port of `edit_map.rs` from `markdown-rs`:
|
||||
* <https://github.com/wooorm/markdown-rs/blob/1506572f/src/util/edit_map.rs>.
|
||||
*
|
||||
* Deal with several changes in events, batching them together.
|
||||
*
|
||||
* Preferably, changes should be kept to a minimum.
|
||||
* Sometimes, it’s needed to change the list of events, because parsing can be
|
||||
* messy, and it helps to expose a cleaner interface of events to the compiler
|
||||
* and other users.
|
||||
* It can also help to merge many adjacent similar events.
|
||||
* And, in other cases, it’s needed to parse subcontent: pass some events
|
||||
* through another tokenizer and inject the result.
|
||||
*/
|
||||
export class EditMap {
|
||||
/**
|
||||
* Create a new edit map.
|
||||
*
|
||||
* @returns
|
||||
* New instance.
|
||||
*/
|
||||
constructor() {
|
||||
/**
|
||||
* Changes by index, so `add` does not need to scan `map` (which is
|
||||
* quadratic for documents with many edits at different places).
|
||||
*
|
||||
* @type {Map<number, Change>}
|
||||
*/
|
||||
this.index = new Map()
|
||||
|
||||
/**
|
||||
* Record of changes.
|
||||
*
|
||||
* @type {Array<Change>}
|
||||
*/
|
||||
this.map = []
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an edit: a remove and/or add at a certain place.
|
||||
*
|
||||
* @param {number} index
|
||||
* Index at which to apply the edit.
|
||||
* @param {number} remove
|
||||
* Count of items to remove at the index.
|
||||
* @param {Array<Event>} add
|
||||
* Items to add at the index.
|
||||
* @returns {undefined}
|
||||
* Nothing.
|
||||
*/
|
||||
add(index, remove, add) {
|
||||
addImplementation(this, index, remove, add, false)
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an edit: but insert `add` before existing additions, instead of
|
||||
* after them.
|
||||
*
|
||||
* @param {number} index
|
||||
* Index at which to apply the edit.
|
||||
* @param {number} remove
|
||||
* Count of items to remove at the index.
|
||||
* @param {Array<Event>} add
|
||||
* Items to add at the index.
|
||||
* @returns {undefined}
|
||||
* Nothing.
|
||||
*/
|
||||
addBefore(index, remove, add) {
|
||||
addImplementation(this, index, remove, add, true)
|
||||
}
|
||||
|
||||
/**
|
||||
* Done, change the events.
|
||||
*
|
||||
* @param {Array<Event>} events
|
||||
* List of events to apply the edits to.
|
||||
* @returns {undefined}
|
||||
* Nothing.
|
||||
*/
|
||||
consume(events) {
|
||||
this.map.sort(function (a, b) {
|
||||
return a[0] - b[0]
|
||||
})
|
||||
|
||||
if (this.map.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
// To do: if links are added in events, like they are in `markdown-rs`,
|
||||
// this is needed.
|
||||
// // Calculate jumps: where items in the current list move to.
|
||||
// /** @type {Array<Jump>} */
|
||||
// const jumps = []
|
||||
// let index = 0
|
||||
// let addAcc = 0
|
||||
// let removeAcc = 0
|
||||
// while (index < this.map.length) {
|
||||
// const [at, remove, add] = this.map[index]
|
||||
// removeAcc += remove
|
||||
// addAcc += add.length
|
||||
// jumps.push([at, removeAcc, addAcc])
|
||||
// index += 1
|
||||
// }
|
||||
//
|
||||
// shiftLinks(events, jumps)
|
||||
|
||||
let index = this.map.length
|
||||
/** @type {Array<Array<Event>>} */
|
||||
const vecs = []
|
||||
while (index > 0) {
|
||||
index -= 1
|
||||
vecs.push(
|
||||
events.slice(this.map[index][0] + this.map[index][1]),
|
||||
this.map[index][2]
|
||||
)
|
||||
|
||||
// Truncate rest.
|
||||
events.length = this.map[index][0]
|
||||
}
|
||||
|
||||
vecs.push(events.slice())
|
||||
events.length = 0
|
||||
|
||||
let slice = vecs.pop()
|
||||
|
||||
while (slice) {
|
||||
for (const element of slice) {
|
||||
events.push(element)
|
||||
}
|
||||
|
||||
slice = vecs.pop()
|
||||
}
|
||||
|
||||
// Truncate everything.
|
||||
this.map.length = 0
|
||||
this.index.clear()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an edit.
|
||||
*
|
||||
* @param {EditMap} editMap
|
||||
* Edit map to apply to.
|
||||
* @param {number} at
|
||||
* Index at which to apply the edit.
|
||||
* @param {number} remove
|
||||
* Count of items to remove at the index.
|
||||
* @param {Array<Event>} add
|
||||
* Items to add at the index.
|
||||
* @param {boolean} before
|
||||
* Insert `add` before existing additions at `at`, instead of after them.
|
||||
* @returns {undefined}
|
||||
* Nothing.
|
||||
*/
|
||||
function addImplementation(editMap, at, remove, add, before) {
|
||||
if (remove === 0 && add.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const existing = editMap.index.get(at)
|
||||
|
||||
if (existing) {
|
||||
existing[1] += remove
|
||||
|
||||
if (before) {
|
||||
add.push(...existing[2])
|
||||
existing[2] = add
|
||||
} else {
|
||||
existing[2].push(...add)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/** @type {Change} */
|
||||
const change = [at, remove, add]
|
||||
editMap.map.push(change)
|
||||
editMap.index.set(at, change)
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Shift `previous` and `next` links according to `jumps`.
|
||||
// *
|
||||
// * This fixes links in case there are events removed or added between them.
|
||||
// *
|
||||
// * @param {Array<Event>} events
|
||||
// * @param {Array<Jump>} jumps
|
||||
// */
|
||||
// function shiftLinks(events, jumps) {
|
||||
// let jumpIndex = 0
|
||||
// let index = 0
|
||||
// let add = 0
|
||||
// let rm = 0
|
||||
|
||||
// while (index < events.length) {
|
||||
// const rmCurr = rm
|
||||
|
||||
// while (jumpIndex < jumps.length && jumps[jumpIndex][0] <= index) {
|
||||
// add = jumps[jumpIndex][2]
|
||||
// rm = jumps[jumpIndex][1]
|
||||
// jumpIndex += 1
|
||||
// }
|
||||
|
||||
// // Ignore items that will be removed.
|
||||
// if (rm > rmCurr) {
|
||||
// index += rm - rmCurr
|
||||
// } else {
|
||||
// // ?
|
||||
// // if let Some(link) = &events[index].link {
|
||||
// // if let Some(next) = link.next {
|
||||
// // events[next].link.as_mut().unwrap().previous = Some(index + add - rm);
|
||||
// // while jumpIndex < jumps.len() && jumps[jumpIndex].0 <= next {
|
||||
// // add = jumps[jumpIndex].2;
|
||||
// // rm = jumps[jumpIndex].1;
|
||||
// // jumpIndex += 1;
|
||||
// // }
|
||||
// // events[index].link.as_mut().unwrap().next = Some(next + add - rm);
|
||||
// // index = next;
|
||||
// // continue;
|
||||
// // }
|
||||
// // }
|
||||
// index += 1
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user