未验证 提交 537ebcf4 编写于 作者: A Alexandru Dima 提交者: GitHub

Merge pull request #37196 from Takadimi/Takadimi/36562

#36562 Sort multiple selections
......@@ -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) {
......@@ -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();
......@@ -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',
......@@ -178,7 +180,7 @@ class SortLinesAscendingAction extends AbstractSortLinesAction {
}
}
class SortLinesDescendingAction extends AbstractSortLinesAction {
export class SortLinesDescendingAction extends AbstractSortLinesAction {
constructor() {
super(true, {
id: 'editor.action.sortLinesDescending',
......
......@@ -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(
......
......@@ -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',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册