@@ -802,8 +802,14 @@ exports.textLinesMutator = function (lines) {
802802 curSplice [ 1 ] += L - 1 ;
803803 var sline = curSplice . length - 1 ;
804804 removed = curSplice [ sline ] . substring ( curCol ) + removed ;
805- curSplice [ sline ] = curSplice [ sline ] . substring ( 0 , curCol ) + lines_get ( curSplice [ 0 ] + curSplice [ 1 ] ) ;
806- curSplice [ 1 ] += 1 ;
805+ var line = lines_get ( curSplice [ 0 ] + curSplice [ 1 ] ) ;
806+ // if no line follows the splice
807+ if ( ! line ) {
808+ curSplice [ sline ] = curSplice [ sline ] . substring ( 0 , curCol ) ;
809+ } else {
810+ curSplice [ sline ] = curSplice [ sline ] . substring ( 0 , curCol ) + line ;
811+ curSplice [ 1 ] += 1 ;
812+ }
807813 }
808814 } else {
809815 removed = nextKLinesText ( L ) ;
@@ -854,6 +860,7 @@ exports.textLinesMutator = function (lines) {
854860 if ( L ) {
855861 var newLines = exports . splitTextLines ( text ) ;
856862 if ( isCurLineInSplice ( ) ) {
863+ //TODO(doc) is this relevant?
857864 //if (curCol == 0) {
858865 //curSplice.length--;
859866 //curSplice[1]--;
@@ -864,22 +871,47 @@ exports.textLinesMutator = function (lines) {
864871 var sline = curSplice . length - 1 ;
865872 var theLine = curSplice [ sline ] ;
866873 var lineCol = curCol ;
874+ // insert the first new line
867875 curSplice [ sline ] = theLine . substring ( 0 , lineCol ) + newLines [ 0 ] ;
868876 curLine ++ ;
869877 newLines . splice ( 0 , 1 ) ;
878+ // insert the remaining new lines
870879 Array . prototype . push . apply ( curSplice , newLines ) ;
871880 curLine += newLines . length ;
872- curSplice . push ( theLine . substring ( lineCol ) ) ;
873- curCol = 0 ;
881+ // insert the remaining chars from the "old" line (e.g. the line we were in
882+ // when we started to insert new lines)
883+ // if nothing is left we don't push an empty string
884+ if ( theLine . substring ( lineCol ) ) {
885+ curSplice . push ( theLine . substring ( lineCol ) ) ;
886+ }
887+ curCol = 0 ; // TODO(doc) why is this not set to the length of last line?
874888 //}
875889 } else {
876890 Array . prototype . push . apply ( curSplice , newLines ) ;
877891 curLine += newLines . length ;
878892 }
879893 } else {
880- var sline = putCurLineInSplice ( ) ;
881- curSplice [ sline ] = curSplice [ sline ] . substring ( 0 , curCol ) + text + curSplice [ sline ] . substring ( curCol ) ;
882- curCol += text . length ;
894+ // there are no additional lines
895+ if ( lines_get ( curSplice [ 0 ] + curSplice [ 1 ] ) === undefined ) {
896+ // find out if there is a line in splice that is not finished processing
897+ // if yes, we can add our text to it
898+ if ( isCurLineInSplice ( ) ) {
899+ var sline = curSplice . length - 1 ;
900+ curSplice [ sline ] = curSplice [ sline ] . substring ( 0 , curCol ) + text + curSplice [ sline ] . substring ( curCol ) ;
901+ curCol += text . length ;
902+ }
903+ // if no, we need to add the text in a new line
904+ else {
905+ Array . prototype . push . apply ( curSplice , [ text ] ) ;
906+ curCol += text . length ;
907+ }
908+ } else {
909+ // although the line is put into splice, curLine is not increased, because
910+ // there may be more chars in the line (newline is not reached)
911+ var sline = putCurLineInSplice ( ) ;
912+ curSplice [ sline ] = curSplice [ sline ] . substring ( 0 , curCol ) + text + curSplice [ sline ] . substring ( curCol ) ;
913+ curCol += text . length ;
914+ }
883915 }
884916 //debugPrint("insert");
885917 }
0 commit comments