From 0364eac4b1cf3a71bcddc2defd124f167fd3d637 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sat, 3 Apr 2021 00:35:49 +0300 Subject: [PATCH] Allow multiple modules from the same crate in fixtures --- crates/base_db/src/fixture.rs | 43 +++++++++++++------ .../ide_assists/src/handlers/auto_import.rs | 5 ++- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs index 8d4641355723..516c91063c6c 100644 --- a/crates/base_db/src/fixture.rs +++ b/crates/base_db/src/fixture.rs @@ -57,7 +57,7 @@ //! fn insert_source_code_here() {} //! " //! ``` -use std::{mem, str::FromStr, sync::Arc}; +use std::{mem, str::FromStr, sync::Arc, collections::hash_map::Entry}; use cfg::CfgOptions; use rustc_hash::FxHashMap; @@ -175,19 +175,34 @@ impl ChangeFixture { if let Some(krate) = meta.krate { let crate_name = CrateName::normalize_dashes(&krate); - let crate_id = crate_graph.add_crate_root( - file_id, - meta.edition, - Some(crate_name.clone().into()), - meta.cfg, - meta.env, - Default::default(), - ); - let prev = crates.insert(crate_name.clone(), crate_id); - assert!(prev.is_none()); - for dep in meta.deps { - let dep = CrateName::normalize_dashes(&dep); - crate_deps.push((crate_name.clone(), dep)) + match crates.entry(crate_name.clone()) { + Entry::Occupied(_) => { + assert_eq!( + crate_deps, + meta.deps + .iter() + .map(|dep| (crate_name.clone(), CrateName::normalize_dashes(dep))) + .collect::>(), + "Crate {} has two modules with different dependencies in metadata", + krate, + ) + } + Entry::Vacant(v) => { + let new_crate_id = crate_graph.add_crate_root( + file_id, + meta.edition, + Some(crate_name.clone().into()), + meta.cfg, + meta.env, + Default::default(), + ); + v.insert(new_crate_id); + + for dep in meta.deps { + let dep = CrateName::normalize_dashes(&dep); + crate_deps.push((crate_name.clone(), dep)) + } + } } } else if meta.path == "/main.rs" || meta.path == "/lib.rs" { assert!(default_crate_root.is_none()); diff --git a/crates/ide_assists/src/handlers/auto_import.rs b/crates/ide_assists/src/handlers/auto_import.rs index 5ccd7f7a2c63..737f2f3e5fb3 100644 --- a/crates/ide_assists/src/handlers/auto_import.rs +++ b/crates/ide_assists/src/handlers/auto_import.rs @@ -918,6 +918,9 @@ fn main() { auto_import, r" //- /lib.rs crate:dep +pub mod formatters; + +//- /formatters.rs crate:dep pub struct FMT; pub struct fmt; @@ -926,7 +929,7 @@ fn main() { FMT$0; } ", - r"use dep::FMT; + r"use dep::formatters::FMT; fn main() { FMT;