Changeset: Replace `appendATextToAssembler()` with a generator

pull/5298/head
Richard Hansen 2021-10-25 01:16:46 -04:00
parent 9cd59a84af
commit d74dd235a4
5 changed files with 26 additions and 10 deletions

View File

@ -19,6 +19,8 @@
* `eachAttribNumber()`
* `makeAttribsString()`
* `opAttributeValue()`
* `appendATextToAssembler()`: Deprecated in favor of the new `opsFromAText()`
generator function.
# 1.8.15

View File

@ -499,7 +499,7 @@ Pad.prototype.copyPadWithoutHistory = async function (destinationID, force) {
// based on Changeset.makeSplice
const assem = Changeset.smartOpAssembler();
Changeset.appendATextToAssembler(oldAText, assem);
for (const op of Changeset.opsFromAText(oldAText)) assem.append(op);
assem.endDocument();
// although we have instantiated the newPad with '\n', an additional '\n' is

View File

@ -1718,17 +1718,18 @@ exports.copyAText = (atext1, atext2) => {
};
/**
* Append the set of operations from atext to an assembler.
* Convert AText to a series of operations.
*
* @param {AText} atext -
* @param assem - Assembler like SmartOpAssembler TODO add desc
* @param {AText} atext - The AText to convert.
* @yields {Op}
* @returns {Generator<Op>}
*/
exports.appendATextToAssembler = (atext, assem) => {
exports.opsFromAText = function* (atext) {
// intentionally skips last newline char of atext
const iter = exports.opIterator(atext.attribs);
let lastOp = null;
while (iter.hasNext()) {
if (lastOp != null) assem.append(lastOp);
if (lastOp != null) yield lastOp;
lastOp = iter.next();
}
if (lastOp == null) return;
@ -1741,11 +1742,24 @@ exports.appendATextToAssembler = (atext, assem) => {
const lastLineLength = atext.text.length - nextToLastNewlineEnd - 1;
lastOp.lines--;
lastOp.chars -= (lastLineLength + 1);
assem.append(lastOp);
yield copyOp(lastOp);
lastOp.lines = 0;
lastOp.chars = lastLineLength;
}
if (lastOp.chars) assem.append(lastOp);
if (lastOp.chars) yield lastOp;
};
/**
* Append the set of operations from atext to an assembler.
*
* @deprecated Use `opsFromAText` instead.
* @param {AText} atext -
* @param assem - Assembler like SmartOpAssembler TODO add desc
*/
exports.appendATextToAssembler = (atext, assem) => {
padutils.warnWithStack(
'Changeset.appendATextToAssembler() is deprecated; use Changeset.opsFromAText() instead');
for (const op of exports.opsFromAText(atext)) assem.append(op);
};
/**

View File

@ -530,7 +530,7 @@ function Ace2Inner(editorInfo, cssManagers) {
o.chars = lastLineLength;
o.lines = 0;
assem.append(o);
Changeset.appendATextToAssembler(atext, assem);
for (const op of Changeset.opsFromAText(atext)) assem.append(op);
const newLen = oldLen + assem.getLengthChange();
const changeset = Changeset.checkRep(
Changeset.pack(oldLen, newLen, assem.toString(), atext.text.slice(0, -1)));

View File

@ -745,7 +745,7 @@ describe('easysync', function () {
const testAppendATextToAssembler = (testId, atext, correctOps) => {
it(`testAppendATextToAssembler#${testId}`, async function () {
const assem = Changeset.smartOpAssembler();
Changeset.appendATextToAssembler(atext, assem);
for (const op of Changeset.opsFromAText(atext)) assem.append(op);
expect(assem.toString()).to.equal(correctOps);
});
};