@@ -421,6 +421,9 @@ exports.mergingOpAssembler = () => {
421
421
// ops immediately after it.
422
422
let bufOpAdditionalCharsAfterNewline = 0 ;
423
423
424
+ /**
425
+ * @param {boolean } [isEndDocument]
426
+ */
424
427
const flush = ( isEndDocument ) => {
425
428
if ( bufOp . opcode ) {
426
429
if ( isEndDocument && bufOp . opcode === '=' && ! bufOp . attribs ) {
@@ -727,7 +730,7 @@ exports.textLinesMutator = (lines) => {
727
730
728
731
/**
729
732
* 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)
731
734
*
732
735
* TODO(doc) why aren't removals considered?
733
736
*
@@ -752,7 +755,7 @@ exports.textLinesMutator = (lines) => {
752
755
* It will skip some newlines by putting them into the splice.
753
756
*
754
757
* @param {number } L -
755
- * @param {boolean } includeInSplice - indicates if attributes are present
758
+ * @param {boolean } includeInSplice - indicates that attributes are present
756
759
*/
757
760
const skipLines = ( L , includeInSplice ) => {
758
761
if ( L ) {
@@ -898,7 +901,7 @@ exports.textLinesMutator = (lines) => {
898
901
/** @type {string } */
899
902
const theLine = curSplice [ sline ] ;
900
903
const lineCol = curCol ;
901
- // insert the first new line
904
+ // insert the chars up to curCol and the first new line
902
905
curSplice [ sline ] = theLine . substring ( 0 , lineCol ) + newLines [ 0 ] ;
903
906
curLine ++ ;
904
907
newLines . splice ( 0 , 1 ) ;
@@ -1299,6 +1302,15 @@ exports.applyToAttribution = (cs, astr, pool) => {
1299
1302
( op1 , op2 , opOut ) => exports . _slicerZipperFunc ( op1 , op2 , opOut , pool ) ) ;
1300
1303
} ;
1301
1304
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
+ */
1302
1314
exports . mutateAttributionLines = ( cs , lines , pool ) => {
1303
1315
const unpacked = exports . unpack ( cs ) ;
1304
1316
const csIter = exports . opIterator ( unpacked . ops ) ;
@@ -1307,24 +1319,49 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
1307
1319
// treat the attribution lines as text lines, mutating a line at a time
1308
1320
const mut = exports . textLinesMutator ( lines ) ;
1309
1321
1310
- /** @type {?OpIter } */
1322
+ /**
1323
+ * a line in lines array that is currently changed
1324
+ *
1325
+ * @type {?OpIter }
1326
+ *
1327
+ */
1311
1328
let lineIter = null ;
1312
1329
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
+ */
1313
1336
const isNextMutOp = ( ) => ( lineIter && lineIter . hasNext ( ) ) || mut . hasMore ( ) ;
1314
1337
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
+ */
1315
1344
const nextMutOp = ( destOp ) => {
1316
1345
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
1317
1348
const line = mut . removeLines ( 1 ) ;
1318
1349
lineIter = exports . opIterator ( line ) ;
1319
1350
}
1320
1351
if ( lineIter && lineIter . hasNext ( ) ) {
1352
+ // put next op from lineIter into destOp
1321
1353
lineIter . next ( destOp ) ;
1322
1354
} else {
1355
+ // lineIter finished, reset destOp
1323
1356
destOp . opcode = '' ;
1324
1357
}
1325
1358
} ;
1326
1359
let lineAssem = null ;
1327
1360
1361
+ /**
1362
+ * Appends an op to lineAssem
1363
+ * In case lineAssem includes one single newline, add it to `lines` mutator
1364
+ */
1328
1365
const outputMutOp = ( op ) => {
1329
1366
if ( ! lineAssem ) {
1330
1367
lineAssem = exports . mergingOpAssembler ( ) ;
@@ -1343,24 +1380,28 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
1343
1380
const opOut = exports . newOp ( ) ;
1344
1381
while ( csOp . opcode || csIter . hasNext ( ) || attOp . opcode || isNextMutOp ( ) ) {
1345
1382
if ( ( ! csOp . opcode ) && csIter . hasNext ( ) ) {
1383
+ // more ops in cs and csOp done
1346
1384
csIter . next ( csOp ) ;
1347
1385
}
1348
1386
if ( ( ! csOp . opcode ) && ( ! attOp . opcode ) && ( ! lineAssem ) && ( ! ( lineIter && lineIter . hasNext ( ) ) ) ) {
1349
1387
break ; // done
1350
1388
} else if ( csOp . opcode === '=' && csOp . lines > 0 && ( ! csOp . attribs ) &&
1351
1389
( ! 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
1353
1392
mut . skipLines ( csOp . lines ) ;
1354
1393
csOp . opcode = '' ;
1355
1394
} else if ( csOp . opcode === '+' ) {
1356
1395
if ( csOp . lines > 1 ) {
1396
+ // copy the first line from csOp to opOut
1357
1397
const firstLineLen = csBank . indexOf ( '\n' , csBankIndex ) + 1 - csBankIndex ;
1358
1398
exports . copyOp ( csOp , opOut ) ;
1359
1399
csOp . chars -= firstLineLen ;
1360
1400
csOp . lines -- ;
1361
1401
opOut . lines = 1 ;
1362
1402
opOut . chars = firstLineLen ;
1363
1403
} else {
1404
+ // either one or no newline in + csOp, copy to opOut and reset csOp
1364
1405
exports . copyOp ( csOp , opOut ) ;
1365
1406
csOp . opcode = '' ;
1366
1407
}
@@ -1806,7 +1847,8 @@ exports.copyAText = (atext1, atext2) => {
1806
1847
} ;
1807
1848
1808
1849
/**
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.
1810
1852
*
1811
1853
* @param {AText } atext -
1812
1854
* @param assem - Assembler like SmartOpAssembler TODO add desc
0 commit comments