Skip to content

Commit 32357f7

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

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

extensions/vscode/lib/rangeFormatting.ts

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

32+
if (editStart === editEnd) {
33+
result.push(edit);
34+
continue;
35+
}
36+
3237
const trimmedEdit = getTrimmedNewText(document, selectionStart, selectionEnd, edit, editStart, editEnd);
3338
if (trimmedEdit) {
3439
result.push(replace(trimmedEdit.start, trimmedEdit.end, trimmedEdit.newText));
@@ -46,24 +51,14 @@ function getTrimmedNewText(
4651
editStart: number,
4752
editEnd: number,
4853
) {
49-
if (editStart === editEnd) {
50-
return {
51-
start: editStart,
52-
end: editEnd,
53-
newText: edit.newText,
54-
};
55-
}
5654
const oldText = document.getText(edit.range);
5755
let overlapStart = Math.max(editStart, selectionStart) - editStart;
5856
let overlapEnd = Math.min(editEnd, selectionEnd) - editStart;
59-
if (overlapStart === overlapEnd) {
60-
return;
61-
}
62-
6357
let oldTextIndex = 0;
6458
let newTextIndex = 0;
6559
let newStart!: number;
6660
let newEnd!: number;
61+
6762
while (true) {
6863
if (oldTextIndex === overlapStart) {
6964
newStart = newTextIndex;
@@ -78,7 +73,7 @@ function getTrimmedNewText(
7873
}
7974
if (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode)) {
8075
newStart = newTextIndex;
81-
overlapStart -= overlapStart - oldTextIndex;
76+
overlapStart = oldTextIndex;
8277
break;
8378
}
8479
if (isWhitespaceChar(oldCharCode)) {
@@ -91,6 +86,7 @@ function getTrimmedNewText(
9186

9287
oldTextIndex = oldText.length - 1;
9388
newTextIndex = edit.newText.length - 1;
89+
9490
while (true) {
9591
if (oldTextIndex + 1 === overlapEnd) {
9692
newEnd = newTextIndex + 1;
@@ -105,7 +101,7 @@ function getTrimmedNewText(
105101
}
106102
if (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode)) {
107103
newEnd = newTextIndex + 1;
108-
overlapEnd += overlapEnd - oldTextIndex + 1;
104+
overlapEnd = oldTextIndex + 1;
109105
break;
110106
}
111107
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)