@@ -9,6 +9,7 @@ import { Range } from 'vs/editor/common/core/range';
99import { ApplyEditsResult , EndOfLinePreference , FindMatch , IInternalModelContentChange , ISingleEditOperationIdentifier , ITextBuffer , ITextSnapshot , ValidAnnotatedEditOperation , IValidEditOperation } from 'vs/editor/common/model' ;
1010import { PieceTreeBase , StringBuffer } from 'vs/editor/common/model/pieceTreeTextBuffer/pieceTreeBase' ;
1111import { SearchData } from 'vs/editor/common/model/textModelSearch' ;
12+ import { CharCode } from 'vs/base/common/charCode' ;
1213
1314export interface IValidatedEditOperation {
1415 sortIndex : number ;
@@ -220,13 +221,42 @@ export class PieceTreeTextBuffer implements ITextBuffer {
220221 if ( ! mightContainNonBasicASCII && op . text ) {
221222 mightContainNonBasicASCII = ! strings . isBasicASCII ( op . text ) ;
222223 }
224+
225+ let lines : string [ ] | null = null ;
226+ if ( op . text ) {
227+ lines = [ ] ;
228+ let linesLen = 0 ;
229+ let lastLineStart = 0 ;
230+ for ( let j = 0 , len = op . text . length ; j < len ; j ++ ) {
231+ const chr = op . text . charCodeAt ( j ) ;
232+
233+ if ( chr === CharCode . CarriageReturn ) {
234+ if ( j + 1 < len && op . text . charCodeAt ( j + 1 ) === CharCode . LineFeed ) {
235+ // \r\n... case
236+ lines [ linesLen ++ ] = op . text . substring ( lastLineStart , j ) ;
237+ lastLineStart = j + 2 ;
238+ j ++ ; // skip \n
239+ } else {
240+ // \r... case
241+ lines [ linesLen ++ ] = op . text . substring ( lastLineStart , j ) ;
242+ lastLineStart = j + 1 ;
243+ }
244+ } else if ( chr === CharCode . LineFeed ) {
245+ // \n... case
246+ lines [ linesLen ++ ] = op . text . substring ( lastLineStart , j ) ;
247+ lastLineStart = j + 1 ;
248+ }
249+ }
250+ lines [ linesLen ++ ] = op . text . substring ( lastLineStart , op . text . length ) ;
251+ }
252+
223253 operations [ i ] = {
224254 sortIndex : i ,
225255 identifier : op . identifier || null ,
226256 range : validatedRange ,
227257 rangeOffset : this . getOffsetAt ( validatedRange . startLineNumber , validatedRange . startColumn ) ,
228258 rangeLength : this . getValueLengthInRange ( validatedRange ) ,
229- lines : op . text ? op . text . split ( / \r \n | \r | \n / ) : null ,
259+ lines : lines ,
230260 forceMoveMarkers : Boolean ( op . forceMoveMarkers ) ,
231261 isAutoWhitespaceEdit : op . isAutoWhitespaceEdit || false
232262 } ;
@@ -426,10 +456,6 @@ export class PieceTreeTextBuffer implements ITextBuffer {
426456 continue ;
427457 }
428458
429- const deletingLinesCnt = endLineNumber - startLineNumber ;
430- const insertingLinesCnt = ( op . lines ? op . lines . length - 1 : 0 ) ;
431- const editingLinesCnt = Math . min ( deletingLinesCnt , insertingLinesCnt ) ;
432-
433459 const text = ( op . lines ? op . lines . join ( this . getEOL ( ) ) : '' ) ;
434460
435461 if ( text ) {
@@ -442,15 +468,6 @@ export class PieceTreeTextBuffer implements ITextBuffer {
442468 this . _pieceTree . delete ( op . rangeOffset , op . rangeLength ) ;
443469 }
444470
445- if ( editingLinesCnt < insertingLinesCnt ) {
446- let newLinesContent : string [ ] = [ ] ;
447- for ( let j = editingLinesCnt + 1 ; j <= insertingLinesCnt ; j ++ ) {
448- newLinesContent . push ( op . lines ! [ j ] ) ;
449- }
450-
451- newLinesContent [ newLinesContent . length - 1 ] = this . getLineContent ( startLineNumber + insertingLinesCnt - 1 ) ;
452- }
453-
454471 const contentChangeRange = new Range ( startLineNumber , startColumn , endLineNumber , endColumn ) ;
455472 contentChanges . push ( {
456473 range : contentChangeRange ,
0 commit comments