Skip to content

Commit

Permalink
fix #16633
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Dec 6, 2016
1 parent 28b1636 commit 81d6900
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/vs/editor/common/model/editStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class EditStack {
onUnexpectedError(e);
this.currentOpenStackElement.afterCursorState = null;
}

this.currentOpenStackElement.afterVersionId = this.model.getVersionId();
return this.currentOpenStackElement.afterCursorState;
}
Expand Down
22 changes: 18 additions & 4 deletions src/vs/editor/contrib/linesOperations/common/linesOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,26 @@ export class TransposeAction extends EditorAction {

for (let i = 0, len = selections.length; i < len; i++) {
let selection = selections[i];
if (selection.isEmpty()) {
let cursor = selection.getStartPosition();
if (cursor.column > model.getLineContent(cursor.lineNumber).length) {
return;

if (!selection.isEmpty()) {
continue;
}

let cursor = selection.getStartPosition();
let maxColumn = model.getLineMaxColumn(cursor.lineNumber);

if (cursor.column >= maxColumn) {
if (cursor.lineNumber === model.getLineCount()) {
continue;
}

// The cursor is at the end of current line and current line is not empty
// then we transpose the character before the cursor and the line break if there is any following line.
let deleteSelection = new Range(cursor.lineNumber, Math.max(1, cursor.column - 1), cursor.lineNumber + 1, 1);
let chars = model.getValueInRange(deleteSelection).split('').reverse().join('');

commands.push(new ReplaceCommand(new Selection(cursor.lineNumber, Math.max(1, cursor.column - 1), cursor.lineNumber + 1, 1), chars));
} else {
let deleteSelection = new Range(cursor.lineNumber, Math.max(1, cursor.column - 1), cursor.lineNumber, cursor.column + 1);
let chars = model.getValueInRange(deleteSelection).split('').reverse().join('');
commands.push(new ReplaceCommandThatPreservesSelection(deleteSelection, chars,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ suite('Editor Contrib - Line Operations', () => {
[
'hello world',
'',
' '
'',
' ',
], {}, (editor, cursor) => {
let model = editor.getModel();
let transposeAction = new TransposeAction();
Expand All @@ -135,18 +136,55 @@ suite('Editor Contrib - Line Operations', () => {

editor.setSelection(new Selection(1, 12, 1, 12));
transposeAction.run(null, editor);
assert.equal(model.getLineContent(1), 'hell oworld', '005');
assert.deepEqual(editor.getSelection().toString(), new Selection(1, 12, 1, 12).toString(), '006');
assert.equal(model.getLineContent(1), 'hell oworl', '005');
assert.deepEqual(editor.getSelection().toString(), new Selection(2, 2, 2, 2).toString(), '006');

editor.setSelection(new Selection(2, 1, 2, 1));
editor.setSelection(new Selection(3, 1, 3, 1));
transposeAction.run(null, editor);
assert.equal(model.getLineContent(2), '', '007');
assert.deepEqual(editor.getSelection().toString(), new Selection(2, 1, 2, 1).toString(), '008');
assert.equal(model.getLineContent(3), '', '007');
assert.deepEqual(editor.getSelection().toString(), new Selection(4, 1, 4, 1).toString(), '008');

editor.setSelection(new Selection(3, 2, 3, 2));
editor.setSelection(new Selection(4, 2, 4, 2));
transposeAction.run(null, editor);
assert.equal(model.getLineContent(4), ' ', '009');
assert.deepEqual(editor.getSelection().toString(), new Selection(4, 3, 4, 3).toString(), '010');
}
);

// fix #16633
withMockCodeEditor(
[
'',
'',
'hello',
'world',
'',
'hello world',
'',
'hello world'
], {}, (editor, cursor) => {
let model = editor.getModel();
let transposeAction = new TransposeAction();

editor.setSelection(new Selection(1, 1, 1, 1));
transposeAction.run(null, editor);
assert.equal(model.getLineContent(3), ' ', '009');
assert.deepEqual(editor.getSelection().toString(), new Selection(3, 3, 3, 3).toString(), '010');
assert.equal(model.getLineContent(2), '', '011');
assert.deepEqual(editor.getSelection().toString(), new Selection(2, 1, 2, 1).toString(), '012');

editor.setSelection(new Selection(3, 6, 3, 6));
transposeAction.run(null, editor);
assert.equal(model.getLineContent(4), 'oworld', '013');
assert.deepEqual(editor.getSelection().toString(), new Selection(4, 2, 4, 2).toString(), '014');

editor.setSelection(new Selection(6, 12, 6, 12));
transposeAction.run(null, editor);
assert.equal(model.getLineContent(7), 'd', '015');
assert.deepEqual(editor.getSelection().toString(), new Selection(7, 2, 7, 2).toString(), '016');

editor.setSelection(new Selection(8, 12, 8, 12));
transposeAction.run(null, editor);
assert.equal(model.getLineContent(8), 'hello world', '019');
assert.deepEqual(editor.getSelection().toString(), new Selection(8, 12, 8, 12).toString(), '020');
}
);
});
Expand Down Expand Up @@ -204,23 +242,23 @@ suite('Editor Contrib - Line Operations', () => {

editor.setSelection(new Selection(1, 1, 1, 1));
uppercaseAction.run(null, editor);
assert.equal(model.getLineContent(1), '', '001');
assert.deepEqual(editor.getSelection().toString(), new Selection(1, 1, 1, 1).toString(), '002');
assert.equal(model.getLineContent(1), '', '013');
assert.deepEqual(editor.getSelection().toString(), new Selection(1, 1, 1, 1).toString(), '014');

editor.setSelection(new Selection(1, 1, 1, 1));
lowercaseAction.run(null, editor);
assert.equal(model.getLineContent(1), '', '003');
assert.deepEqual(editor.getSelection().toString(), new Selection(1, 1, 1, 1).toString(), '004');
assert.equal(model.getLineContent(1), '', '015');
assert.deepEqual(editor.getSelection().toString(), new Selection(1, 1, 1, 1).toString(), '016');

editor.setSelection(new Selection(2, 2, 2, 2));
uppercaseAction.run(null, editor);
assert.equal(model.getLineContent(2), ' ', '005');
assert.deepEqual(editor.getSelection().toString(), new Selection(2, 2, 2, 2).toString(), '006');
assert.equal(model.getLineContent(2), ' ', '017');
assert.deepEqual(editor.getSelection().toString(), new Selection(2, 2, 2, 2).toString(), '018');

editor.setSelection(new Selection(2, 2, 2, 2));
lowercaseAction.run(null, editor);
assert.equal(model.getLineContent(2), ' ', '007');
assert.deepEqual(editor.getSelection().toString(), new Selection(2, 2, 2, 2).toString(), '008');
assert.equal(model.getLineContent(2), ' ', '019');
assert.deepEqual(editor.getSelection().toString(), new Selection(2, 2, 2, 2).toString(), '020');
}
);
});
Expand Down

0 comments on commit 81d6900

Please sign in to comment.