aboutsummaryrefslogtreecommitdiff
path: root/crates/base_db
diff options
context:
space:
mode:
Diffstat (limited to 'crates/base_db')
-rw-r--r--crates/base_db/src/fixture.rs45
1 files changed, 41 insertions, 4 deletions
diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs
index da4afb5eb..6ce377710 100644
--- a/crates/base_db/src/fixture.rs
+++ b/crates/base_db/src/fixture.rs
@@ -9,8 +9,8 @@ use test_utils::{
9use vfs::{file_set::FileSet, VfsPath}; 9use vfs::{file_set::FileSet, VfsPath};
10 10
11use crate::{ 11use crate::{
12 input::CrateName, Change, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, FileRange, 12 input::CrateName, Change, CrateDisplayName, CrateGraph, CrateId, Edition, Env, FileId,
13 SourceDatabaseExt, SourceRoot, SourceRootId, 13 FilePosition, FileRange, SourceDatabaseExt, SourceRoot, SourceRootId,
14}; 14};
15 15
16pub const WORKSPACE: SourceRootId = SourceRootId(0); 16pub const WORKSPACE: SourceRootId = SourceRootId(0);
@@ -24,6 +24,14 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
24 (db, fixture.files[0]) 24 (db, fixture.files[0])
25 } 25 }
26 26
27 fn with_many_files(ra_fixture: &str) -> (Self, Vec<FileId>) {
28 let fixture = ChangeFixture::parse(ra_fixture);
29 let mut db = Self::default();
30 fixture.change.apply(&mut db);
31 assert!(fixture.file_position.is_none());
32 (db, fixture.files)
33 }
34
27 fn with_files(ra_fixture: &str) -> Self { 35 fn with_files(ra_fixture: &str) -> Self {
28 let fixture = ChangeFixture::parse(ra_fixture); 36 let fixture = ChangeFixture::parse(ra_fixture);
29 let mut db = Self::default(); 37 let mut db = Self::default();
@@ -73,7 +81,7 @@ pub struct ChangeFixture {
73 81
74impl ChangeFixture { 82impl ChangeFixture {
75 pub fn parse(ra_fixture: &str) -> ChangeFixture { 83 pub fn parse(ra_fixture: &str) -> ChangeFixture {
76 let fixture = Fixture::parse(ra_fixture); 84 let (mini_core, fixture) = Fixture::parse(ra_fixture);
77 let mut change = Change::new(); 85 let mut change = Change::new();
78 86
79 let mut files = Vec::new(); 87 let mut files = Vec::new();
@@ -98,7 +106,7 @@ impl ChangeFixture {
98 let (range_or_offset, text) = extract_range_or_offset(&entry.text); 106 let (range_or_offset, text) = extract_range_or_offset(&entry.text);
99 assert!(file_position.is_none()); 107 assert!(file_position.is_none());
100 file_position = Some((file_id, range_or_offset)); 108 file_position = Some((file_id, range_or_offset));
101 text.to_string() 109 text
102 } 110 }
103 } else { 111 } else {
104 entry.text.clone() 112 entry.text.clone()
@@ -106,6 +114,9 @@ impl ChangeFixture {
106 114
107 let meta = FileMeta::from(entry); 115 let meta = FileMeta::from(entry);
108 assert!(meta.path.starts_with(&source_root_prefix)); 116 assert!(meta.path.starts_with(&source_root_prefix));
117 if !meta.deps.is_empty() {
118 assert!(meta.krate.is_some(), "can't specify deps without naming the crate")
119 }
109 120
110 if meta.introduce_new_source_root { 121 if meta.introduce_new_source_root {
111 roots.push(SourceRoot::new_local(mem::take(&mut file_set))); 122 roots.push(SourceRoot::new_local(mem::take(&mut file_set)));
@@ -158,6 +169,31 @@ impl ChangeFixture {
158 } 169 }
159 } 170 }
160 171
172 if let Some(mini_core) = mini_core {
173 let core_file = file_id;
174 file_id.0 += 1;
175
176 let mut fs = FileSet::default();
177 fs.insert(core_file, VfsPath::new_virtual_path("/sysroot/core/lib.rs".to_string()));
178 roots.push(SourceRoot::new_library(fs));
179
180 change.change_file(core_file, Some(Arc::new(mini_core.source_code())));
181
182 let all_crates = crate_graph.crates_in_topological_order();
183
184 let core_crate = crate_graph.add_crate_root(
185 core_file,
186 Edition::Edition2021,
187 Some(CrateDisplayName::from_canonical_name("core".to_string())),
188 CfgOptions::default(),
189 Env::default(),
190 Vec::new(),
191 );
192
193 for krate in all_crates {
194 crate_graph.add_dep(krate, CrateName::new("core").unwrap(), core_crate).unwrap();
195 }
196 }
161 roots.push(SourceRoot::new_local(mem::take(&mut file_set))); 197 roots.push(SourceRoot::new_local(mem::take(&mut file_set)));
162 change.set_roots(roots); 198 change.set_roots(roots);
163 change.set_crate_graph(crate_graph); 199 change.set_crate_graph(crate_graph);
@@ -166,6 +202,7 @@ impl ChangeFixture {
166 } 202 }
167} 203}
168 204
205#[derive(Debug)]
169struct FileMeta { 206struct FileMeta {
170 path: String, 207 path: String,
171 krate: Option<String>, 208 krate: Option<String>,