Skip to content

Commit c828c8e

Browse files
committed
Changeset.js: refine comments
1 parent 211201f commit c828c8e

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/static/js/Changeset.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ exports.mergingOpAssembler = () => {
421421
// ops immediately after it.
422422
let bufOpAdditionalCharsAfterNewline = 0;
423423

424+
/**
425+
* @param {boolean} [isEndDocument]
426+
*/
424427
const flush = (isEndDocument) => {
425428
if (bufOp.opcode) {
426429
if (isEndDocument && bufOp.opcode === '=' && !bufOp.attribs) {
@@ -727,7 +730,7 @@ exports.textLinesMutator = (lines) => {
727730

728731
/**
729732
* Indicates if curLine is already in the splice. This is necessary because the last element in
730-
* curSplice is curLine when this line is currently worked on (e.g. when skipping are inserting).
733+
* curSplice is curLine when this line is currently worked on (e.g. when skipping or inserting)
731734
*
732735
* TODO(doc) why aren't removals considered?
733736
*
@@ -752,7 +755,7 @@ exports.textLinesMutator = (lines) => {
752755
* It will skip some newlines by putting them into the splice.
753756
*
754757
* @param {number} L -
755-
* @param {boolean} includeInSplice - indicates if attributes are present
758+
* @param {boolean} includeInSplice - indicates that attributes are present
756759
*/
757760
const skipLines = (L, includeInSplice) => {
758761
if (L) {
@@ -898,7 +901,7 @@ exports.textLinesMutator = (lines) => {
898901
/** @type {string} */
899902
const theLine = curSplice[sline];
900903
const lineCol = curCol;
901-
// insert the first new line
904+
// insert the chars up to curCol and the first new line
902905
curSplice[sline] = theLine.substring(0, lineCol) + newLines[0];
903906
curLine++;
904907
newLines.splice(0, 1);
@@ -1299,6 +1302,15 @@ exports.applyToAttribution = (cs, astr, pool) => {
12991302
(op1, op2, opOut) => exports._slicerZipperFunc(op1, op2, opOut, pool));
13001303
};
13011304

1305+
/**
1306+
* Applies a changeset to an array of attribution lines.
1307+
* Lines are changed in-place.
1308+
*
1309+
* @param {string} cs Changeset
1310+
* @param {Array.<string>} lines Attribution lines
1311+
* @pool {AttribPool} pool Pool
1312+
*
1313+
*/
13021314
exports.mutateAttributionLines = (cs, lines, pool) => {
13031315
const unpacked = exports.unpack(cs);
13041316
const csIter = exports.opIterator(unpacked.ops);
@@ -1307,24 +1319,49 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
13071319
// treat the attribution lines as text lines, mutating a line at a time
13081320
const mut = exports.textLinesMutator(lines);
13091321

1310-
/** @type {?OpIter} */
1322+
/**
1323+
* a line in lines array that is currently changed
1324+
*
1325+
* @type {?OpIter}
1326+
*
1327+
*/
13111328
let lineIter = null;
13121329

1330+
/**
1331+
* Returns false if we are on the last attribution line in `lines` and
1332+
* there is no additional op in that line.
1333+
*
1334+
* @returns {boolean} True if there are more ops to go through
1335+
*/
13131336
const isNextMutOp = () => (lineIter && lineIter.hasNext()) || mut.hasMore();
13141337

1338+
/**
1339+
* Puts the next op from lineIter into destOp. Enters a new attribution line in `lines`
1340+
* if lineIter finished.
1341+
*
1342+
* @param {Opcode} destOp
1343+
*/
13151344
const nextMutOp = (destOp) => {
13161345
if ((!(lineIter && lineIter.hasNext())) && mut.hasMore()) {
1346+
// There are more attribution lines in `lines` to do AND
1347+
// either we just started so lineIter is still null or no more ops in current lineIter
13171348
const line = mut.removeLines(1);
13181349
lineIter = exports.opIterator(line);
13191350
}
13201351
if (lineIter && lineIter.hasNext()) {
1352+
// put next op from lineIter into destOp
13211353
lineIter.next(destOp);
13221354
} else {
1355+
// lineIter finished, reset destOp
13231356
destOp.opcode = '';
13241357
}
13251358
};
13261359
let lineAssem = null;
13271360

1361+
/**
1362+
* Appends an op to lineAssem
1363+
* In case lineAssem includes one single newline, add it to `lines` mutator
1364+
*/
13281365
const outputMutOp = (op) => {
13291366
if (!lineAssem) {
13301367
lineAssem = exports.mergingOpAssembler();
@@ -1343,24 +1380,28 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
13431380
const opOut = exports.newOp();
13441381
while (csOp.opcode || csIter.hasNext() || attOp.opcode || isNextMutOp()) {
13451382
if ((!csOp.opcode) && csIter.hasNext()) {
1383+
// more ops in cs and csOp done
13461384
csIter.next(csOp);
13471385
}
13481386
if ((!csOp.opcode) && (!attOp.opcode) && (!lineAssem) && (!(lineIter && lineIter.hasNext()))) {
13491387
break; // done
13501388
} else if (csOp.opcode === '=' && csOp.lines > 0 && (!csOp.attribs) &&
13511389
(!attOp.opcode) && (!lineAssem) && (!(lineIter && lineIter.hasNext()))) {
1352-
// skip multiple lines; this is what makes small changes not order of the document size
1390+
// skip multiple lines without attributes; this is what makes small changes not order of the
1391+
// document size
13531392
mut.skipLines(csOp.lines);
13541393
csOp.opcode = '';
13551394
} else if (csOp.opcode === '+') {
13561395
if (csOp.lines > 1) {
1396+
// copy the first line from csOp to opOut
13571397
const firstLineLen = csBank.indexOf('\n', csBankIndex) + 1 - csBankIndex;
13581398
exports.copyOp(csOp, opOut);
13591399
csOp.chars -= firstLineLen;
13601400
csOp.lines--;
13611401
opOut.lines = 1;
13621402
opOut.chars = firstLineLen;
13631403
} else {
1404+
// either one or no newline in + csOp, copy to opOut and reset csOp
13641405
exports.copyOp(csOp, opOut);
13651406
csOp.opcode = '';
13661407
}
@@ -1806,7 +1847,8 @@ exports.copyAText = (atext1, atext2) => {
18061847
};
18071848

18081849
/**
1809-
* Append the set of operations from atext to an assembler.
1850+
* Append the set of operations from atext to an assembler
1851+
* Strips final newline.
18101852
*
18111853
* @param {AText} atext -
18121854
* @param assem - Assembler like SmartOpAssembler TODO add desc

src/tests/frontend/specs/easysync-assembler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,6 @@ describe('easysync', function () {
227227
testAppendATextToAssembler(8, {
228228
text: '\n\n\nfoo\n',
229229
attribs: '|2+2*x|2+5',
230-
}, '|2+2*x|1+1*x+3'); //TODO: why isn't this |2+2*x|1+4 ?
230+
}, '|2+2*x|1+1*x+3');
231231
});
232232
});

0 commit comments

Comments
 (0)