Skip to content

Commit d55b443

Browse files
committed
Stashing all selections before proceeding with selections distinct by line. Applies stash after line movement to keep previous
selections. Aborting when moving multiple lines and the line is currently at top or end of document
1 parent 1e009f7 commit d55b443

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

src/vs/editor/contrib/linesOperations/browser/linesOperations.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,41 +184,50 @@ abstract class AbstractMoveLinesAction extends EditorAction {
184184
movingMultipleLines = selections[0].endLineNumber !== selections[selections.length - 1].endLineNumber;
185185

186186
if (movingMultipleLines) {
187+
// Stash selections while processing, set new selections +/- line-change only
188+
for (const selection of selections) {
189+
let startCol = selection.getStartPosition().column;
190+
let startLine = selection.getStartPosition().lineNumber;
191+
let newStartLine = this.down ? startLine + 1 : startLine - 1;
192+
193+
let endCol = selection.getEndPosition().column;
194+
let endLine = selection.getEndPosition().lineNumber;
195+
let newEndLine = this.down ? endLine + 1 : endLine - 1;
196+
197+
newSelections.push(new Selection(newStartLine, startCol, newEndLine, endCol));
198+
}
199+
187200
// Work only with one selection per line
188201
selections = selections.filter((s, idx, arr) => {
189202
return arr.map(sel => sel['endLineNumber']).indexOf(s['endLineNumber']) === idx;
190203
});
191204

192205
selectionDirectionDown = selections[0].endLineNumber < selections[selections.length - 1].endLineNumber;
193-
}
194-
}
195206

196-
if (movingMultipleLines) {
197-
editor.pushUndoStop();
207+
editor.pushUndoStop();
198208

199-
if (selectionDirectionDown === this.down) {
200-
selections.reverse();
209+
if (selectionDirectionDown === this.down) {
210+
selections.reverse();
211+
}
201212
}
202213
}
203214

204215
for (const selection of selections) {
205216
if (movingMultipleLines) {
206-
editor.executeCommand(this.id, new MoveLinesCommand(selection, this.down, autoIndent, languageConfigurationService));
207-
208-
let editorSelection = editor.getSelection();
209-
if (editorSelection !== null) {
210-
newSelections.push(editorSelection);
217+
// If we are at the beginning/end of document and multiple lines are moved, abort.
218+
if ((selection.startLineNumber === 1 && !this.down) || (selection.endLineNumber === editor.getModel()?.getLineCount() && this.down)) {
219+
editor.pushUndoStop();
220+
return;
211221
}
222+
223+
editor.executeCommand(this.id, new MoveLinesCommand(selection, this.down, autoIndent, languageConfigurationService));
212224
} else {
213225
commands.push(new MoveLinesCommand(selection, this.down, autoIndent, languageConfigurationService));
214226
}
215227
}
216228

217229
if (movingMultipleLines) {
218230
if (newSelections.length > 1) {
219-
if (selectionDirectionDown === this.down) {
220-
newSelections.reverse();
221-
}
222231
editor.setSelections(newSelections);
223232
}
224233

0 commit comments

Comments
 (0)