Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix multi range formatting #180955

Merged
merged 1 commit into from Apr 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 20 additions & 19 deletions src/vs/editor/contrib/format/browser/format.ts
Expand Up @@ -228,32 +228,33 @@ export async function formatDocumentRangesWithProvider(
logService.trace(`[format][provideDocumentRangeFormattingEdits] (response)`, provider.extensionId?.value, result);
rawEditsList.push(result);
} else {

for (const range of ranges) {
if (cts.token.isCancellationRequested) {
return true;
}
rawEditsList.push(await computeEdits(range));
}
}

for (let i = 0; i < ranges.length; ++i) {
for (let j = i + 1; j < ranges.length; ++j) {
if (cts.token.isCancellationRequested) {
return true;
}
if (hasIntersectingEdit(rawEditsList[i], rawEditsList[j])) {
// Merge ranges i and j into a single range, recompute the associated edits
const mergedRange = Range.plusRange(ranges[i], ranges[j]);
const edits = await computeEdits(mergedRange);
ranges.splice(j, 1);
ranges.splice(i, 1);
ranges.push(mergedRange);
rawEditsList.splice(j, 1);
rawEditsList.splice(i, 1);
rawEditsList.push(edits);
// Restart scanning
i = 0;
j = 0;
for (let i = 0; i < ranges.length; ++i) {
for (let j = i + 1; j < ranges.length; ++j) {
if (cts.token.isCancellationRequested) {
return true;
}
if (hasIntersectingEdit(rawEditsList[i], rawEditsList[j])) {
// Merge ranges i and j into a single range, recompute the associated edits
const mergedRange = Range.plusRange(ranges[i], ranges[j]);
const edits = await computeEdits(mergedRange);
ranges.splice(j, 1);
ranges.splice(i, 1);
ranges.push(mergedRange);
rawEditsList.splice(j, 1);
rawEditsList.splice(i, 1);
rawEditsList.push(edits);
// Restart scanning
i = 0;
j = 0;
}
}
}
}
Expand Down