@@ -1918,98 +1918,100 @@ exports.attribsAttributeValue = (attribs, key, pool) => {
19181918
19191919/**
19201920 * Incrementally builds a Changeset.
1921- *
1922- * @typedef {object } Builder
1923- * @property {Function } insert -
1924- * @property {Function } keep -
1925- * @property {Function } keepText -
1926- * @property {Function } remove -
1927- * @property {Function } toString -
19281921 */
1922+ class Builder {
1923+ /**
1924+ * @param {number } oldLen - Old length
1925+ */
1926+ constructor ( oldLen ) {
1927+ this . _oldLen = oldLen ;
1928+ this . _ops = [ ] ;
1929+ this . _charBank = exports . stringAssembler ( ) ;
1930+ }
19291931
1930- /**
1931- * @param {number } oldLen - Old length
1932- * @returns {Builder }
1933- */
1934- exports . builder = ( oldLen ) => {
1935- const ops = [ ] ;
1936- const charBank = exports . stringAssembler ( ) ;
1932+ /**
1933+ * @param {number } N - Number of characters to keep.
1934+ * @param {number } L - Number of newlines among the `N` characters. If positive, the last
1935+ * character must be a newline.
1936+ * @param {(string|Attribute[]) } attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...'
1937+ * (no pool needed in latter case).
1938+ * @param {?AttributePool } pool - Attribute pool, only required if `attribs` is a list of
1939+ * attribute key, value pairs.
1940+ * @returns {Builder } this
1941+ */
1942+ keep ( N , L , attribs , pool ) {
1943+ const o = new Op ( '=' ) ;
1944+ o . attribs = typeof attribs === 'string'
1945+ ? attribs : new AttributeMap ( pool ) . update ( attribs || [ ] ) . toString ( ) ;
1946+ o . chars = N ;
1947+ o . lines = ( L || 0 ) ;
1948+ this . _ops . push ( o ) ;
1949+ return this ;
1950+ }
19371951
1938- const self = {
1939- /**
1940- * @param {number } N - Number of characters to keep.
1941- * @param {number } L - Number of newlines among the `N` characters. If positive, the last
1942- * character must be a newline.
1943- * @param {(string|Attribute[]) } attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...'
1944- * (no pool needed in latter case).
1945- * @param {?AttributePool } pool - Attribute pool, only required if `attribs` is a list of
1946- * attribute key, value pairs.
1947- * @returns {Builder } this
1948- */
1949- keep : ( N , L , attribs , pool ) => {
1950- const o = new Op ( '=' ) ;
1951- o . attribs = typeof attribs === 'string'
1952- ? attribs : new AttributeMap ( pool ) . update ( attribs || [ ] ) . toString ( ) ;
1953- o . chars = N ;
1954- o . lines = ( L || 0 ) ;
1955- ops . push ( o ) ;
1956- return self ;
1957- } ,
1952+ /**
1953+ * @param {string } text - Text to keep.
1954+ * @param {(string|Attribute[]) } attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...'
1955+ * (no pool needed in latter case).
1956+ * @param {?AttributePool } pool - Attribute pool, only required if `attribs` is a list of
1957+ * attribute key, value pairs.
1958+ * @returns {Builder } this
1959+ */
1960+ keepText ( text , attribs , pool ) {
1961+ this . _ops . push ( ...opsFromText ( '=' , text , attribs , pool ) ) ;
1962+ return this ;
1963+ }
19581964
1959- /**
1960- * @param {string } text - Text to keep.
1961- * @param {(string|Attribute[]) } attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...'
1962- * (no pool needed in latter case).
1963- * @param {?AttributePool } pool - Attribute pool, only required if `attribs` is a list of
1964- * attribute key, value pairs.
1965- * @returns {Builder } this
1966- */
1967- keepText : ( text , attribs , pool ) => {
1968- ops . push ( ...opsFromText ( '=' , text , attribs , pool ) ) ;
1969- return self ;
1970- } ,
1965+ /**
1966+ * @param {string } text - Text to insert.
1967+ * @param {(string|Attribute[]) } attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...'
1968+ * (no pool needed in latter case).
1969+ * @param {?AttributePool } pool - Attribute pool, only required if `attribs` is a list of
1970+ * attribute key, value pairs.
1971+ * @returns {Builder } this
1972+ */
1973+ insert ( text , attribs , pool ) {
1974+ this . _ops . push ( ...opsFromText ( '+' , text , attribs , pool ) ) ;
1975+ this . _charBank . append ( text ) ;
1976+ return this ;
1977+ }
19711978
1972- /**
1973- * @param {string } text - Text to insert.
1974- * @param {(string|Attribute[]) } attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...'
1975- * (no pool needed in latter case).
1976- * @param {?AttributePool } pool - Attribute pool, only required if `attribs` is a list of
1977- * attribute key, value pairs.
1978- * @returns {Builder } this
1979- */
1980- insert : ( text , attribs , pool ) => {
1981- ops . push ( ...opsFromText ( '+' , text , attribs , pool ) ) ;
1982- charBank . append ( text ) ;
1983- return self ;
1984- } ,
1979+ /**
1980+ * @param {number } N - Number of characters to remove.
1981+ * @param {number } L - Number of newlines among the `N` characters. If positive, the last
1982+ * character must be a newline.
1983+ * @returns {Builder } this
1984+ */
1985+ remove ( N , L ) {
1986+ const o = new Op ( '-' ) ;
1987+ o . attribs = '' ;
1988+ o . chars = N ;
1989+ o . lines = ( L || 0 ) ;
1990+ this . _ops . push ( o ) ;
1991+ return this ;
1992+ }
19851993
1986- /**
1987- * @param {number } N - Number of characters to remove.
1988- * @param {number } L - Number of newlines among the `N` characters. If positive, the last
1989- * character must be a newline.
1990- * @returns {Builder } this
1991- */
1992- remove : ( N , L ) => {
1993- const o = new Op ( '-' ) ;
1994- o . attribs = '' ;
1995- o . chars = N ;
1996- o . lines = ( L || 0 ) ;
1997- ops . push ( o ) ;
1998- return self ;
1999- } ,
2000-
2001- toString : ( ) => {
2002- /** @type {number } */
2003- let lengthChange ;
2004- const serializedOps = exports . serializeOps ( ( function * ( ) {
2005- lengthChange = yield * exports . canonicalizeOps ( ops , true ) ;
2006- } ) ( ) ) ;
2007- const newLen = oldLen + lengthChange ;
2008- return exports . pack ( oldLen , newLen , serializedOps , charBank . toString ( ) ) ;
2009- } ,
2010- } ;
1994+ toString ( ) {
1995+ /** @type {number } */
1996+ let lengthChange ;
1997+ const serializedOps = exports . serializeOps ( ( function * ( ) {
1998+ lengthChange = yield * exports . canonicalizeOps ( this . _ops , true ) ;
1999+ } ) . call ( this ) ) ;
2000+ const newLen = this . _oldLen + lengthChange ;
2001+ return exports . pack ( this . _oldLen , newLen , serializedOps , this . _charBank . toString ( ) ) ;
2002+ }
2003+ }
2004+ exports . Builder = Builder ;
20112005
2012- return self ;
2006+ /**
2007+ * @deprecated Use the `Builder` class instead.
2008+ * @param {number } oldLen - Old length
2009+ * @returns {Builder }
2010+ */
2011+ exports . builder = ( oldLen ) => {
2012+ padutils . warnWithStack (
2013+ 'Changeset.builder() is deprecated; use the Changeset.Builder class instead' ) ;
2014+ return new Builder ( oldLen ) ;
20132015} ;
20142016
20152017/**
@@ -2110,7 +2112,7 @@ exports.inverse = (cs, lines, alines, pool) => {
21102112 let curLineNextOp = new Op ( '+' ) ;
21112113
21122114 const unpacked = exports . unpack ( cs ) ;
2113- const builder = exports . builder ( unpacked . newLen ) ;
2115+ const builder = new Builder ( unpacked . newLen ) ;
21142116
21152117 const consumeAttribRuns = ( numChars , func /* (len, attribs, endsLine)*/ ) => {
21162118 if ( ! curLineOps || curLineOpsLine !== curLine ) {
0 commit comments