Prioritize formatting thread tasks in main_loop

This commit is contained in:
Lukas Wirth 2025-01-27 18:23:42 +01:00
parent 35b55fd67f
commit ec06a9d1d4
2 changed files with 14 additions and 9 deletions

View file

@ -118,7 +118,7 @@ impl RequestDispatcher<'_> {
} }
return self; return self;
} }
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>( self.on_with_thread_intent::<false, ALLOW_RETRYING, R>(
ThreadIntent::Worker, ThreadIntent::Worker,
f, f,
Self::content_modified_error, Self::content_modified_error,
@ -147,7 +147,7 @@ impl RequestDispatcher<'_> {
} }
return self; return self;
} }
self.on_with_thread_intent::<true, false, R>(ThreadIntent::Worker, f, on_cancelled) self.on_with_thread_intent::<false, false, R>(ThreadIntent::Worker, f, on_cancelled)
} }
/// Dispatches a non-latency-sensitive request onto the thread pool. When the VFS is marked not /// Dispatches a non-latency-sensitive request onto the thread pool. When the VFS is marked not
@ -166,7 +166,7 @@ impl RequestDispatcher<'_> {
} }
return self; return self;
} }
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>( self.on_with_thread_intent::<false, ALLOW_RETRYING, R>(
ThreadIntent::Worker, ThreadIntent::Worker,
f, f,
Self::content_modified_error, Self::content_modified_error,
@ -193,7 +193,7 @@ impl RequestDispatcher<'_> {
} }
return self; return self;
} }
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>( self.on_with_thread_intent::<false, ALLOW_RETRYING, R>(
ThreadIntent::LatencySensitive, ThreadIntent::LatencySensitive,
f, f,
Self::content_modified_error, Self::content_modified_error,
@ -212,7 +212,7 @@ impl RequestDispatcher<'_> {
R::Params: DeserializeOwned + panic::UnwindSafe + Send + fmt::Debug, R::Params: DeserializeOwned + panic::UnwindSafe + Send + fmt::Debug,
R::Result: Serialize, R::Result: Serialize,
{ {
self.on_with_thread_intent::<false, false, R>( self.on_with_thread_intent::<true, false, R>(
ThreadIntent::LatencySensitive, ThreadIntent::LatencySensitive,
f, f,
Self::content_modified_error, Self::content_modified_error,
@ -231,7 +231,7 @@ impl RequestDispatcher<'_> {
} }
} }
fn on_with_thread_intent<const MAIN_POOL: bool, const ALLOW_RETRYING: bool, R>( fn on_with_thread_intent<const RUSTFMT: bool, const ALLOW_RETRYING: bool, R>(
&mut self, &mut self,
intent: ThreadIntent, intent: ThreadIntent,
f: fn(GlobalStateSnapshot, R::Params) -> anyhow::Result<R::Result>, f: fn(GlobalStateSnapshot, R::Params) -> anyhow::Result<R::Result>,
@ -251,10 +251,10 @@ impl RequestDispatcher<'_> {
tracing::debug!(?params); tracing::debug!(?params);
let world = self.global_state.snapshot(); let world = self.global_state.snapshot();
if MAIN_POOL { if RUSTFMT {
&mut self.global_state.task_pool.handle
} else {
&mut self.global_state.fmt_pool.handle &mut self.global_state.fmt_pool.handle
} else {
&mut self.global_state.task_pool.handle
} }
.spawn(intent, move || { .spawn(intent, move || {
let result = panic::catch_unwind(move || { let result = panic::catch_unwind(move || {

View file

@ -253,6 +253,11 @@ impl GlobalState {
&self, &self,
inbox: &Receiver<lsp_server::Message>, inbox: &Receiver<lsp_server::Message>,
) -> Result<Option<Event>, crossbeam_channel::RecvError> { ) -> Result<Option<Event>, crossbeam_channel::RecvError> {
// Make sure we reply to formatting requests ASAP so the editor doesn't block
if let Ok(task) = self.fmt_pool.receiver.try_recv() {
return Ok(Some(Event::Task(task)));
}
select! { select! {
recv(inbox) -> msg => recv(inbox) -> msg =>
return Ok(msg.ok().map(Event::Lsp)), return Ok(msg.ok().map(Event::Lsp)),