Skip to content

Commit

Permalink
Merge pull request #37196 from Takadimi/Takadimi/36562
Browse files Browse the repository at this point in the history
#36562 Sort multiple selections
  • Loading branch information
alexdima authored Nov 14, 2017
2 parents ee85477 + 4c8d50c commit 537ebcf
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 12 deletions.
22 changes: 12 additions & 10 deletions src/vs/editor/contrib/linesOperations/linesOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class MoveLinesDownAction extends AbstractMoveLinesAction {
}
}

abstract class AbstractSortLinesAction extends EditorAction {
export abstract class AbstractSortLinesAction extends EditorAction {
private descending: boolean;

constructor(descending: boolean, opts: IActionOptions) {
Expand All @@ -149,16 +149,18 @@ abstract class AbstractSortLinesAction extends EditorAction {
}

public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
const selections = editor.getSelections();

if (!SortLinesCommand.canRun(editor.getModel(), editor.getSelection(), this.descending)) {
return;
for (let i = 0, len = selections.length; i < len; i++) {
const selection = selections[i];
if (!SortLinesCommand.canRun(editor.getModel(), selection, this.descending)) {
return;
}
}

var commands: ICommand[] = [];
var selections = editor.getSelections();

for (var i = 0; i < selections.length; i++) {
commands.push(new SortLinesCommand(selections[i], this.descending));
let commands: ICommand[] = [];
for (let i = 0, len = selections.length; i < len; i++) {
commands[i] = new SortLinesCommand(selections[i], this.descending);
}

editor.pushUndoStop();
Expand All @@ -167,7 +169,7 @@ abstract class AbstractSortLinesAction extends EditorAction {
}
}

class SortLinesAscendingAction extends AbstractSortLinesAction {
export class SortLinesAscendingAction extends AbstractSortLinesAction {
constructor() {
super(false, {
id: 'editor.action.sortLinesAscending',
Expand All @@ -178,7 +180,7 @@ class SortLinesAscendingAction extends AbstractSortLinesAction {
}
}

class SortLinesDescendingAction extends AbstractSortLinesAction {
export class SortLinesDescendingAction extends AbstractSortLinesAction {
constructor() {
super(true, {
id: 'editor.action.sortLinesDescending',
Expand Down
119 changes: 118 additions & 1 deletion src/vs/editor/contrib/linesOperations/test/linesOperations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,129 @@ import { Selection } from 'vs/editor/common/core/selection';
import { Position } from 'vs/editor/common/core/position';
import { Handler, IModel, DefaultEndOfLine } from 'vs/editor/common/editorCommon';
import { withTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
import { DeleteAllLeftAction, JoinLinesAction, TransposeAction, UpperCaseAction, LowerCaseAction, DeleteAllRightAction, InsertLineBeforeAction, InsertLineAfterAction, IndentLinesAction } from 'vs/editor/contrib/linesOperations/linesOperations';
import { DeleteAllLeftAction, JoinLinesAction, TransposeAction, UpperCaseAction, LowerCaseAction, DeleteAllRightAction, InsertLineBeforeAction, InsertLineAfterAction, IndentLinesAction, SortLinesAscendingAction, SortLinesDescendingAction } from 'vs/editor/contrib/linesOperations/linesOperations';
import { Cursor } from 'vs/editor/common/controller/cursor';
import { Model } from 'vs/editor/common/model/model';
import { CoreEditingCommands } from 'vs/editor/browser/controller/coreCommands';

suite('Editor Contrib - Line Operations', () => {
suite('SortLinesAscendingAction', () => {
test('should sort selected lines in ascending order', function () {
withTestCodeEditor(
[
'omicron',
'beta',
'alpha'
], {}, (editor, cursor) => {
let model = editor.getModel();
let sortLinesAscendingAction = new SortLinesAscendingAction();

editor.setSelection(new Selection(1, 1, 3, 5));
sortLinesAscendingAction.run(null, editor);
assert.deepEqual(model.getLinesContent(), [
'alpha',
'beta',
'omicron'
]);
assert.deepEqual(editor.getSelection().toString(), new Selection(1, 1, 3, 7).toString());
});
});

test('should sort multiple selections in ascending order', function () {
withTestCodeEditor(
[
'omicron',
'beta',
'alpha',
'',
'omicron',
'beta',
'alpha'
], {}, (editor, cursor) => {
let model = editor.getModel();
let sortLinesAscendingAction = new SortLinesAscendingAction();

editor.setSelections([new Selection(1, 1, 3, 5), new Selection(5, 1, 7, 5)]);
sortLinesAscendingAction.run(null, editor);
assert.deepEqual(model.getLinesContent(), [
'alpha',
'beta',
'omicron',
'',
'alpha',
'beta',
'omicron'
]);
let expectedSelections = [
new Selection(1, 1, 3, 7),
new Selection(5, 1, 7, 7)
];
editor.getSelections().forEach((actualSelection, index) => {
assert.deepEqual(actualSelection.toString(), expectedSelections[index].toString());
});
});
});
});

suite('SortLinesDescendingAction', () => {
test('should sort selected lines in descending order', function () {
withTestCodeEditor(
[
'alpha',
'beta',
'omicron'
], {}, (editor, cursor) => {
let model = editor.getModel();
let sortLinesDescendingAction = new SortLinesDescendingAction();

editor.setSelection(new Selection(1, 1, 3, 7));
sortLinesDescendingAction.run(null, editor);
assert.deepEqual(model.getLinesContent(), [
'omicron',
'beta',
'alpha'
]);
assert.deepEqual(editor.getSelection().toString(), new Selection(1, 1, 3, 5).toString());
});
});

test('should sort multiple selections in descending order', function () {
withTestCodeEditor(
[
'alpha',
'beta',
'omicron',
'',
'alpha',
'beta',
'omicron'
], {}, (editor, cursor) => {
let model = editor.getModel();
let sortLinesDescendingAction = new SortLinesDescendingAction();

editor.setSelections([new Selection(1, 1, 3, 7), new Selection(5, 1, 7, 7)]);
sortLinesDescendingAction.run(null, editor);
assert.deepEqual(model.getLinesContent(), [
'omicron',
'beta',
'alpha',
'',
'omicron',
'beta',
'alpha'
]);
let expectedSelections = [
new Selection(1, 1, 3, 5),
new Selection(5, 1, 7, 5)
];
editor.getSelections().forEach((actualSelection, index) => {
assert.deepEqual(actualSelection.toString(), expectedSelections[index].toString());
});
});
});
});


suite('DeleteAllLeftAction', () => {
test('should delete to the left of the cursor', function () {
withTestCodeEditor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ suite('Editor Contrib - Sort Lines Command', () => {
);
});

test('sorting first 4 lines desscending', function () {
test('sorting first 4 lines descending', function () {
testSortLinesDescendingCommand(
[
'first',
Expand Down

0 comments on commit 537ebcf

Please sign in to comment.