Reverse lines: apply to whole document when selection is single line (#257031)
Some checks failed
Monaco Editor checks / Monaco Editor checks (push) Has been cancelled
Code OSS (node_modules) / Compile (push) Has been cancelled
Code OSS (node_modules) / Linux (push) Has been cancelled
Code OSS (node_modules) / macOS (push) Has been cancelled
Code OSS (node_modules) / Windows (push) Has been cancelled

Likewise for actions 'Delete duplicate lines' and 'Sort lines'
This commit is contained in:
M Hickford 2025-12-26 18:41:10 +00:00 committed by GitHub
parent 4a433b1fec
commit 1debf21160
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 68 additions and 6 deletions

View File

@ -364,6 +364,9 @@ export class Range {
return new Range(this.startLineNumber + lineCount, this.startColumn, this.endLineNumber + lineCount, this.endColumn);
}
/**
* Test if this range starts and ends on the same line.
*/
public isSingleLine(): boolean {
return this.startLineNumber === this.endLineNumber;
}

View File

@ -251,7 +251,7 @@ export abstract class AbstractSortLinesAction extends EditorAction {
const model = editor.getModel();
let selections = editor.getSelections();
if (selections.length === 1 && selections[0].isEmpty()) {
if (selections.length === 1 && selections[0].isSingleLine()) {
// Apply to whole document.
selections = [new Selection(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount()))];
}
@ -322,7 +322,7 @@ export class DeleteDuplicateLinesAction extends EditorAction {
let updateSelection = true;
let selections = editor.getSelections();
if (selections.length === 1 && selections[0].isEmpty()) {
if (selections.length === 1 && selections[0].isSingleLine()) {
// Apply to whole document.
selections = [new Selection(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount()))];
updateSelection = false;
@ -389,7 +389,7 @@ export class ReverseLinesAction extends EditorAction {
const model: ITextModel = editor.getModel();
const originalSelections = editor.getSelections();
let selections = originalSelections;
if (selections.length === 1 && selections[0].isEmpty()) {
if (selections.length === 1 && selections[0].isSingleLine()) {
// Apply to whole document.
selections = [new Selection(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount()))];
}

View File

@ -106,6 +106,26 @@ suite('Editor Contrib - Line Operations', () => {
});
});
});
test('applies to whole document when selection is single line', function () {
withTestCodeEditor(
[
'omicron',
'beta',
'alpha'
], {}, (editor) => {
const model = editor.getModel()!;
const sortLinesAscendingAction = new SortLinesAscendingAction();
editor.setSelection(new Selection(2, 1, 2, 4));
executeAction(sortLinesAscendingAction, editor);
assert.deepStrictEqual(model.getLinesContent(), [
'alpha',
'beta',
'omicron'
]);
});
});
});
suite('SortLinesDescendingAction', () => {
@ -248,6 +268,23 @@ suite('Editor Contrib - Line Operations', () => {
});
});
});
test('applies to whole document when selection is single line', function () {
withTestCodeEditor(
[
'alpha',
'beta',
'alpha',
'omicron'
], {}, (editor) => {
const model = editor.getModel()!;
const deleteDuplicateLinesAction = new DeleteDuplicateLinesAction();
editor.setSelection(new Selection(2, 1, 2, 2));
executeAction(deleteDuplicateLinesAction, editor);
assert.deepStrictEqual(model.getLinesContent(), ['alpha', 'beta', 'omicron']);
});
});
});
@ -729,7 +766,7 @@ suite('Editor Contrib - Line Operations', () => {
});
});
test('handles single line selection', function () {
test('applies to whole document when selection is single line', function () {
withTestCodeEditor(
[
'line1',
@ -742,8 +779,7 @@ suite('Editor Contrib - Line Operations', () => {
// Select only line 2
editor.setSelection(new Selection(2, 1, 2, 6));
executeAction(reverseLinesAction, editor);
// Single line should remain unchanged
assert.deepStrictEqual(model.getLinesContent(), ['line1', 'line2', 'line3']);
assert.deepStrictEqual(model.getLinesContent(), ['line3', 'line2', 'line1']);
});
});
@ -765,6 +801,26 @@ suite('Editor Contrib - Line Operations', () => {
assert.deepStrictEqual(model.getLinesContent(), ['line1', 'line3', 'line2', 'line4', 'line5']);
});
});
test('applies to whole document when selection is single line', function () {
withTestCodeEditor(
[
'omicron',
'beta',
'alpha'
], {}, (editor) => {
const model = editor.getModel()!;
const reverseLinesAction = new ReverseLinesAction();
editor.setSelection(new Selection(2, 1, 2, 4));
executeAction(reverseLinesAction, editor);
assert.deepStrictEqual(model.getLinesContent(), [
'alpha',
'beta',
'omicron'
]);
});
});
});
test('transpose', () => {

3
src/vs/monaco.d.ts vendored
View File

@ -771,6 +771,9 @@ declare namespace monaco {
* Moves the range by the given amount of lines.
*/
delta(lineCount: number): Range;
/**
* Test if this range starts and ends on the same line.
*/
isSingleLine(): boolean;
static fromPositions(start: IPosition, end?: IPosition): Range;
/**