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