aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-03 20:35:48 +0000
committerAleksey Kladov <[email protected]>2019-11-03 20:35:48 +0000
commit6fba51c5fc05264abcbf971dcf28142746588d74 (patch)
treeedbdbf98b279802ee8dc03f35e941943e1e20e10 /crates/ra_db
parent0933d914a37c4ab57fda6fe95464d194dab6f80c (diff)
move crate_def_map tests to hir_def
Diffstat (limited to 'crates/ra_db')
-rw-r--r--crates/ra_db/src/fixture.rs148
-rw-r--r--crates/ra_db/src/input.rs1
2 files changed, 148 insertions, 1 deletions
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs
index 469251fe9..f5dd59f84 100644
--- a/crates/ra_db/src/fixture.rs
+++ b/crates/ra_db/src/fixture.rs
@@ -3,9 +3,12 @@
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use ra_cfg::CfgOptions; 5use ra_cfg::CfgOptions;
6use rustc_hash::FxHashMap;
7use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER};
6 8
7use crate::{ 9use crate::{
8 CrateGraph, Edition, FileId, RelativePathBuf, SourceDatabaseExt, SourceRoot, SourceRootId, 10 CrateGraph, Edition, FileId, FilePosition, RelativePathBuf, SourceDatabaseExt, SourceRoot,
11 SourceRootId,
9}; 12};
10 13
11pub const WORKSPACE: SourceRootId = SourceRootId(0); 14pub const WORKSPACE: SourceRootId = SourceRootId(0);
@@ -16,6 +19,19 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
16 let file_id = with_single_file(&mut db, text); 19 let file_id = with_single_file(&mut db, text);
17 (db, file_id) 20 (db, file_id)
18 } 21 }
22
23 fn with_files(fixture: &str) -> Self {
24 let mut db = Self::default();
25 let pos = with_files(&mut db, fixture);
26 assert!(pos.is_none());
27 db
28 }
29
30 fn with_position(fixture: &str) -> (Self, FilePosition) {
31 let mut db = Self::default();
32 let pos = with_files(&mut db, fixture);
33 (db, pos.unwrap())
34 }
19} 35}
20 36
21impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} 37impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
@@ -38,3 +54,133 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, text: &str) -> FileId {
38 54
39 file_id 55 file_id
40} 56}
57
58fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosition> {
59 let fixture = parse_fixture(fixture);
60
61 let mut crate_graph = CrateGraph::default();
62 let mut crates = FxHashMap::default();
63 let mut crate_deps = Vec::new();
64 let mut default_crate_root: Option<FileId> = None;
65
66 let mut source_root = SourceRoot::default();
67 let mut source_root_id = WORKSPACE;
68 let mut source_root_prefix: RelativePathBuf = "/".into();
69 let mut file_id = FileId(0);
70
71 let mut file_position = None;
72
73 for entry in fixture.iter() {
74 let meta = match parse_meta(&entry.meta) {
75 ParsedMeta::Root { path } => {
76 let source_root = std::mem::replace(&mut source_root, SourceRoot::default());
77 db.set_source_root(source_root_id, Arc::new(source_root));
78 source_root_id.0 += 1;
79 source_root_prefix = path;
80 continue;
81 }
82 ParsedMeta::File(it) => it,
83 };
84 assert!(meta.path.starts_with(&source_root_prefix));
85
86 if let Some(krate) = meta.krate {
87 let crate_id = crate_graph.add_crate_root(file_id, meta.edition, meta.cfg);
88 let prev = crates.insert(krate.clone(), crate_id);
89 assert!(prev.is_none());
90 for dep in meta.deps {
91 crate_deps.push((krate.clone(), dep))
92 }
93 } else if meta.path == "/main.rs" || meta.path == "/lib.rs" {
94 assert!(default_crate_root.is_none());
95 default_crate_root = Some(file_id);
96 }
97
98 let text = if entry.text.contains(CURSOR_MARKER) {
99 let (offset, text) = extract_offset(&entry.text);
100 assert!(file_position.is_none());
101 file_position = Some(FilePosition { file_id, offset });
102 text.to_string()
103 } else {
104 entry.text.to_string()
105 };
106
107 db.set_file_text(file_id, Arc::new(text));
108 db.set_file_relative_path(file_id, meta.path.clone());
109 db.set_file_source_root(file_id, source_root_id);
110 source_root.insert_file(meta.path, file_id);
111
112 file_id.0 += 1;
113 }
114
115 if crates.is_empty() {
116 let crate_root = default_crate_root.unwrap();
117 crate_graph.add_crate_root(crate_root, Edition::Edition2018, CfgOptions::default());
118 } else {
119 for (from, to) in crate_deps {
120 let from_id = crates[&from];
121 let to_id = crates[&to];
122 crate_graph.add_dep(from_id, to.into(), to_id).unwrap();
123 }
124 }
125
126 db.set_source_root(source_root_id, Arc::new(source_root));
127 db.set_crate_graph(Arc::new(crate_graph));
128
129 file_position
130}
131
132enum ParsedMeta {
133 Root { path: RelativePathBuf },
134 File(FileMeta),
135}
136
137struct FileMeta {
138 path: RelativePathBuf,
139 krate: Option<String>,
140 deps: Vec<String>,
141 cfg: CfgOptions,
142 edition: Edition,
143}
144
145//- /lib.rs crate:foo deps:bar,baz
146fn parse_meta(meta: &str) -> ParsedMeta {
147 let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
148
149 if components[0] == "root" {
150 let path: RelativePathBuf = components[1].into();
151 assert!(path.starts_with("/") && path.ends_with("/"));
152 return ParsedMeta::Root { path };
153 }
154
155 let path: RelativePathBuf = components[0].into();
156 assert!(path.starts_with("/"));
157
158 let mut krate = None;
159 let mut deps = Vec::new();
160 let mut edition = Edition::Edition2018;
161 let mut cfg = CfgOptions::default();
162 for component in components[1..].iter() {
163 let (key, value) = split1(component, ':').unwrap();
164 match key {
165 "crate" => krate = Some(value.to_string()),
166 "deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
167 "edition" => edition = Edition::from_string(&value),
168 "cfg" => {
169 for key in value.split(',') {
170 match split1(key, '=') {
171 None => cfg.insert_atom(key.into()),
172 Some((k, v)) => cfg.insert_key_value(k.into(), v.into()),
173 }
174 }
175 }
176 _ => panic!("bad component: {:?}", component),
177 }
178 }
179
180 ParsedMeta::File(FileMeta { path, krate, deps, edition, cfg })
181}
182
183fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> {
184 let idx = haystack.find(delim)?;
185 Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..]))
186}
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index eafa95921..7c8dac1d3 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -97,6 +97,7 @@ pub enum Edition {
97} 97}
98 98
99impl Edition { 99impl Edition {
100 //FIXME: replace with FromStr with proper error handling
100 pub fn from_string(s: &str) -> Edition { 101 pub fn from_string(s: &str) -> Edition {
101 match s { 102 match s {
102 "2015" => Edition::Edition2015, 103 "2015" => Edition::Edition2015,