@@ -496,6 +496,35 @@ const canonicalizeOps = function* (ops, finalize) {
496496 return lengthChange ;
497497} ;
498498
499+ /**
500+ * Generates operations from the given text and attributes.
501+ *
502+ * @param {('-'|'+'|'=') } opcode - The operator to use.
503+ * @param {string } text - The text to remove/add/keep.
504+ * @param {(string|string[][]) } attribs - The attributes to apply to the operations. See
505+ * `makeAttribsString`.
506+ * @param {AttributePool } pool - See `makeAttribsString`.
507+ * @yields {Op} One or two ops (depending on the presense of newlines) that cover the given text.
508+ */
509+ const opsFromText = function * ( opcode , text , attribs , pool ) {
510+ const op = new exports . Op ( opcode ) ;
511+ op . attribs = exports . makeAttribsString ( opcode , attribs , pool ) ;
512+ const lastNewlinePos = text . lastIndexOf ( '\n' ) ;
513+ if ( lastNewlinePos < 0 ) {
514+ op . chars = text . length ;
515+ op . lines = 0 ;
516+ yield op ;
517+ } else {
518+ op . chars = lastNewlinePos + 1 ;
519+ op . lines = text . match ( / \n / g) . length ;
520+ yield op ;
521+ const op2 = copyOp ( op ) ;
522+ op2 . chars = text . length - ( lastNewlinePos + 1 ) ;
523+ op2 . lines = 0 ;
524+ yield op2 ;
525+ }
526+ } ;
527+
499528/**
500529 * Creates an object that allows you to append operations (type Op) and also compresses them if
501530 * possible. Like MergingOpAssembler, but able to produce conforming exportss from slightly looser
@@ -522,22 +551,13 @@ class SmartOpAssembler {
522551 this . _ops . push ( copyOp ( op ) ) ;
523552 }
524553
554+ /**
555+ * @deprecated Use `opsFromText` instead.
556+ */
525557 appendOpWithText ( opcode , text , attribs , pool ) {
526- const op = new exports . Op ( opcode ) ;
527- op . attribs = exports . makeAttribsString ( opcode , attribs , pool ) ;
528- const lastNewlinePos = text . lastIndexOf ( '\n' ) ;
529- if ( lastNewlinePos < 0 ) {
530- op . chars = text . length ;
531- op . lines = 0 ;
532- this . append ( op ) ;
533- } else {
534- op . chars = lastNewlinePos + 1 ;
535- op . lines = text . match ( / \n / g) . length ;
536- this . append ( op ) ;
537- op . chars = text . length - ( lastNewlinePos + 1 ) ;
538- op . lines = 0 ;
539- this . append ( op ) ;
540- }
558+ warnDeprecated ( 'Changeset.SmartOpAssembler.prototype.appendOpWithText() is deprecated; ' +
559+ 'use Changeset.opsFromText() instead.' ) ;
560+ for ( const op of opsFromText ( opcode , text , attribs , pool ) ) this . append ( op ) ;
541561 }
542562
543563 toString ( ) {
@@ -1527,9 +1547,12 @@ exports.makeSplice = (oldFullText, spliceStart, numRemoved, newText, optNewTextA
15271547 const newLen = oldLen + newText . length - oldText . length ;
15281548
15291549 const assem = new SmartOpAssembler ( ) ;
1530- assem . appendOpWithText ( '=' , oldFullText . substring ( 0 , spliceStart ) ) ;
1531- assem . appendOpWithText ( '-' , oldText ) ;
1532- assem . appendOpWithText ( '+' , newText , optNewTextAPairs , pool ) ;
1550+ const ops = ( function * ( ) {
1551+ yield * opsFromText ( '=' , oldFullText . substring ( 0 , spliceStart ) ) ;
1552+ yield * opsFromText ( '-' , oldText ) ;
1553+ yield * opsFromText ( '+' , newText , optNewTextAPairs , pool ) ;
1554+ } ) ( ) ;
1555+ for ( const op of ops ) assem . append ( op ) ;
15331556 assem . endDocument ( ) ;
15341557 return exports . pack ( oldLen , newLen , assem . toString ( ) , newText ) ;
15351558} ;
@@ -1662,7 +1685,7 @@ exports.moveOpsToNewPool = (cs, oldPool, newPool) => {
16621685 */
16631686exports . makeAttribution = ( text ) => {
16641687 const assem = new SmartOpAssembler ( ) ;
1665- assem . appendOpWithText ( '+' , text ) ;
1688+ for ( const op of opsFromText ( '+' , text ) ) assem . append ( op ) ;
16661689 return assem . toString ( ) ;
16671690} ;
16681691
@@ -1902,11 +1925,11 @@ exports.builder = (oldLen) => {
19021925 return self ;
19031926 } ,
19041927 keepText : ( text , attribs , pool ) => {
1905- assem . appendOpWithText ( '=' , text , attribs , pool ) ;
1928+ for ( const op of opsFromText ( '=' , text , attribs , pool ) ) assem . append ( op ) ;
19061929 return self ;
19071930 } ,
19081931 insert : ( text , attribs , pool ) => {
1909- assem . appendOpWithText ( '+' , text , attribs , pool ) ;
1932+ for ( const op of opsFromText ( '+' , text , attribs , pool ) ) assem . append ( op ) ;
19101933 charBank . append ( text ) ;
19111934 return self ;
19121935 } ,
0 commit comments