]> 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 69ceba735617b9e0eaf93d6b3bd11e4d1f7150ae..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);
@@ -24,6 +24,14 @@ fn with_single_file(text: &str) -> (Self, FileId) {
         (db, fixture.files[0])
     }
 
+    fn with_many_files(ra_fixture: &str) -> (Self, Vec<FileId>) {
+        let fixture = ChangeFixture::parse(ra_fixture);
+        let mut db = Self::default();
+        fixture.change.apply(&mut db);
+        assert!(fixture.file_position.is_none());
+        (db, fixture.files)
+    }
+
     fn with_files(ra_fixture: &str) -> Self {
         let fixture = ChangeFixture::parse(ra_fixture);
         let mut db = Self::default();
@@ -73,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();
@@ -98,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()
@@ -106,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)));
@@ -120,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());
@@ -149,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 {
@@ -158,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);
@@ -166,6 +204,7 @@ pub fn parse(ra_fixture: &str) -> ChangeFixture {
     }
 }
 
+#[derive(Debug)]
 struct FileMeta {
     path: String,
     krate: Option<String>,
@@ -190,7 +229,7 @@ fn from(f: Fixture) -> FileMeta {
             edition: f
                 .edition
                 .as_ref()
-                .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()),
+                .map_or(Edition::Edition2018, |v| Edition::from_str(v).unwrap()),
             env: f.env.into_iter().collect(),
             introduce_new_source_root: f.introduce_new_source_root,
         }