]> git.lizzy.rs Git - rust.git/blobdiff - crates/base_db/src/fixture.rs
Improve completion of cfg attributes
[rust.git] / crates / base_db / src / fixture.rs
index 1b17db102c2fabca294f80f6d13a58c182d16162..6d3b1266e6fd99b2b97d301d77947fb80d52a9fd 100644 (file)
@@ -9,8 +9,8 @@
 use vfs::{file_set::FileSet, VfsPath};
 
 use crate::{
-    input::CrateName, Change, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, FileRange,
-    SourceDatabaseExt, SourceRoot, SourceRootId,
+    input::CrateName, Change, CrateDisplayName, CrateGraph, CrateId, Edition, Env, FileId,
+    FilePosition, FileRange, SourceDatabaseExt, SourceRoot, SourceRootId,
 };
 
 pub const WORKSPACE: SourceRootId = SourceRootId(0);
@@ -81,7 +81,7 @@ pub struct ChangeFixture {
 
 impl ChangeFixture {
     pub fn parse(ra_fixture: &str) -> ChangeFixture {
-        let fixture = Fixture::parse(ra_fixture);
+        let (mini_core, fixture) = Fixture::parse(ra_fixture);
         let mut change = Change::new();
 
         let mut files = Vec::new();
@@ -106,7 +106,7 @@ pub fn parse(ra_fixture: &str) -> ChangeFixture {
                     let (range_or_offset, text) = extract_range_or_offset(&entry.text);
                     assert!(file_position.is_none());
                     file_position = Some((file_id, range_or_offset));
-                    text.to_string()
+                    text
                 }
             } else {
                 entry.text.clone()
@@ -114,6 +114,9 @@ pub fn parse(ra_fixture: &str) -> ChangeFixture {
 
             let meta = FileMeta::from(entry);
             assert!(meta.path.starts_with(&source_root_prefix));
+            if !meta.deps.is_empty() {
+                assert!(meta.krate.is_some(), "can't specify deps without naming the crate")
+            }
 
             if meta.introduce_new_source_root {
                 roots.push(SourceRoot::new_local(mem::take(&mut file_set)));
@@ -128,6 +131,7 @@ pub fn parse(ra_fixture: &str) -> ChangeFixture {
                     meta.cfg,
                     meta.env,
                     Default::default(),
+                    Default::default(),
                 );
                 let prev = crates.insert(crate_name.clone(), crate_id);
                 assert!(prev.is_none());
@@ -157,6 +161,7 @@ pub fn parse(ra_fixture: &str) -> ChangeFixture {
                 default_cfg,
                 Env::default(),
                 Default::default(),
+                Default::default(),
             );
         } else {
             for (from, to) in crate_deps {
@@ -166,6 +171,31 @@ pub fn parse(ra_fixture: &str) -> ChangeFixture {
             }
         }
 
+        if let Some(mini_core) = mini_core {
+            let core_file = file_id;
+            file_id.0 += 1;
+
+            let mut fs = FileSet::default();
+            fs.insert(core_file, VfsPath::new_virtual_path("/sysroot/core/lib.rs".to_string()));
+            roots.push(SourceRoot::new_library(fs));
+
+            change.change_file(core_file, Some(Arc::new(mini_core.source_code())));
+
+            let all_crates = crate_graph.crates_in_topological_order();
+
+            let core_crate = crate_graph.add_crate_root(
+                core_file,
+                Edition::Edition2021,
+                Some(CrateDisplayName::from_canonical_name("core".to_string())),
+                CfgOptions::default(),
+                Env::default(),
+                Vec::new(),
+            );
+
+            for krate in all_crates {
+                crate_graph.add_dep(krate, CrateName::new("core").unwrap(), core_crate).unwrap();
+            }
+        }
         roots.push(SourceRoot::new_local(mem::take(&mut file_set)));
         change.set_roots(roots);
         change.set_crate_graph(crate_graph);
@@ -174,6 +204,7 @@ pub fn parse(ra_fixture: &str) -> ChangeFixture {
     }
 }
 
+#[derive(Debug)]
 struct FileMeta {
     path: String,
     krate: Option<String>,