Skip to content

Commit

Permalink
Fixes #48741: Sort reverse edit operations only if order is not signi…
Browse files Browse the repository at this point in the history
…ficant
  • Loading branch information
alexdima committed Apr 26, 2018
1 parent b1f5a4d commit 3929ea0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,17 @@ export class PieceTreeTextBuffer implements ITextBuffer {
// Sort operations ascending
operations.sort(PieceTreeTextBuffer._sortOpsAscending);

let hasTouchingRanges = false;
for (let i = 0, count = operations.length - 1; i < count; i++) {
let rangeEnd = operations[i].range.getEndPosition();
let nextRangeStart = operations[i + 1].range.getStartPosition();

if (nextRangeStart.isBefore(rangeEnd)) {
// overlapping ranges
throw new Error('Overlapping ranges are not allowed!');
if (nextRangeStart.isBeforeOrEqual(rangeEnd)) {
if (nextRangeStart.isBefore(rangeEnd)) {
// overlapping ranges
throw new Error('Overlapping ranges are not allowed!');
}
hasTouchingRanges = true;
}
}

Expand Down Expand Up @@ -256,7 +260,11 @@ export class PieceTreeTextBuffer implements ITextBuffer {
forceMoveMarkers: op.forceMoveMarkers
};
}
reverseOperations.sort((a, b) => a.sortIndex - b.sortIndex);

// Can only sort reverse operations when the order is not significant
if (!hasTouchingRanges) {
reverseOperations.sort((a, b) => a.sortIndex - b.sortIndex);
}

this._mightContainRTL = mightContainRTL;
this._mightContainNonBasicASCII = mightContainNonBasicASCII;
Expand Down
22 changes: 22 additions & 0 deletions src/vs/editor/test/common/model/editableTextModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1091,4 +1091,26 @@ suite('EditorModel - EditableTextModel.applyEdits', () => {

model.dispose();
});

test('issue #48741: Broken undo stack with move lines up with multiple cursors', () => {
let model = createEditableTextModelFromString([
'line1',
'line2',
'line3',
'',
].join('\n'));

const undoEdits = model.applyEdits([
{ range: new Range(4, 1, 4, 1), text: 'line3', },
{ range: new Range(3, 1, 3, 6), text: null, },
{ range: new Range(2, 1, 3, 1), text: null, },
{ range: new Range(3, 6, 3, 6), text: '\nline2' }
]);

model.applyEdits(undoEdits);

assert.deepEqual(model.getValue(), 'line1\nline2\nline3\n');

model.dispose();
});
});

0 comments on commit 3929ea0

Please sign in to comment.