Skip to content

Commit e95f130

Browse files
webzwo0irhansen
authored andcommitted
Changeset.js: refine comments
1 parent 13a844c commit e95f130

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
@@ -424,6 +424,9 @@ exports.mergingOpAssembler = () => {
424424
// ops immediately after it.
425425
let bufOpAdditionalCharsAfterNewline = 0;
426426

427+
/**
428+
* @param {boolean} [isEndDocument]
429+
*/
427430
const flush = (isEndDocument) => {
428431
if (bufOp.opcode) {
429432
if (isEndDocument && bufOp.opcode === '=' && !bufOp.attribs) {
@@ -736,7 +739,7 @@ exports.textLinesMutator = (lines) => {
736739

737740
/**
738741
* Indicates if curLine is already in the splice. This is necessary because the last element in
739-
* curSplice is curLine when this line is currently worked on (e.g. when skipping are inserting).
742+
* curSplice is curLine when this line is currently worked on (e.g. when skipping or inserting)
740743
*
741744
* TODO(doc) why aren't removals considered?
742745
*
@@ -761,7 +764,7 @@ exports.textLinesMutator = (lines) => {
761764
* It will skip some newlines by putting them into the splice.
762765
*
763766
* @param {number} L -
764-
* @param {boolean} includeInSplice - indicates if attributes are present
767+
* @param {boolean} includeInSplice - indicates that attributes are present
765768
*/
766769
const skipLines = (L, includeInSplice) => {
767770
if (L) {
@@ -907,7 +910,7 @@ exports.textLinesMutator = (lines) => {
907910
/** @type {string} */
908911
const theLine = curSplice[sline];
909912
const lineCol = curCol;
910-
// insert the first new line
913+
// insert the chars up to curCol and the first new line
911914
curSplice[sline] = theLine.substring(0, lineCol) + newLines[0];
912915
curLine++;
913916
newLines.splice(0, 1);
@@ -1306,6 +1309,15 @@ exports.applyToAttribution = (cs, astr, pool) => {
13061309
(op1, op2, opOut) => exports._slicerZipperFunc(op1, op2, opOut, pool));
13071310
};
13081311

1312+
/**
1313+
* Applies a changeset to an array of attribution lines.
1314+
* Lines are changed in-place.
1315+
*
1316+
* @param {string} cs Changeset
1317+
* @param {Array.<string>} lines Attribution lines
1318+
* @pool {AttribPool} pool Pool
1319+
*
1320+
*/
13091321
exports.mutateAttributionLines = (cs, lines, pool) => {
13101322
const unpacked = exports.unpack(cs);
13111323
const csIter = exports.opIterator(unpacked.ops);
@@ -1314,24 +1326,49 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
13141326
// treat the attribution lines as text lines, mutating a line at a time
13151327
const mut = exports.textLinesMutator(lines);
13161328

1317-
/** @type {?OpIter} */
1329+
/**
1330+
* a line in lines array that is currently changed
1331+
*
1332+
* @type {?OpIter}
1333+
*
1334+
*/
13181335
let lineIter = null;
13191336

1337+
/**
1338+
* Returns false if we are on the last attribution line in `lines` and
1339+
* there is no additional op in that line.
1340+
*
1341+
* @returns {boolean} True if there are more ops to go through
1342+
*/
13201343
const isNextMutOp = () => (lineIter && lineIter.hasNext()) || mut.hasMore();
13211344

1345+
/**
1346+
* Puts the next op from lineIter into destOp. Enters a new attribution line in `lines`
1347+
* if lineIter finished.
1348+
*
1349+
* @param {Opcode} destOp
1350+
*/
13221351
const nextMutOp = (destOp) => {
13231352
if ((!(lineIter && lineIter.hasNext())) && mut.hasMore()) {
1353+
// There are more attribution lines in `lines` to do AND
1354+
// either we just started so lineIter is still null or no more ops in current lineIter
13241355
const line = mut.removeLines(1);
13251356
lineIter = exports.opIterator(line);
13261357
}
13271358
if (lineIter && lineIter.hasNext()) {
1359+
// put next op from lineIter into destOp
13281360
lineIter.next(destOp);
13291361
} else {
1362+
// lineIter finished, reset destOp
13301363
destOp.opcode = '';
13311364
}
13321365
};
13331366
let lineAssem = null;
13341367

1368+
/**
1369+
* Appends an op to lineAssem
1370+
* In case lineAssem includes one single newline, add it to `lines` mutator
1371+
*/
13351372
const outputMutOp = (op) => {
13361373
if (!lineAssem) {
13371374
lineAssem = exports.mergingOpAssembler();
@@ -1350,24 +1387,28 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
13501387
const opOut = exports.newOp();
13511388
while (csOp.opcode || csIter.hasNext() || attOp.opcode || isNextMutOp()) {
13521389
if ((!csOp.opcode) && csIter.hasNext()) {
1390+
// more ops in cs and csOp done
13531391
csIter.next(csOp);
13541392
}
13551393
if ((!csOp.opcode) && (!attOp.opcode) && (!lineAssem) && (!(lineIter && lineIter.hasNext()))) {
13561394
break; // done
13571395
} else if (csOp.opcode === '=' && csOp.lines > 0 && (!csOp.attribs) &&
13581396
(!attOp.opcode) && (!lineAssem) && (!(lineIter && lineIter.hasNext()))) {
1359-
// skip multiple lines; this is what makes small changes not order of the document size
1397+
// skip multiple lines without attributes; this is what makes small changes not order of the
1398+
// document size
13601399
mut.skipLines(csOp.lines);
13611400
csOp.opcode = '';
13621401
} else if (csOp.opcode === '+') {
13631402
if (csOp.lines > 1) {
1403+
// copy the first line from csOp to opOut
13641404
const firstLineLen = csBank.indexOf('\n', csBankIndex) + 1 - csBankIndex;
13651405
exports.copyOp(csOp, opOut);
13661406
csOp.chars -= firstLineLen;
13671407
csOp.lines--;
13681408
opOut.lines = 1;
13691409
opOut.chars = firstLineLen;
13701410
} else {
1411+
// either one or no newline in + csOp, copy to opOut and reset csOp
13711412
exports.copyOp(csOp, opOut);
13721413
csOp.opcode = '';
13731414
}
@@ -1813,7 +1854,8 @@ exports.copyAText = (atext1, atext2) => {
18131854
};
18141855

18151856
/**
1816-
* Append the set of operations from atext to an assembler.
1857+
* Append the set of operations from atext to an assembler
1858+
* Strips final newline.
18171859
*
18181860
* @param {AText} atext -
18191861
* @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
@@ -226,6 +226,6 @@ describe('easysync', function () {
226226
testAppendATextToAssembler(8, {
227227
text: '\n\n\nfoo\n',
228228
attribs: '|2+2*x|2+5',
229-
}, '|2+2*x|1+1*x+3'); //TODO: why isn't this |2+2*x|1+4 ?
229+
}, '|2+2*x|1+1*x+3');
230230
});
231231
});

0 commit comments

Comments
 (0)