Repository navigation
perf: only edit what changed when formatting a document - #182
Merged
Merged
Conversation
Member
Author
|
A second agent reviewed this PR. It found no input where the edits give the wrong text (5.4 million exhaustive small pairs, 120,000 random pairs with mixed line endings, lone
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Format Document replaced the whole document with one edit. VS Code reduces an extension's edits to what changed on its own, but only for text up to 100,000 characters. Above that the whole-document edit is applied as-is, which moves the cursor and loses selections and folded regions.
Changes
FolderServicereturns only the edits for what changed instead of a single whole-document edit. This applies to Format Document, notebook cells, and the global config commands, for text of any length so that the same code runs for every file. For text up to 100,000 characters this made no measurable difference to the time of Format Document in VS Code 1.140 (84,000 characters: 7.7 ms before and 6.2 ms after with 1 changed line, 379 ms both ways with every line changed, where the time is VS Code's own diff). Format Selection is unchanged.src/legacy/minimalEdits.ts(new) computes those edits and is written to be fast on large text where a small part changed:\r\nor a surrogate pair.normalizeToSourceLineEndingsnow checks if the text needs converting before creating a new string.Performance
Median of 20 runs on my machine (Node 24, Windows). "Before" is the first version of this PR, which numbered every line between the first and last change and compared the start and end of the text one code unit at a time.
The CRLF row is the time to convert the line endings of 11 MB of formatted text, which only happens when a plugin's output has different line endings than the document.
Verification
minimalEdits.test.ts, including seeded tests of random changes (LF, CRLF and mixed line endings, lone carriage returns and surrogates, with and without a final line break, short and long texts, few distinct lines so that lines match by chance, and groups of changes that exhaust the limits) that apply the edits and compare the result to the formatted text.FolderService.npm run test:unitandnpm testpass locally on Windows.