fix(formatting/#2196): Fix buffer de-sync when applying formatting edits #3233
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.
Issue: When running format providers, like
prettier
orrescript
, after multiple iterations, there could start to be duplicated or corrupted text.Defect: Some of the edits provided by the format provider, once they round-trip through our vim layer, weren't being applied back correctly in the extension host buffer updates (the
ModelContentChange
that we send to the extension host layer) - this would cause the buffer state to be de-sync'd - meaning that the view of the buffer text was different in the extension host than in the editor. The editor was correct, but the buffer text on the extension host side would have extra lines or extra concatenation, and when the next format was triggered, the formatting edits for the desync'd buffer would be applied, causing the issues in #2196Currently, the way we send buffer updates is always linewise - and therefore a newline must always be included at the end. In the particular case of a formatting edit consolidating lines (deleting empty spaces), we wouldn't be adding that trailing newline - and this was the root cause of the desync.
While investigating, I found another case with undo - creating multiple lines, and then undoing them, triggers a similar bug.
Fix: Simplify the way we decide to append a newline to correct these issues. Add a test case exercising the undo condition (once we have
$tryApplyEdit
, it'd be great to have that API exercised in a text synchronization case as well).In addition, add some extra tooling in our
oni-dev-extension
to support troubleshooting these issues - when running withoni2 -f --silent --debug-exthost
and running theDeveloper: Show buffer updates
command, the extension host's view of the buffer will be shown in the console after each update.Next steps:
Fixes #2196 and the remainder of #2820