Skip to content

Commit 17758e7

Browse files
webzwo0irhansen
authored andcommitted
Changeset.js: refine comments
1 parent 265108a commit 17758e7

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

src/static/js/Changeset.js

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

423+
/**
424+
* @param {boolean} [isEndDocument]
425+
*/
423426
const flush = (isEndDocument) => {
424427
if (!bufOp.opcode) return;
425428
if (isEndDocument && bufOp.opcode === '=' && !bufOp.attribs) {
@@ -712,7 +715,7 @@ const textLinesMutator = (lines) => {
712715

713716
/**
714717
* Indicates if curLine is already in the splice. This is necessary because the last element in
715-
* curSplice is curLine when this line is currently worked on (e.g. when skipping are inserting).
718+
* curSplice is curLine when this line is currently worked on (e.g. when skipping or inserting).
716719
*
717720
* TODO(doc) why aren't removals considered?
718721
*
@@ -737,7 +740,7 @@ const textLinesMutator = (lines) => {
737740
* It will skip some newlines by putting them into the splice.
738741
*
739742
* @param {number} L -
740-
* @param {boolean} includeInSplice - indicates if attributes are present
743+
* @param {boolean} includeInSplice - Indicates that attributes are present.
741744
*/
742745
const skipLines = (L, includeInSplice) => {
743746
if (!L) return;
@@ -866,7 +869,7 @@ const textLinesMutator = (lines) => {
866869
/** @type {string} */
867870
const theLine = curSplice[sline];
868871
const lineCol = curCol;
869-
// insert the first new line
872+
// Insert the chars up to `curCol` and the first new line.
870873
curSplice[sline] = theLine.substring(0, lineCol) + newLines[0];
871874
curLine++;
872875
newLines.splice(0, 1);
@@ -882,9 +885,8 @@ const textLinesMutator = (lines) => {
882885
curLine += newLines.length;
883886
}
884887
} else {
885-
// there are no additional lines
886-
// although the line is put into splice, curLine is not increased, because
887-
// there may be more chars in the line (newline is not reached)
888+
// There are no additional lines. Although the line is put into splice, curLine is not
889+
// increased because there may be more chars in the line (newline is not reached).
888890
const sline = putCurLineInSplice();
889891
if (!curSplice[sline]) {
890892
const err = new Error(
@@ -1228,6 +1230,13 @@ exports.applyToAttribution = (cs, astr, pool) => {
12281230
return applyZip(astr, unpacked.ops, (op1, op2) => slicerZipperFunc(op1, op2, pool));
12291231
};
12301232

1233+
/**
1234+
* Applies a changeset to an array of attribute lines.
1235+
*
1236+
* @param {string} cs - The encoded changeset.
1237+
* @param {Array<string>} lines - Attribute lines. Modified in place.
1238+
* @param {AttributePool} pool - Attribute pool.
1239+
*/
12311240
exports.mutateAttributionLines = (cs, lines, pool) => {
12321241
const unpacked = exports.unpack(cs);
12331242
const csIter = exports.opIterator(unpacked.ops);
@@ -1236,21 +1245,42 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
12361245
// treat the attribution lines as text lines, mutating a line at a time
12371246
const mut = textLinesMutator(lines);
12381247

1239-
/** @type {?OpIter} */
1248+
/**
1249+
* An iterator over the Ops in the current line from `lines`.
1250+
*
1251+
* @type {?OpIter}
1252+
*/
12401253
let lineIter = null;
12411254

1255+
/**
1256+
* Returns false if we are on the last attribute line in `lines` and there is no additional op in
1257+
* that line.
1258+
*
1259+
* @returns {boolean} True if there are more ops to go through.
1260+
*/
12421261
const isNextMutOp = () => (lineIter && lineIter.hasNext()) || mut.hasMore();
12431262

1263+
/**
1264+
* @returns {Op} The next Op from `lineIter`. If there are no more Ops, `lineIter` is reset to
1265+
* iterate over the next line, which is consumed from `mut`. If there are no more lines,
1266+
* returns a null Op.
1267+
*/
12441268
const nextMutOp = () => {
12451269
if ((!(lineIter && lineIter.hasNext())) && mut.hasMore()) {
1270+
// There are more attribute lines in `lines` to do AND either we just started so `lineIter` is
1271+
// still null or there are no more ops in current `lineIter`.
12461272
const line = mut.removeLines(1);
12471273
lineIter = exports.opIterator(line);
12481274
}
1249-
if (!lineIter || !lineIter.hasNext()) return exports.newOp();
1275+
if (!lineIter || !lineIter.hasNext()) return exports.newOp(); // No more ops and no more lines.
12501276
return lineIter.next();
12511277
};
12521278
let lineAssem = null;
12531279

1280+
/**
1281+
* Appends an op to `lineAssem`. In case `lineAssem` includes one single newline, adds it to the
1282+
* `lines` mutator.
1283+
*/
12541284
const outputMutOp = (op) => {
12551285
if (!lineAssem) {
12561286
lineAssem = exports.mergingOpAssembler();
@@ -1266,23 +1296,26 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
12661296
let csOp = exports.newOp();
12671297
let attOp = exports.newOp();
12681298
while (csOp.opcode || csIter.hasNext() || attOp.opcode || isNextMutOp()) {
1269-
if (!csOp.opcode && csIter.hasNext()) csOp = csIter.next();
1299+
if (!csOp.opcode && csIter.hasNext()) csOp = csIter.next(); // coOp done, but more ops in cs.
12701300
if ((!csOp.opcode) && (!attOp.opcode) && (!lineAssem) && (!(lineIter && lineIter.hasNext()))) {
12711301
break; // done
12721302
} else if (csOp.opcode === '=' && csOp.lines > 0 && (!csOp.attribs) &&
12731303
(!attOp.opcode) && (!lineAssem) && (!(lineIter && lineIter.hasNext()))) {
1274-
// skip multiple lines; this is what makes small changes not order of the document size
1304+
// Skip multiple lines without attributes; this is what makes small changes not order of the
1305+
// document size.
12751306
mut.skipLines(csOp.lines);
12761307
csOp.opcode = '';
12771308
} else if (csOp.opcode === '+') {
12781309
const opOut = copyOp(csOp);
12791310
if (csOp.lines > 1) {
1311+
// Copy the first line from `csOp` to `opOut`.
12801312
const firstLineLen = csBank.indexOf('\n', csBankIndex) + 1 - csBankIndex;
12811313
csOp.chars -= firstLineLen;
12821314
csOp.lines--;
12831315
opOut.lines = 1;
12841316
opOut.chars = firstLineLen;
12851317
} else {
1318+
// Either one or no newlines in '+' `csOp`, copy to `opOut` and reset `csOp`.
12861319
csOp.opcode = '';
12871320
}
12881321
outputMutOp(opOut);
@@ -1705,7 +1738,7 @@ exports.copyAText = (atext1, atext2) => {
17051738
};
17061739

17071740
/**
1708-
* Append the set of operations from atext to an assembler.
1741+
* Appends the set of operations from `atext` to an assembler. Strips final newline.
17091742
*
17101743
* @param {AText} atext -
17111744
* @param assem - Assembler like SmartOpAssembler TODO add desc

0 commit comments

Comments
 (0)