Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/vs/editor/contrib/linesOperations/browser/linesOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,47 @@ 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 () {
Comment thread
Copilot marked this conversation as resolved.
Resolved
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(),
});
});
});

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,
});
});
});
});


Expand Down