Skip to content

Commit a8a758a

Browse files
feat(vscode): conservatively modify formatting ranges (#5851)
1 parent 704b30a commit a8a758a

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

extensions/vscode/lib/rangeFormatting.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ function getTrimmedNewText(
5454
};
5555
}
5656
const oldText = document.getText(edit.range);
57-
const overlapStart = Math.max(editStart, selectionStart) - editStart;
58-
const overlapEnd = Math.min(editEnd, selectionEnd) - editStart;
57+
let overlapStart = Math.max(editStart, selectionStart) - editStart;
58+
let overlapEnd = Math.min(editEnd, selectionEnd) - editStart;
5959
if (overlapStart === overlapEnd) {
6060
return;
6161
}
@@ -64,19 +64,23 @@ function getTrimmedNewText(
6464
let newTextIndex = 0;
6565
let newStart!: number;
6666
let newEnd!: number;
67-
6867
while (true) {
6968
if (oldTextIndex === overlapStart) {
7069
newStart = newTextIndex;
7170
break;
7271
}
7372
const oldCharCode = oldText.charCodeAt(oldTextIndex);
7473
const newCharCode = edit.newText.charCodeAt(newTextIndex);
75-
if (oldCharCode === newCharCode || (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode))) {
74+
if (oldCharCode === newCharCode) {
7675
oldTextIndex++;
7776
newTextIndex++;
7877
continue;
7978
}
79+
if (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode)) {
80+
newStart = newTextIndex;
81+
overlapStart -= overlapStart - oldTextIndex;
82+
break;
83+
}
8084
if (isWhitespaceChar(oldCharCode)) {
8185
oldTextIndex++;
8286
}
@@ -94,11 +98,16 @@ function getTrimmedNewText(
9498
}
9599
const oldCharCode = oldText.charCodeAt(oldTextIndex);
96100
const newCharCode = edit.newText.charCodeAt(newTextIndex);
97-
if (oldCharCode === newCharCode || (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode))) {
101+
if (oldCharCode === newCharCode) {
98102
oldTextIndex--;
99103
newTextIndex--;
100104
continue;
101105
}
106+
if (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode)) {
107+
newEnd = newTextIndex + 1;
108+
overlapEnd += overlapEnd - oldTextIndex + 1;
109+
break;
110+
}
102111
if (isWhitespaceChar(oldCharCode)) {
103112
oldTextIndex--;
104113
}

extensions/vscode/tests/rangeFormatting.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('provideDocumentRangeFormattingEdits', () => {
88
const selection = createRange(1, 5);
99
const edits = [createTextEdit(0, 5, '_BCDE')];
1010
const result = restrictFormattingEditsToRange(document, selection, edits, createTextEdit);
11-
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"0BCDE5"`);
11+
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"_BCDE5"`);
1212
});
1313

1414
test('keeps indent when edits start on previous line', () => {
@@ -89,7 +89,7 @@ describe('provideDocumentRangeFormattingEdits', () => {
8989
const selection = createRange(1, 5); // select "bcde"
9090
const edits = [createTextEdit(0, 6, 'ab')]; // replace all with just "ab"
9191
const result = restrictFormattingEditsToRange(document, selection, edits, createTextEdit);
92-
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"af"`);
92+
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"ab"`);
9393
});
9494

9595
test('handles insertion where newText is longer than oldText', () => {
@@ -164,7 +164,7 @@ describe('provideDocumentRangeFormattingEdits', () => {
164164
const selection = createRange(1, 3); // select "好世"
165165
const edits = [createTextEdit(0, 4, '你好朋友')];
166166
const result = restrictFormattingEditsToRange(document, selection, edits, createTextEdit);
167-
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"你好朋界"`);
167+
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"你好朋友"`);
168168
});
169169

170170
test('handles overlapStart equals overlapEnd', () => {

0 commit comments

Comments
 (0)