@@ -711,9 +711,14 @@ exports.textLinesMutator = (lines) => {
711
711
curSplice [ 1 ] += L - 1 ;
712
712
const sline = curSplice . length - 1 ;
713
713
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
+ }
717
722
}
718
723
} else {
719
724
removed = nextKLinesText ( L ) ;
@@ -775,14 +780,27 @@ exports.textLinesMutator = (lines) => {
775
780
curLine += newLines . length ;
776
781
// insert the remaining chars from the "old" line (e.g. the line we were in
777
782
// 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
+ }
779
787
curCol = 0 ; // TODO(doc) why is this not set to the length of last line?
780
788
} else {
781
789
Array . prototype . push . apply ( curSplice , newLines ) ;
782
790
curLine += newLines . length ;
783
791
}
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
+ }
784
803
} else {
785
- // there are no additional lines
786
804
// although the line is put into splice, curLine is not increased, because
787
805
// there may be more chars in the line (newline is not reached)
788
806
const sline = putCurLineInSplice ( ) ;
0 commit comments