Skip to content

Commit 662b13a

Browse files
committed
Debounce workspace fetching for workspace structure changes
1 parent b3e086a commit 662b13a

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

crates/rust-analyzer/src/global_state.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
//!
44
//! Each tick provides an immutable snapshot of the state as `WorldSnapshot`.
55
6-
use std::{ops::Not as _, time::Instant};
6+
use std::{
7+
ops::Not as _,
8+
time::{Duration, Instant},
9+
};
710

811
use crossbeam_channel::{Receiver, Sender, unbounded};
912
use hir::ChangeWithProcMacros;
@@ -114,6 +117,9 @@ pub(crate) struct GlobalState {
114117
pub(crate) discover_sender: Sender<discover::DiscoverProjectMessage>,
115118
pub(crate) discover_receiver: Receiver<discover::DiscoverProjectMessage>,
116119

120+
// Debouncing channel for switching
121+
pub(crate) fetch_ws_receiver: Option<(Receiver<Instant>, FetchWorkspaceRequest)>,
122+
117123
// VFS
118124
pub(crate) loader: Handle<Box<dyn vfs::loader::Handle>, Receiver<vfs::loader::Message>>,
119125
pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
@@ -264,6 +270,8 @@ impl GlobalState {
264270
discover_sender,
265271
discover_receiver,
266272

273+
fetch_ws_receiver: None,
274+
267275
vfs: Arc::new(RwLock::new((vfs::Vfs::default(), Default::default()))),
268276
vfs_config_version: 0,
269277
vfs_progress_config_version: 0,
@@ -508,11 +516,12 @@ impl GlobalState {
508516
if let Some((path, force_crate_graph_reload)) = workspace_structure_change {
509517
let _p = span!(Level::INFO, "GlobalState::process_changes/ws_structure_change")
510518
.entered();
511-
512-
self.fetch_workspaces_queue.request_op(
513-
format!("workspace vfs file change: {path}"),
514-
FetchWorkspaceRequest { path: Some(path), force_crate_graph_reload },
515-
);
519+
if self.fetch_workspaces_queue.should_start_op().is_none() {
520+
self.fetch_ws_receiver = Some((
521+
crossbeam_channel::after(Duration::from_millis(50)),
522+
FetchWorkspaceRequest { path: Some(path), force_crate_graph_reload },
523+
));
524+
}
516525
}
517526
}
518527

crates/rust-analyzer/src/main_loop.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
time::{Duration, Instant},
99
};
1010

11-
use crossbeam_channel::{Receiver, select};
11+
use crossbeam_channel::{Receiver, never, select};
1212
use ide_db::base_db::{SourceDatabase, VfsPath, salsa::Database as _};
1313
use lsp_server::{Connection, Notification, Request};
1414
use lsp_types::{TextDocumentIdentifier, notification::Notification as _};
@@ -71,6 +71,7 @@ enum Event {
7171
Flycheck(FlycheckMessage),
7272
TestResult(CargoTestMessage),
7373
DiscoverProject(DiscoverProjectMessage),
74+
SwitchWorkspaces(FetchWorkspaceRequest),
7475
}
7576

7677
impl fmt::Display for Event {
@@ -83,6 +84,7 @@ impl fmt::Display for Event {
8384
Event::QueuedTask(_) => write!(f, "Event::QueuedTask"),
8485
Event::TestResult(_) => write!(f, "Event::TestResult"),
8586
Event::DiscoverProject(_) => write!(f, "Event::DiscoverProject"),
87+
Event::SwitchWorkspaces(_) => write!(f, "Event::SwitchWorkspaces"),
8688
}
8789
}
8890
}
@@ -150,6 +152,7 @@ impl fmt::Debug for Event {
150152
}
151153
_ => (),
152154
}
155+
153156
match self {
154157
Event::Lsp(it) => fmt::Debug::fmt(it, f),
155158
Event::Task(it) => fmt::Debug::fmt(it, f),
@@ -158,6 +161,7 @@ impl fmt::Debug for Event {
158161
Event::Flycheck(it) => fmt::Debug::fmt(it, f),
159162
Event::TestResult(it) => fmt::Debug::fmt(it, f),
160163
Event::DiscoverProject(it) => fmt::Debug::fmt(it, f),
164+
Event::SwitchWorkspaces(_) => Ok(()),
161165
}
162166
}
163167
}
@@ -251,7 +255,7 @@ impl GlobalState {
251255
}
252256

253257
fn next_event(
254-
&self,
258+
&mut self,
255259
inbox: &Receiver<lsp_server::Message>,
256260
) -> Result<Option<Event>, crossbeam_channel::RecvError> {
257261
// Make sure we reply to formatting requests ASAP so the editor doesn't block
@@ -283,6 +287,10 @@ impl GlobalState {
283287

284288
recv(self.discover_receiver) -> task =>
285289
task.map(Event::DiscoverProject),
290+
291+
recv(self.fetch_ws_receiver.as_ref().map_or(&never(), |(chan, _)| chan)) -> _instant => {
292+
Ok(Event::SwitchWorkspaces(self.fetch_ws_receiver.take().unwrap().1))
293+
},
286294
}
287295
.map(Some)
288296
}
@@ -412,6 +420,9 @@ impl GlobalState {
412420
self.handle_discover_msg(message);
413421
}
414422
}
423+
Event::SwitchWorkspaces(req) => {
424+
self.fetch_workspaces_queue.request_op("vfs change".to_owned(), req)
425+
}
415426
}
416427
let event_handling_duration = loop_start.elapsed();
417428
let (state_changed, memdocs_added_or_removed) = if self.vfs_done {

0 commit comments

Comments
 (0)