Changeset: Improve readability of makeSplice()

This commit is contained in:
Richard Hansen 2021-12-12 19:36:08 -05:00
parent b1d0848701
commit fdf1fdbc23

View file

@ -1482,35 +1482,27 @@ exports.identity = (N) => exports.pack(N, N, '', '');
* spliceStart+numRemoved and inserts newText instead. Also gives possibility to add attributes * spliceStart+numRemoved and inserts newText instead. Also gives possibility to add attributes
* optNewTextAPairs for the new text. * optNewTextAPairs for the new text.
* *
* @param {string} oldFullText - old text * @param {string} orig - Original text.
* @param {number} spliceStart - where splicing starts * @param {number} start - Index into `orig` where characters should be removed and inserted.
* @param {number} numRemoved - number of characters to remove * @param {number} ndel - Number of characters to delete at `start`.
* @param {string} newText - string to insert * @param {string} ins - Text to insert at `start` (after deleting `ndel` characters).
* @param {string} optNewTextAPairs - new pairs to insert * @param {string} [attribs] - Optional attributes to apply to the inserted text.
* @param {AttributePool} pool - Attribute pool * @param {AttributePool} [pool] - Attribute pool.
* @returns {string} * @returns {string}
*/ */
exports.makeSplice = (oldFullText, spliceStart, numRemoved, newText, optNewTextAPairs, pool) => { exports.makeSplice = (orig, start, ndel, ins, attribs, pool) => {
const oldLen = oldFullText.length; if (start >= orig.length) start = orig.length - 1;
if (ndel > orig.length - start) ndel = orig.length - start;
if (spliceStart >= oldLen) { const deleted = orig.substring(start, start + ndel);
spliceStart = oldLen - 1;
}
if (numRemoved > oldFullText.length - spliceStart) {
numRemoved = oldFullText.length - spliceStart;
}
const oldText = oldFullText.substring(spliceStart, spliceStart + numRemoved);
const newLen = oldLen + newText.length - oldText.length;
const assem = exports.smartOpAssembler(); const assem = exports.smartOpAssembler();
const ops = (function* () { const ops = (function* () {
yield* opsFromText('=', oldFullText.substring(0, spliceStart)); yield* opsFromText('=', orig.substring(0, start));
yield* opsFromText('-', oldText); yield* opsFromText('-', deleted);
yield* opsFromText('+', newText, optNewTextAPairs, pool); yield* opsFromText('+', ins, attribs, pool);
})(); })();
for (const op of ops) assem.append(op); for (const op of ops) assem.append(op);
assem.endDocument(); assem.endDocument();
return exports.pack(oldLen, newLen, assem.toString(), newText); return exports.pack(orig.length, orig.length + ins.length - ndel, assem.toString(), ins);
}; };
/** /**