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(formatting/#2196): Fix buffer de-sync when applying formatting edits #3233

merged 14 commits into from Mar 5, 2021


Copy link

@bryphe bryphe commented Mar 4, 2021

Issue: When running format providers, like prettier or rescript, 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 #2196

Currently, 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 with oni2 -f --silent --debug-exthost and running the Developer: Show buffer updates command, the extension host's view of the buffer will be shown in the console after each update.

Next steps:

  • There's still a lot of room for improvement in the way we handle buffer updates - streamlining individual edits, and batched related updates into a single update call.

Fixes #2196 and the remainder of #2820

@bryphe bryphe added the WIP label Mar 4, 2021
@bryphe bryphe removed the WIP label Mar 5, 2021
@bryphe bryphe merged commit d75c5a7 into master Mar 5, 2021
@bryphe bryphe deleted the fix/formatting/2196/buffer-desync branch March 5, 2021 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

Formatting(in my case via Prettier extension) start duplicate text after approx 4 uses
1 participant