Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions extensions/vscode/lib/rangeFormatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function getTrimmedNewText(
};
}
const oldText = document.getText(edit.range);
const overlapStart = Math.max(editStart, selectionStart) - editStart;
const overlapEnd = Math.min(editEnd, selectionEnd) - editStart;
let overlapStart = Math.max(editStart, selectionStart) - editStart;
let overlapEnd = Math.min(editEnd, selectionEnd) - editStart;
if (overlapStart === overlapEnd) {
return;
}
Expand All @@ -64,19 +64,23 @@ function getTrimmedNewText(
let newTextIndex = 0;
let newStart!: number;
let newEnd!: number;

while (true) {
if (oldTextIndex === overlapStart) {
newStart = newTextIndex;
break;
}
const oldCharCode = oldText.charCodeAt(oldTextIndex);
const newCharCode = edit.newText.charCodeAt(newTextIndex);
if (oldCharCode === newCharCode || (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode))) {
if (oldCharCode === newCharCode) {
oldTextIndex++;
newTextIndex++;
continue;
}
if (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode)) {
newStart = newTextIndex;
overlapStart -= overlapStart - oldTextIndex;
break;
}
if (isWhitespaceChar(oldCharCode)) {
oldTextIndex++;
}
Expand All @@ -94,11 +98,16 @@ function getTrimmedNewText(
}
const oldCharCode = oldText.charCodeAt(oldTextIndex);
const newCharCode = edit.newText.charCodeAt(newTextIndex);
if (oldCharCode === newCharCode || (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode))) {
if (oldCharCode === newCharCode) {
oldTextIndex--;
newTextIndex--;
continue;
}
if (!isWhitespaceChar(oldCharCode) && !isWhitespaceChar(newCharCode)) {
newEnd = newTextIndex + 1;
overlapEnd += overlapEnd - oldTextIndex + 1;
break;
}
if (isWhitespaceChar(oldCharCode)) {
oldTextIndex--;
}
Expand Down
6 changes: 3 additions & 3 deletions extensions/vscode/tests/rangeFormatting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('provideDocumentRangeFormattingEdits', () => {
const selection = createRange(1, 5);
const edits = [createTextEdit(0, 5, '_BCDE')];
const result = restrictFormattingEditsToRange(document, selection, edits, createTextEdit);
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"0BCDE5"`);
expect(applyEdits(document, result)).toMatchInlineSnapshot(`"_BCDE5"`);
});

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

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

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