aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_db')
-rw-r--r--crates/ra_db/src/fixture.rs138
1 files changed, 47 insertions, 91 deletions
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs
index f7d9118a9..ddf46e6c4 100644
--- a/crates/ra_db/src/fixture.rs
+++ b/crates/ra_db/src/fixture.rs
@@ -61,7 +61,7 @@ use std::{str::FromStr, sync::Arc};
61 61
62use ra_cfg::CfgOptions; 62use ra_cfg::CfgOptions;
63use rustc_hash::FxHashMap; 63use rustc_hash::FxHashMap;
64use test_utils::{extract_offset, parse_fixture, parse_single_fixture, FixtureMeta, CURSOR_MARKER}; 64use test_utils::{extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER};
65use vfs::{file_set::FileSet, VfsPath}; 65use vfs::{file_set::FileSet, VfsPath};
66 66
67use crate::{ 67use crate::{
@@ -74,21 +74,32 @@ pub const WORKSPACE: SourceRootId = SourceRootId(0);
74pub trait WithFixture: Default + SourceDatabaseExt + 'static { 74pub trait WithFixture: Default + SourceDatabaseExt + 'static {
75 fn with_single_file(text: &str) -> (Self, FileId) { 75 fn with_single_file(text: &str) -> (Self, FileId) {
76 let mut db = Self::default(); 76 let mut db = Self::default();
77 let file_id = with_single_file(&mut db, text); 77 let (_, files) = with_files(&mut db, text);
78 (db, file_id) 78 assert_eq!(files.len(), 1);
79 (db, files[0])
79 } 80 }
80 81
81 fn with_files(ra_fixture: &str) -> Self { 82 fn with_files(ra_fixture: &str) -> Self {
82 let mut db = Self::default(); 83 let mut db = Self::default();
83 let pos = with_files(&mut db, ra_fixture); 84 let (pos, _) = with_files(&mut db, ra_fixture);
84 assert!(pos.is_none()); 85 assert!(pos.is_none());
85 db 86 db
86 } 87 }
87 88
88 fn with_position(ra_fixture: &str) -> (Self, FilePosition) { 89 fn with_position(ra_fixture: &str) -> (Self, FilePosition) {
90 let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture);
91 let offset = match range_or_offset {
92 RangeOrOffset::Range(_) => panic!(),
93 RangeOrOffset::Offset(it) => it,
94 };
95 (db, FilePosition { file_id, offset })
96 }
97
98 fn with_range_or_offset(ra_fixture: &str) -> (Self, FileId, RangeOrOffset) {
89 let mut db = Self::default(); 99 let mut db = Self::default();
90 let pos = with_files(&mut db, ra_fixture); 100 let (pos, _) = with_files(&mut db, ra_fixture);
91 (db, pos.unwrap()) 101 let (file_id, range_or_offset) = pos.unwrap();
102 (db, file_id, range_or_offset)
92 } 103 }
93 104
94 fn test_crate(&self) -> CrateId { 105 fn test_crate(&self) -> CrateId {
@@ -102,78 +113,27 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
102 113
103impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} 114impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
104 115
105fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId { 116fn with_files(
106 let file_id = FileId(0); 117 db: &mut dyn SourceDatabaseExt,
107 let mut file_set = vfs::file_set::FileSet::default(); 118 fixture: &str,
108 file_set.insert(file_id, vfs::VfsPath::new_virtual_path("/main.rs".to_string())); 119) -> (Option<(FileId, RangeOrOffset)>, Vec<FileId>) {
109 120 let fixture = Fixture::parse(fixture);
110 let source_root = SourceRoot::new_local(file_set);
111
112 let fixture = parse_single_fixture(ra_fixture);
113
114 let crate_graph = if let Some(entry) = fixture {
115 let meta = match ParsedMeta::from(&entry.meta) {
116 ParsedMeta::File(it) => it,
117 _ => panic!("with_single_file only support file meta"),
118 };
119
120 let mut crate_graph = CrateGraph::default();
121 crate_graph.add_crate_root(
122 file_id,
123 meta.edition,
124 meta.krate.map(|name| {
125 CrateName::new(&name).expect("Fixture crate name should not contain dashes")
126 }),
127 meta.cfg,
128 meta.env,
129 Default::default(),
130 );
131 crate_graph
132 } else {
133 let mut crate_graph = CrateGraph::default();
134 crate_graph.add_crate_root(
135 file_id,
136 Edition::Edition2018,
137 None,
138 CfgOptions::default(),
139 Env::default(),
140 Default::default(),
141 );
142 crate_graph
143 };
144
145 db.set_file_text(file_id, Arc::new(ra_fixture.to_string()));
146 db.set_file_source_root(file_id, WORKSPACE);
147 db.set_source_root(WORKSPACE, Arc::new(source_root));
148 db.set_crate_graph(Arc::new(crate_graph));
149
150 file_id
151}
152
153fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosition> {
154 let fixture = parse_fixture(fixture);
155 121
122 let mut files = Vec::new();
156 let mut crate_graph = CrateGraph::default(); 123 let mut crate_graph = CrateGraph::default();
157 let mut crates = FxHashMap::default(); 124 let mut crates = FxHashMap::default();
158 let mut crate_deps = Vec::new(); 125 let mut crate_deps = Vec::new();
159 let mut default_crate_root: Option<FileId> = None; 126 let mut default_crate_root: Option<FileId> = None;
160 127
161 let mut file_set = FileSet::default(); 128 let mut file_set = FileSet::default();
162 let mut source_root_id = WORKSPACE; 129 let source_root_id = WORKSPACE;
163 let mut source_root_prefix = "/".to_string(); 130 let source_root_prefix = "/".to_string();
164 let mut file_id = FileId(0); 131 let mut file_id = FileId(0);
165 132
166 let mut file_position = None; 133 let mut file_position = None;
167 134
168 for entry in fixture.iter() { 135 for entry in fixture.iter() {
169 let meta = match ParsedMeta::from(&entry.meta) { 136 let meta = match ParsedMeta::from(entry) {
170 ParsedMeta::Root { path } => {
171 let file_set = std::mem::replace(&mut file_set, FileSet::default());
172 db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set)));
173 source_root_id.0 += 1;
174 source_root_prefix = path;
175 continue;
176 }
177 ParsedMeta::File(it) => it, 137 ParsedMeta::File(it) => it,
178 }; 138 };
179 assert!(meta.path.starts_with(&source_root_prefix)); 139 assert!(meta.path.starts_with(&source_root_prefix));
@@ -198,9 +158,9 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
198 } 158 }
199 159
200 let text = if entry.text.contains(CURSOR_MARKER) { 160 let text = if entry.text.contains(CURSOR_MARKER) {
201 let (offset, text) = extract_offset(&entry.text); 161 let (range_or_offset, text) = extract_range_or_offset(&entry.text);
202 assert!(file_position.is_none()); 162 assert!(file_position.is_none());
203 file_position = Some(FilePosition { file_id, offset }); 163 file_position = Some((file_id, range_or_offset));
204 text.to_string() 164 text.to_string()
205 } else { 165 } else {
206 entry.text.to_string() 166 entry.text.to_string()
@@ -210,7 +170,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
210 db.set_file_source_root(file_id, source_root_id); 170 db.set_file_source_root(file_id, source_root_id);
211 let path = VfsPath::new_virtual_path(meta.path); 171 let path = VfsPath::new_virtual_path(meta.path);
212 file_set.insert(file_id, path.into()); 172 file_set.insert(file_id, path.into());
213 173 files.push(file_id);
214 file_id.0 += 1; 174 file_id.0 += 1;
215 } 175 }
216 176
@@ -235,11 +195,10 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
235 db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set))); 195 db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set)));
236 db.set_crate_graph(Arc::new(crate_graph)); 196 db.set_crate_graph(Arc::new(crate_graph));
237 197
238 file_position 198 (file_position, files)
239} 199}
240 200
241enum ParsedMeta { 201enum ParsedMeta {
242 Root { path: String },
243 File(FileMeta), 202 File(FileMeta),
244} 203}
245 204
@@ -252,25 +211,22 @@ struct FileMeta {
252 env: Env, 211 env: Env,
253} 212}
254 213
255impl From<&FixtureMeta> for ParsedMeta { 214impl From<&Fixture> for ParsedMeta {
256 fn from(meta: &FixtureMeta) -> Self { 215 fn from(f: &Fixture) -> Self {
257 match meta { 216 let mut cfg = CfgOptions::default();
258 FixtureMeta::Root { path } => { 217 f.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into()));
259 // `Self::Root` causes a false warning: 'variant is never constructed: `Root` ' 218 f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
260 // see https://github.com/rust-lang/rust/issues/69018 219
261 ParsedMeta::Root { path: path.to_owned() } 220 Self::File(FileMeta {
262 } 221 path: f.path.to_owned(),
263 FixtureMeta::File(f) => Self::File(FileMeta { 222 krate: f.crate_name.to_owned(),
264 path: f.path.to_owned(), 223 deps: f.deps.to_owned(),
265 krate: f.crate_name.to_owned(), 224 cfg,
266 deps: f.deps.to_owned(), 225 edition: f
267 cfg: f.cfg.to_owned(), 226 .edition
268 edition: f 227 .as_ref()
269 .edition 228 .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()),
270 .as_ref() 229 env: Env::from(f.env.iter()),
271 .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()), 230 })
272 env: Env::from(f.env.iter()),
273 }),
274 }
275 } 231 }
276} 232}