Skip to content

Conversation

@hickford
Copy link
Contributor

@hickford hickford commented Jul 21, 2025

Previously, actions 'reverse lines', 'sort lines' and 'delete duplicate lines' did nothing when the selection starts and ends on the same line. This could be confusing if the user was trying to sort the document and nothing happened.

Instead, apply the action to the whole document. This is more useful.

@ivanstepanovftw

This comment was marked as off-topic.

@mjbvz mjbvz assigned Tyriar and unassigned mjbvz Jul 21, 2025
@bpasero bpasero assigned aiday-mar and unassigned Tyriar Sep 22, 2025
Copilot AI review requested due to automatic review settings December 24, 2025 19:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR changes the behavior of three line operation actions ('reverse lines', 'sort lines ascending/descending', and 'delete duplicate lines') to be more intuitive. Previously, when a selection started and ended on the same line, these actions did nothing. Now, they apply to the entire document when the selection is within a single line, making the actions more useful when users intend to operate on the whole document.

Key Changes

  • Changed condition from isEmpty() to isSingleLine() so actions apply to whole document when selection is on a single line
  • Added isSingleLine() method to Range class to test if a range starts and ends on the same line
  • Updated tests to reflect and validate the new behavior

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/vs/editor/common/core/range.ts Adds isSingleLine() method to Range class that checks if startLineNumber equals endLineNumber
src/vs/monaco.d.ts Adds JSDoc documentation for the new isSingleLine() method in the public API
src/vs/editor/contrib/linesOperations/browser/linesOperations.ts Updates AbstractSortLinesAction, DeleteDuplicateLinesAction, and ReverseLinesAction to use isSingleLine() instead of isEmpty()
src/vs/editor/contrib/linesOperations/test/browser/linesOperations.test.ts Adds tests for the new behavior and updates an existing test description
Comments suppressed due to low confidence (1)

src/vs/editor/contrib/linesOperations/test/browser/linesOperations.test.ts:187

  • Missing test coverage for the new single-line selection behavior. The SortLinesDescendingAction suite should include a test similar to the one added for SortLinesAscendingAction (lines 110-128, though that test has a separate bug) and DeleteDuplicateLinesAction (lines 272-287) to verify that the action applies to the whole document when the selection is a single line.
	suite('SortLinesDescendingAction', () => {
		test('should sort selected lines in descending order', function () {
			withTestCodeEditor(
				[
					'alpha',
					'beta',
					'omicron'
				], {}, (editor) => {
					const model = editor.getModel()!;
					const sortLinesDescendingAction = new SortLinesDescendingAction();

					editor.setSelection(new Selection(1, 1, 3, 7));
					executeAction(sortLinesDescendingAction, editor);
					assert.deepStrictEqual(model.getLinesContent(), [
						'omicron',
						'beta',
						'alpha'
					]);
					assertSelection(editor, new Selection(1, 1, 3, 5));
				});
		});

		test('should sort multiple selections in descending order', function () {
			withTestCodeEditor(
				[
					'alpha',
					'beta',
					'omicron',
					'',
					'alpha',
					'beta',
					'omicron'
				], {}, (editor) => {
					const model = editor.getModel()!;
					const sortLinesDescendingAction = new SortLinesDescendingAction();

					editor.setSelections([new Selection(1, 1, 3, 7), new Selection(5, 1, 7, 7)]);
					executeAction(sortLinesDescendingAction, editor);
					assert.deepStrictEqual(model.getLinesContent(), [
						'omicron',
						'beta',
						'alpha',
						'',
						'omicron',
						'beta',
						'alpha'
					]);
					const expectedSelections = [
						new Selection(1, 1, 3, 5),
						new Selection(5, 1, 7, 5)
					];
					editor.getSelections()!.forEach((actualSelection, index) => {
						assert.deepStrictEqual(actualSelection.toString(), expectedSelections[index].toString());
					});
				});
		});
	});

Comment on lines 110 to 128
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'
]);
});
});
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is using ReverseLinesAction but is placed in the SortLinesAscendingAction suite. It should either test SortLinesAscendingAction or be moved to the ReverseLinesAction suite. Since there's already an identical test in the ReverseLinesAction suite (line 769), this test should be updated to test SortLinesAscendingAction instead.

Copilot uses AI. Check for mistakes.
Likewise for actions 'Delete duplicate lines' and 'Sort lines'
@aiday-mar aiday-mar enabled auto-merge (squash) December 26, 2025 13:35
@vs-code-engineering vs-code-engineering bot added this to the December / January 2026 milestone Dec 26, 2025
@aiday-mar aiday-mar merged commit 1debf21 into microsoft:main Dec 26, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants