From 6c74e2e3fb6eeb210c78be594c88b0b97abe0912 Mon Sep 17 00:00:00 2001 From: Federico Brancasi Date: Sat, 28 Nov 2026 15:51:01 +0100 Subject: [PATCH 1/2] Delete Duplicate Lines: ignore the line a selection ends on at column 1 Selecting whole lines leaves the selection at column 1 of the next line. Delete Duplicate Lines still included that line, so it could delete a line that wasn't selected. Exclude it, like Copy, Move, Sort, Reverse and Delete Lines do. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../browser/linesOperations.ts | 14 +++++++++---- .../test/browser/linesOperations.test.ts | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/vs/editor/contrib/linesOperations/browser/linesOperations.ts b/src/vs/editor/contrib/linesOperations/browser/linesOperations.ts index 8e69736c87b960..b0758888e68f27 100644 --- a/src/vs/editor/contrib/linesOperations/browser/linesOperations.ts +++ b/src/vs/editor/contrib/linesOperations/browser/linesOperations.ts @@ -332,7 +332,13 @@ export class DeleteDuplicateLinesAction extends EditorAction { const uniqueLines = new Set(); const lines = []; - for (let i = selection.startLineNumber; i <= selection.endLineNumber; i++) { + // A selection that ends at the start of a line doesn't include that line + let endLineNumber = selection.endLineNumber; + if (selection.startLineNumber < endLineNumber && selection.endColumn === 1) { + endLineNumber--; + } + + for (let i = selection.startLineNumber; i <= endLineNumber; i++) { const line = model.getLineContent(i); if (uniqueLines.has(line)) { @@ -347,8 +353,8 @@ export class DeleteDuplicateLinesAction extends EditorAction { const selectionToReplace = new Selection( selection.startLineNumber, 1, - selection.endLineNumber, - model.getLineMaxColumn(selection.endLineNumber) + endLineNumber, + model.getLineMaxColumn(endLineNumber) ); const adjustedSelectionStart = selection.startLineNumber - linesDeleted; @@ -362,7 +368,7 @@ export class DeleteDuplicateLinesAction extends EditorAction { edits.push(EditOperation.replace(selectionToReplace, lines.join('\n'))); endCursorState.push(finalSelection); - linesDeleted += (selection.endLineNumber - selection.startLineNumber + 1) - lines.length; + linesDeleted += (endLineNumber - selection.startLineNumber + 1) - lines.length; } editor.pushUndoStop(); diff --git a/src/vs/editor/contrib/linesOperations/test/browser/linesOperations.test.ts b/src/vs/editor/contrib/linesOperations/test/browser/linesOperations.test.ts index b599882be99be5..7213415a831358 100644 --- a/src/vs/editor/contrib/linesOperations/test/browser/linesOperations.test.ts +++ b/src/vs/editor/contrib/linesOperations/test/browser/linesOperations.test.ts @@ -285,6 +285,27 @@ suite('Editor Contrib - Line Operations', () => { assert.deepStrictEqual(model.getLinesContent(), ['alpha', 'beta', 'omicron']); }); }); + + test('does not include the line where the selection ends at column 1', function () { + withTestCodeEditor( + [ + 'alpha', + 'beta', + 'alpha', + 'beta', + 'omicron' + ], {}, (editor) => { + const model = editor.getModel()!; + const deleteDuplicateLinesAction = new DeleteDuplicateLinesAction(); + + editor.setSelection(new Selection(1, 1, 4, 1)); + executeAction(deleteDuplicateLinesAction, editor); + assert.deepStrictEqual({ lines: model.getLinesContent(), selection: editor.getSelection()!.toString() }, { + lines: ['alpha', 'beta', 'beta', 'omicron'], + selection: new Selection(1, 1, 2, 5).toString(), + }); + }); + }); }); From 105e22fdf7906282dd108a97af9fd5e05b4204fc Mon Sep 17 00:00:00 2001 From: Federico Brancasi Date: Sat, 28 Nov 2026 16:01:18 +0100 Subject: [PATCH 2/2] Delete Duplicate Lines: test that the final newline is kept Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../test/browser/linesOperations.test.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/vs/editor/contrib/linesOperations/test/browser/linesOperations.test.ts b/src/vs/editor/contrib/linesOperations/test/browser/linesOperations.test.ts index 7213415a831358..d8d3343286ffb9 100644 --- a/src/vs/editor/contrib/linesOperations/test/browser/linesOperations.test.ts +++ b/src/vs/editor/contrib/linesOperations/test/browser/linesOperations.test.ts @@ -306,6 +306,26 @@ suite('Editor Contrib - Line Operations', () => { }); }); }); + + test('keeps the final newline when applied to the whole document', function () { + withTestCodeEditor( + [ + 'alpha', + '', + 'beta', + 'alpha', + '' + ], {}, (editor) => { + const model = editor.getModel()!; + const deleteDuplicateLinesAction = new DeleteDuplicateLinesAction(); + + executeAction(deleteDuplicateLinesAction, editor); + assert.deepStrictEqual({ lines: model.getLinesContent(), selectionIsEmpty: editor.getSelection()!.isEmpty() }, { + lines: ['alpha', '', 'beta', ''], + selectionIsEmpty: true, + }); + }); + }); });