@@ -758,9 +758,14 @@ exports.textLinesMutator = (lines) => {
758758 curSplice [ 1 ] += L - 1 ;
759759 const sline = curSplice . length - 1 ;
760760 removed = curSplice [ sline ] . substring ( curCol ) + removed ;
761- curSplice [ sline ] = curSplice [ sline ] . substring ( 0 , curCol ) +
762- lines_get ( curSplice [ 0 ] + curSplice [ 1 ] ) ;
763- curSplice [ 1 ] += 1 ;
761+ const line = lines_get ( curSplice [ 0 ] + curSplice [ 1 ] ) ;
762+ // if no line follows the splice
763+ if ( ! line ) {
764+ curSplice [ sline ] = curSplice [ sline ] . substring ( 0 , curCol ) ;
765+ } else {
766+ curSplice [ sline ] = curSplice [ sline ] . substring ( 0 , curCol ) + line ;
767+ curSplice [ 1 ] += 1 ;
768+ }
764769 }
765770 } else {
766771 removed = nextKLinesText ( L ) ;
@@ -822,14 +827,27 @@ exports.textLinesMutator = (lines) => {
822827 curLine += newLines . length ;
823828 // insert the remaining chars from the "old" line (e.g. the line we were in
824829 // when we started to insert new lines)
825- curSplice . push ( theLine . substring ( lineCol ) ) ;
830+ // if nothing is left we don't push an empty string
831+ if ( theLine . substring ( lineCol ) ) {
832+ curSplice . push ( theLine . substring ( lineCol ) ) ;
833+ }
826834 curCol = 0 ; // TODO(doc) why is this not set to the length of last line?
827835 } else {
828836 Array . prototype . push . apply ( curSplice , newLines ) ;
829837 curLine += newLines . length ;
830838 }
839+ } else if ( lines_get ( curSplice [ 0 ] + curSplice [ 1 ] ) === undefined ) {
840+ // find out if there is a line in splice that is not finished processing
841+ if ( isCurLineInSplice ( ) ) { // if yes, we can add our text to it
842+ const sline = curSplice . length - 1 ;
843+ curSplice [ sline ] =
844+ curSplice [ sline ] . substring ( 0 , curCol ) + text + curSplice [ sline ] . substring ( curCol ) ;
845+ curCol += text . length ;
846+ } else { // if no, we need to add the text in a new line
847+ Array . prototype . push . apply ( curSplice , [ text ] ) ;
848+ curCol += text . length ;
849+ }
831850 } else {
832- // there are no additional lines
833851 // although the line is put into splice, curLine is not increased, because
834852 // there may be more chars in the line (newline is not reached)
835853 const sline = putCurLineInSplice ( ) ;
0 commit comments