@@ -711,9 +711,14 @@ exports.textLinesMutator = (lines) => {
711711 curSplice [ 1 ] += L - 1 ;
712712 const sline = curSplice . length - 1 ;
713713 removed = curSplice [ sline ] . substring ( curCol ) + removed ;
714- curSplice [ sline ] = curSplice [ sline ] . substring ( 0 , curCol ) +
715- linesGet ( curSplice [ 0 ] + curSplice [ 1 ] ) ;
716- curSplice [ 1 ] += 1 ;
714+ const line = linesGet ( curSplice [ 0 ] + curSplice [ 1 ] ) ;
715+ // if no line follows the splice
716+ if ( ! line ) {
717+ curSplice [ sline ] = curSplice [ sline ] . substring ( 0 , curCol ) ;
718+ } else {
719+ curSplice [ sline ] = curSplice [ sline ] . substring ( 0 , curCol ) + line ;
720+ curSplice [ 1 ] += 1 ;
721+ }
717722 }
718723 } else {
719724 removed = nextKLinesText ( L ) ;
@@ -775,14 +780,27 @@ exports.textLinesMutator = (lines) => {
775780 curLine += newLines . length ;
776781 // insert the remaining chars from the "old" line (e.g. the line we were in
777782 // when we started to insert new lines)
778- curSplice . push ( theLine . substring ( lineCol ) ) ;
783+ // if nothing is left we don't push an empty string
784+ if ( theLine . substring ( lineCol ) ) {
785+ curSplice . push ( theLine . substring ( lineCol ) ) ;
786+ }
779787 curCol = 0 ; // TODO(doc) why is this not set to the length of last line?
780788 } else {
781789 Array . prototype . push . apply ( curSplice , newLines ) ;
782790 curLine += newLines . length ;
783791 }
792+ } else if ( lines_get ( curSplice [ 0 ] + curSplice [ 1 ] ) === undefined ) {
793+ // find out if there is a line in splice that is not finished processing
794+ if ( isCurLineInSplice ( ) ) { // if yes, we can add our text to it
795+ const sline = curSplice . length - 1 ;
796+ curSplice [ sline ] =
797+ curSplice [ sline ] . substring ( 0 , curCol ) + text + curSplice [ sline ] . substring ( curCol ) ;
798+ curCol += text . length ;
799+ } else { // if no, we need to add the text in a new line
800+ Array . prototype . push . apply ( curSplice , [ text ] ) ;
801+ curCol += text . length ;
802+ }
784803 } else {
785- // there are no additional lines
786804 // although the line is put into splice, curLine is not increased, because
787805 // there may be more chars in the line (newline is not reached)
788806 const sline = putCurLineInSplice ( ) ;
0 commit comments