Skip to content

Commit 63ed6d3

Browse files
committed
fix(vscode): handle empty selection in getTrimmedNewText
1 parent a8a758a commit 63ed6d3

File tree

2 files changed

+12
-27
lines changed

2 files changed

+12
-27
lines changed

extensions/vscode/lib/rangeFormatting.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ export function restrictFormattingEditsToRange(
2929
continue;
3030
}
3131

32-
const trimmedEdit = getTrimmedNewText(document, selectionStart, selectionEnd, edit, editStart, editEnd);
33-
if (trimmedEdit) {
34-
result.push(replace(trimmedEdit.start, trimmedEdit.end, trimmedEdit.newText));
32+
if (editStart === editEnd) {
33+
result.push(edit);
34+
continue;
3535
}
36+
37+
const trimmedEdit = getTrimmedNewText(document, selectionStart, selectionEnd, edit, editStart, editEnd);
38+
result.push(replace(trimmedEdit.start, trimmedEdit.end, trimmedEdit.newText));
3639
}
3740

3841
return result;
@@ -46,24 +49,14 @@ function getTrimmedNewText(
4649
editStart: number,
4750
editEnd: number,
4851
) {
49-
if (editStart === editEnd) {
50-
return {
51-
start: editStart,
52-
end: editEnd,
53-
newText: edit.newText,
54-
};
55-
}
5652
const oldText = document.getText(edit.range);
5753
let overlapStart = Math.max(editStart, selectionStart) - editStart;
5854
let overlapEnd = Math.min(editEnd, selectionEnd) - editStart;
59-
if (overlapStart === overlapEnd) {
60-
return;
61-
}
62-
6355
let oldTextIndex = 0;
6456
let newTextIndex = 0;
6557
let newStart!: number;
6658
let newEnd!: number;
59+
6760
while (true) {
6861
if (oldTextIndex === overlapStart) {
6962
newStart = newTextIndex;
@@ -78,7 +71,7 @@ function getTrimmedNewText(
7871
}
7972
if (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode)) {
8073
newStart = newTextIndex;
81-
overlapStart -= overlapStart - oldTextIndex;
74+
overlapStart = oldTextIndex;
8275
break;
8376
}
8477
if (isWhitespaceChar(oldCharCode)) {
@@ -91,6 +84,7 @@ function getTrimmedNewText(
9184

9285
oldTextIndex = oldText.length - 1;
9386
newTextIndex = edit.newText.length - 1;
87+
9488
while (true) {
9589
if (oldTextIndex + 1 === overlapEnd) {
9690
newEnd = newTextIndex + 1;
@@ -105,7 +99,7 @@ function getTrimmedNewText(
10599
}
106100
if (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode)) {
107101
newEnd = newTextIndex + 1;
108-
overlapEnd += overlapEnd - oldTextIndex + 1;
102+
overlapEnd = oldTextIndex + 1;
109103
break;
110104
}
111105
if (isWhitespaceChar(oldCharCode)) {

extensions/vscode/tests/rangeFormatting.spec.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('provideDocumentRangeFormattingEdits', () => {
7373
createTextEdit(2, 2, 'X'),
7474
];
7575
const result = restrictFormattingEditsToRange(document, selection, edits, createTextEdit);
76-
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"01X23456789"`);
76+
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"01X234Z6789"`);
7777
});
7878

7979
test('handles deletion where newText is shorter than oldText in selection', () => {
@@ -129,7 +129,7 @@ describe('provideDocumentRangeFormattingEdits', () => {
129129
const selection = createRange(5, 5); // empty selection at position 5
130130
const edits = [createTextEdit(3, 7, 'ABCD')];
131131
const result = restrictFormattingEditsToRange(document, selection, edits, createTextEdit);
132-
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"0123456789"`);
132+
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"012ABCD789"`);
133133
});
134134

135135
test('handles empty edit (pure insertion)', () => {
@@ -166,15 +166,6 @@ describe('provideDocumentRangeFormattingEdits', () => {
166166
const result = restrictFormattingEditsToRange(document, selection, edits, createTextEdit);
167167
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"你好朋友"`);
168168
});
169-
170-
test('handles overlapStart equals overlapEnd', () => {
171-
// When edit and selection don't actually overlap in content
172-
const document = createDocument('0123456789');
173-
const selection = createRange(5, 5);
174-
const edits = [createTextEdit(3, 7, 'WXYZ')];
175-
const result = restrictFormattingEditsToRange(document, selection, edits, createTextEdit);
176-
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"0123456789"`);
177-
});
178169
});
179170

180171
// self implementation of vscode test utils

0 commit comments

Comments
 (0)