diff --git a/crates/ra_cargo_watch/src/lib.rs b/crates/ra_cargo_watch/src/lib.rs index 9bc0fd405dbf..cb0856aa43e6 100644 --- a/crates/ra_cargo_watch/src/lib.rs +++ b/crates/ra_cargo_watch/src/lib.rs @@ -58,6 +58,12 @@ impl CheckWatcher { CheckWatcher { task_recv, cmd_send: Some(cmd_send), handle: Some(handle), shared } } + /// Returns a CheckWatcher that doesn't actually do anything + pub fn dummy() -> CheckWatcher { + let shared = Arc::new(RwLock::new(CheckWatcherSharedState::new())); + CheckWatcher { task_recv: never(), cmd_send: None, handle: None, shared } + } + /// Schedule a re-start of the cargo check worker. pub fn update(&self) { if let Some(cmd_send) = &self.cmd_send { diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index 121ddfd1f707..c0175c7266c7 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -132,8 +132,20 @@ impl WorldState { change.set_crate_graph(crate_graph); // FIXME: Figure out the multi-workspace situation - let check_watcher = - CheckWatcher::new(&options.cargo_watch, folder_roots.first().cloned().unwrap()); + let check_watcher = workspaces + .iter() + .find_map(|w| match w { + ProjectWorkspace::Cargo { cargo, .. } => Some(cargo), + ProjectWorkspace::Json { .. } => None, + }) + .map(|cargo| { + let cargo_project_root = cargo.workspace_root().to_path_buf(); + CheckWatcher::new(&options.cargo_watch, cargo_project_root) + }) + .unwrap_or_else(|| { + log::warn!("Cargo check watching only supported for cargo workspaces, disabling"); + CheckWatcher::dummy() + }); let mut analysis_host = AnalysisHost::new(lru_capacity, feature_flags); analysis_host.apply_change(change); diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 1b3c246c7e1f..1832c101f097 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -21,7 +21,7 @@ use crate::Result; pub struct CargoWorkspace { packages: Arena, targets: Arena, - pub(crate) workspace_root: PathBuf, + workspace_root: PathBuf, } #[derive(Deserialize, Clone, Debug, PartialEq, Eq)] @@ -225,4 +225,8 @@ impl CargoWorkspace { pub fn target_by_root(&self, root: &Path) -> Option { self.packages().filter_map(|pkg| pkg.targets(self).find(|it| it.root(self) == root)).next() } + + pub fn workspace_root(&self) -> &Path { + &self.workspace_root + } } diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index b7f6a9b57218..6a104e6f2c82 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -333,7 +333,7 @@ impl ProjectWorkspace { pub fn workspace_root_for(&self, path: &Path) -> Option<&Path> { match self { ProjectWorkspace::Cargo { cargo, .. } => { - Some(cargo.workspace_root.as_ref()).filter(|root| path.starts_with(root)) + Some(cargo.workspace_root()).filter(|root| path.starts_with(root)) } ProjectWorkspace::Json { project: JsonProject { roots, .. } } => roots .iter()