@@ -338,7 +338,7 @@ class OpAssembler {
338338 * @yields {Op} The squashed operations.
339339 * @returns {Generator<Op> }
340340 */
341- const squashOps = function * ( ops , finalize ) {
341+ exports . squashOps = function * ( ops , finalize ) {
342342 let prevOp = new Op ( ) ;
343343 // If we get, for example, insertions [xxx\n,yyy], those don't merge, but if we get
344344 // [xxx\n,yyy,zzz\n], that merges to [xxx\nyyyzzz\n]. This variable stores the length of yyy and
@@ -402,7 +402,7 @@ class MergingOpAssembler {
402402 }
403403
404404 _serialize ( finalize ) {
405- this . _serialized = exports . serializeOps ( squashOps ( this . _ops , finalize ) ) ;
405+ this . _serialized = exports . serializeOps ( exports . squashOps ( this . _ops , finalize ) ) ;
406406 }
407407
408408 append ( op ) {
@@ -465,32 +465,29 @@ const opsFromText = function* (opcode, text, attribs = '', pool = null) {
465465 */
466466class SmartOpAssembler {
467467 constructor ( ) {
468- this . _minusAssem = new MergingOpAssembler ( ) ;
469- this . _plusAssem = new MergingOpAssembler ( ) ;
470- this . _keepAssem = new MergingOpAssembler ( ) ;
471468 this . _assem = exports . stringAssembler ( ) ;
472469 this . clear ( ) ;
473470 }
474471
475472 clear ( ) {
476- this . _minusAssem . clear ( ) ;
477- this . _plusAssem . clear ( ) ;
478- this . _keepAssem . clear ( ) ;
473+ this . _minusAssem = [ ] ;
474+ this . _plusAssem = [ ] ;
475+ this . _keepAssem = [ ] ;
479476 this . _assem . clear ( ) ;
480477 this . _lastOpcode = '' ;
481478 this . _lengthChange = 0 ;
482479 }
483480
484- _flushKeeps ( ) {
485- this . _assem . append ( this . _keepAssem . toString ( ) ) ;
486- this . _keepAssem . clear ( ) ;
481+ _flushKeeps ( finalize ) {
482+ this . _assem . append ( exports . serializeOps ( exports . squashOps ( this . _keepAssem , finalize ) ) ) ;
483+ this . _keepAssem = [ ] ;
487484 }
488485
489486 _flushPlusMinus ( ) {
490- this . _assem . append ( this . _minusAssem . toString ( ) ) ;
491- this . _minusAssem . clear ( ) ;
492- this . _assem . append ( this . _plusAssem . toString ( ) ) ;
493- this . _plusAssem . clear ( ) ;
487+ this . _assem . append ( exports . serializeOps ( exports . squashOps ( this . _minusAssem , false ) ) ) ;
488+ this . _minusAssem = [ ] ;
489+ this . _assem . append ( exports . serializeOps ( exports . squashOps ( this . _plusAssem , false ) ) ) ;
490+ this . _plusAssem = [ ] ;
494491 }
495492
496493 append ( op ) {
@@ -499,21 +496,21 @@ class SmartOpAssembler {
499496
500497 if ( op . opcode === '-' ) {
501498 if ( this . _lastOpcode === '=' ) {
502- this . _flushKeeps ( ) ;
499+ this . _flushKeeps ( false ) ;
503500 }
504- this . _minusAssem . append ( op ) ;
501+ this . _minusAssem . push ( copyOp ( op ) ) ;
505502 this . _lengthChange -= op . chars ;
506503 } else if ( op . opcode === '+' ) {
507504 if ( this . _lastOpcode === '=' ) {
508- this . _flushKeeps ( ) ;
505+ this . _flushKeeps ( false ) ;
509506 }
510- this . _plusAssem . append ( op ) ;
507+ this . _plusAssem . push ( copyOp ( op ) ) ;
511508 this . _lengthChange += op . chars ;
512509 } else if ( op . opcode === '=' ) {
513510 if ( this . _lastOpcode !== '=' ) {
514511 this . _flushPlusMinus ( ) ;
515512 }
516- this . _keepAssem . append ( op ) ;
513+ this . _keepAssem . push ( copyOp ( op ) ) ;
517514 }
518515 this . _lastOpcode = op . opcode ;
519516 }
@@ -537,12 +534,13 @@ class SmartOpAssembler {
537534
538535 toString ( ) {
539536 this . _flushPlusMinus ( ) ;
540- this . _flushKeeps ( ) ;
537+ this . _flushKeeps ( false ) ;
541538 return this . _assem . toString ( ) ;
542539 }
543540
544541 endDocument ( ) {
545- this . _keepAssem . endDocument ( ) ;
542+ this . _flushPlusMinus ( ) ;
543+ this . _flushKeeps ( true ) ;
546544 }
547545
548546 getLengthChange ( ) {
@@ -608,9 +606,14 @@ exports.checkRep = (cs) => {
608606exports . smartOpAssembler = ( ) => new SmartOpAssembler ( ) ;
609607
610608/**
609+ * @deprecated Use `squashOps` with `serializeOps` instead.
611610 * @returns {MergingOpAssembler }
612611 */
613- exports . mergingOpAssembler = ( ) => new MergingOpAssembler ( ) ;
612+ exports . mergingOpAssembler = ( ) => {
613+ padutils . warnWithStack (
614+ 'Changeset.mergingOpAssembler() is deprecated; use Changeset.squashOps() instead' ) ;
615+ return new MergingOpAssembler ( ) ;
616+ } ;
614617
615618/**
616619 * @deprecated Use `serializeOps` instead.
@@ -1322,12 +1325,12 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
13221325 let lineAssem = null ;
13231326
13241327 const outputMutOp = ( op ) => {
1325- if ( ! lineAssem ) lineAssem = new MergingOpAssembler ( ) ;
1326- lineAssem . append ( op ) ;
1328+ if ( ! lineAssem ) lineAssem = [ ] ;
1329+ lineAssem . push ( op ) ;
13271330 if ( op . lines <= 0 ) return ;
13281331 assert ( op . lines === 1 , `Can't have op.lines of ${ op . lines } in attribution lines` ) ;
13291332 // ship it to the mut
1330- mut . insert ( lineAssem . toString ( ) , 1 ) ;
1333+ mut . insert ( exports . serializeOps ( exports . squashOps ( lineAssem , false ) ) , 1 ) ;
13311334 lineAssem = null ;
13321335 } ;
13331336
@@ -1376,23 +1379,22 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
13761379 * @returns {string } joined Attribution lines
13771380 */
13781381exports . joinAttributionLines = ( theAlines ) => {
1379- const assem = new MergingOpAssembler ( ) ;
1380- for ( const aline of theAlines ) {
1381- for ( const op of exports . deserializeOps ( aline ) ) assem . append ( op ) ;
1382- }
1383- return assem . toString ( ) ;
1382+ const ops = ( function * ( ) {
1383+ for ( const aline of theAlines ) yield * exports . deserializeOps ( aline ) ;
1384+ } ) ( ) ;
1385+ return exports . serializeOps ( exports . squashOps ( ops , false ) ) ;
13841386} ;
13851387
13861388exports . splitAttributionLines = ( attrOps , text ) => {
1387- const assem = new MergingOpAssembler ( ) ;
1389+ let ops = [ ] ;
13881390 const lines = [ ] ;
13891391 let pos = 0 ;
13901392
13911393 const appendOp = ( op ) => {
1392- assem . append ( op ) ;
1394+ ops . push ( op ) ;
13931395 if ( op . lines > 0 ) {
1394- lines . push ( assem . toString ( ) ) ;
1395- assem . clear ( ) ;
1396+ lines . push ( exports . serializeOps ( exports . squashOps ( ops , false ) ) ) ;
1397+ ops = [ ] ;
13961398 }
13971399 pos += op . chars ;
13981400 } ;
0 commit comments