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