Fix race condition between auto-indent and on-type-formatting (#32005)

This PR addresses to fix (#31308) a race condition where auto-indent (in
buffer.cs) and on-type-formatting (in lsp_store.rs) concurrently
calculate indentation using the same buffer snapshot.

Previous Solution (Abandoned): 
https://github.com/zed-industries/zed/pull/31340

Final Solution:
Delay applying on-type-formatting until auto-indent is complete.

Issue:

If AutoindentMode finishes first, formatting works correctly. If
"Formatting on typing" starts before AutoindentMode completes, it
results in double indentation.

Closes #31308

Release Notes:

- Fixed a race condition resulting in incorrect buffer contents when combining auto-indent and on-type-formatting
This commit is contained in:
Maxim 2025-06-24 00:59:06 +03:00 committed by GitHub
parent d34d4f2ef1
commit 36eebb7ba8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 97 additions and 14 deletions

View file

@ -5098,17 +5098,30 @@ impl LspStore {
.as_ref(),
)
});
self.request_lsp(
buffer.clone(),
LanguageServerToQuery::FirstCapable,
OnTypeFormatting {
position,
trigger,
options,
push_to_history,
},
cx,
)
cx.spawn(async move |this, cx| {
if let Some(waiter) =
buffer.update(cx, |buffer, _| buffer.wait_for_autoindent_applied())?
{
waiter.await?;
}
cx.update(|cx| {
this.update(cx, |this, cx| {
this.request_lsp(
buffer.clone(),
LanguageServerToQuery::FirstCapable,
OnTypeFormatting {
position,
trigger,
options,
push_to_history,
},
cx,
)
})
})??
.await
})
}
pub fn code_actions(