Skip to content

Commit 45c9591

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

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

extensions/vscode/lib/rangeFormatting.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,11 @@ function getTrimmedNewText(
5656
const oldText = document.getText(edit.range);
5757
let overlapStart = Math.max(editStart, selectionStart) - editStart;
5858
let overlapEnd = Math.min(editEnd, selectionEnd) - editStart;
59-
if (overlapStart === overlapEnd) {
60-
return;
61-
}
62-
6359
let oldTextIndex = 0;
6460
let newTextIndex = 0;
6561
let newStart!: number;
6662
let newEnd!: number;
63+
6764
while (true) {
6865
if (oldTextIndex === overlapStart) {
6966
newStart = newTextIndex;
@@ -78,7 +75,7 @@ function getTrimmedNewText(
7875
}
7976
if (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode)) {
8077
newStart = newTextIndex;
81-
overlapStart -= overlapStart - oldTextIndex;
78+
overlapStart = oldTextIndex;
8279
break;
8380
}
8481
if (isWhitespaceChar(oldCharCode)) {
@@ -91,6 +88,7 @@ function getTrimmedNewText(
9188

9289
oldTextIndex = oldText.length - 1;
9390
newTextIndex = edit.newText.length - 1;
91+
9492
while (true) {
9593
if (oldTextIndex + 1 === overlapEnd) {
9694
newEnd = newTextIndex + 1;
@@ -105,7 +103,7 @@ function getTrimmedNewText(
105103
}
106104
if (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode)) {
107105
newEnd = newTextIndex + 1;
108-
overlapEnd += overlapEnd - oldTextIndex + 1;
106+
overlapEnd = oldTextIndex + 1;
109107
break;
110108
}
111109
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)