From aca9aa8b7e7f363779aeba1dfc60397f46b9544d Mon Sep 17 00:00:00 2001 From: veetaha Date: Sat, 9 May 2020 02:24:51 +0300 Subject: [PATCH 1/2] Simplify --- crates/rust-analyzer/src/main_loop.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index b77f0c5a9c..17b0b95b9d 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -96,23 +96,21 @@ pub fn main_loop(ws_roots: Vec, config: Config, connection: Connection) let mut world_state = { let workspaces = { // FIXME: support dynamic workspace loading. - let mut visited = FxHashSet::default(); - let project_roots = ws_roots + let project_roots: FxHashSet<_> = ws_roots .iter() .filter_map(|it| ra_project_model::ProjectRoot::discover(it).ok()) .flatten() - .filter(|it| visited.insert(it.clone())) - .collect::>(); + .collect(); if project_roots.is_empty() && config.notifications.cargo_toml_not_found { show_message( - req::MessageType::Error, - format!( - "rust-analyzer failed to discover workspace, no Cargo.toml found, dirs searched: {}", - ws_roots.iter().format_with(", ", |it, f| f(&it.display())) - ), - &connection.sender, - ); + req::MessageType::Error, + format!( + "rust-analyzer failed to discover workspace, no Cargo.toml found, dirs searched: {}", + ws_roots.iter().format_with(", ", |it, f| f(&it.display())) + ), + &connection.sender, + ); }; project_roots From e83a2912b8deaab560d1ea39232c06a29530d6e5 Mon Sep 17 00:00:00 2001 From: veetaha Date: Sat, 9 May 2020 02:51:59 +0300 Subject: [PATCH 2/2] Simpify project discovery --- crates/ra_project_model/src/lib.rs | 53 +++++++++--------------------- 1 file changed, 16 insertions(+), 37 deletions(-) diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 3adb8baf6a..4f098b706b 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -88,46 +88,28 @@ impl ProjectRoot { } pub fn discover(path: &Path) -> io::Result> { - if let Some(project_json) = find_rust_project_json(path) { + if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") { return Ok(vec![ProjectRoot::ProjectJson(project_json)]); } return find_cargo_toml(path) .map(|paths| paths.into_iter().map(ProjectRoot::CargoToml).collect()); - fn find_rust_project_json(path: &Path) -> Option { - if path.ends_with("rust-project.json") { - return Some(path.to_path_buf()); - } - - let mut curr = Some(path); - while let Some(path) = curr { - let candidate = path.join("rust-project.json"); - if candidate.exists() { - return Some(candidate); - } - curr = path.parent(); - } - - None - } - fn find_cargo_toml(path: &Path) -> io::Result> { - if path.ends_with("Cargo.toml") { - return Ok(vec![path.to_path_buf()]); + match find_in_parent_dirs(path, "Cargo.toml") { + Some(it) => Ok(vec![it]), + None => Ok(find_cargo_toml_in_child_dir(read_dir(path)?)), } - - if let Some(p) = find_cargo_toml_in_parent_dir(path) { - return Ok(vec![p]); - } - - let entities = read_dir(path)?; - Ok(find_cargo_toml_in_child_dir(entities)) } - fn find_cargo_toml_in_parent_dir(path: &Path) -> Option { + fn find_in_parent_dirs(path: &Path, target_file_name: &str) -> Option { + if path.ends_with(target_file_name) { + return Some(path.to_owned()); + } + let mut curr = Some(path); + while let Some(path) = curr { - let candidate = path.join("Cargo.toml"); + let candidate = path.join(target_file_name); if candidate.exists() { return Some(candidate); } @@ -139,14 +121,11 @@ impl ProjectRoot { fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec { // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects - let mut valid_canditates = vec![]; - for entity in entities.filter_map(Result::ok) { - let candidate = entity.path().join("Cargo.toml"); - if candidate.exists() { - valid_canditates.push(candidate) - } - } - valid_canditates + entities + .filter_map(Result::ok) + .map(|it| it.path().join("Cargo.toml")) + .filter(|it| it.exists()) + .collect() } } }