未验证 提交 ac49d591 编写于 作者: A Alex Dima

Use sorting to avoid N^2

上级 38db643f
......@@ -37,24 +37,36 @@ abstract class AbstractCopyLinesAction extends EditorAction {
}
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void {
if (!editor.hasModel()) {
return;
}
let commands: ICommand[] = [];
let selections = editor.getSelections() || [];
selections = selections.reduce((accumulator, selection) => {
const isCurrentSelectionDuplicated = accumulator.some(value => (
value.startLineNumber === selection.startLineNumber &&
value.endLineNumber === selection.endLineNumber
));
if (isCurrentSelectionDuplicated) {
return accumulator;
const selections = editor.getSelections().map((selection, index) => ({ selection, index, ignore: false }));
selections.sort((a, b) => Range.compareRangesUsingStarts(a.selection, b.selection));
// Remove selections that would result in copying the same line
let prev = selections[0];
for (let i = 1; i < selections.length; i++) {
const curr = selections[i];
if (prev.selection.endLineNumber === curr.selection.startLineNumber) {
// these two selections would copy the same line
if (prev.index < curr.index) {
// prev wins
curr.ignore = true;
} else {
// curr wins
prev.ignore = true;
prev = curr;
}
}
}
return [...accumulator, selection];
}, []);
const commands: ICommand[] = [];
for (const selection of selections) {
commands.push(new CopyLinesCommand(selection, this.down));
if (selection.ignore) {
continue;
}
commands.push(new CopyLinesCommand(selection.selection, this.down));
}
editor.pushUndoStop();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册