aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db/src/input.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_db/src/input.rs')
-rw-r--r--crates/ra_db/src/input.rs79
1 files changed, 10 insertions, 69 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index e6af99035..7f3660118 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -6,27 +6,15 @@
6//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how 6//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how
7//! actual IO is done and lowered to input. 7//! actual IO is done and lowered to input.
8 8
9use std::{ 9use std::{fmt, ops, str::FromStr, sync::Arc};
10 fmt, ops,
11 path::{Path, PathBuf},
12 str::FromStr,
13 sync::Arc,
14};
15 10
16use ra_cfg::CfgOptions; 11use ra_cfg::CfgOptions;
17use ra_syntax::SmolStr; 12use ra_syntax::SmolStr;
18use ra_tt::TokenExpander; 13use ra_tt::TokenExpander;
19use rustc_hash::{FxHashMap, FxHashSet}; 14use rustc_hash::{FxHashMap, FxHashSet};
15use vfs::file_set::FileSet;
20 16
21use crate::{RelativePath, RelativePathBuf}; 17pub use vfs::FileId;
22
23/// `FileId` is an integer which uniquely identifies a file. File paths are
24/// messy and system-dependent, so most of the code should work directly with
25/// `FileId`, without inspecting the path. The mapping between `FileId` and path
26/// and `SourceRoot` is constant. A file rename is represented as a pair of
27/// deletion/creation.
28#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
29pub struct FileId(pub u32);
30 18
31/// Files are grouped into source roots. A source root is a directory on the 19/// Files are grouped into source roots. A source root is a directory on the
32/// file systems which is watched for changes. Typically it corresponds to a 20/// file systems which is watched for changes. Typically it corresponds to a
@@ -45,27 +33,18 @@ pub struct SourceRoot {
45 /// Libraries are considered mostly immutable, this assumption is used to 33 /// Libraries are considered mostly immutable, this assumption is used to
46 /// optimize salsa's query structure 34 /// optimize salsa's query structure
47 pub is_library: bool, 35 pub is_library: bool,
48 files: FxHashMap<RelativePathBuf, FileId>, 36 pub(crate) file_set: FileSet,
49} 37}
50 38
51impl SourceRoot { 39impl SourceRoot {
52 pub fn new_local() -> SourceRoot { 40 pub fn new_local(file_set: FileSet) -> SourceRoot {
53 SourceRoot { is_library: false, files: Default::default() } 41 SourceRoot { is_library: false, file_set }
54 }
55 pub fn new_library() -> SourceRoot {
56 SourceRoot { is_library: true, files: Default::default() }
57 }
58 pub fn insert_file(&mut self, path: RelativePathBuf, file_id: FileId) {
59 self.files.insert(path, file_id);
60 } 42 }
61 pub fn remove_file(&mut self, path: &RelativePath) { 43 pub fn new_library(file_set: FileSet) -> SourceRoot {
62 self.files.remove(path); 44 SourceRoot { is_library: true, file_set }
63 } 45 }
64 pub fn walk(&self) -> impl Iterator<Item = FileId> + '_ { 46 pub fn iter(&self) -> impl Iterator<Item = FileId> + '_ {
65 self.files.values().copied() 47 self.file_set.iter()
66 }
67 pub fn file_by_relative_path(&self, path: &RelativePath) -> Option<FileId> {
68 self.files.get(path).copied()
69 } 48 }
70} 49}
71 50
@@ -141,7 +120,6 @@ pub struct CrateData {
141 pub display_name: Option<CrateName>, 120 pub display_name: Option<CrateName>,
142 pub cfg_options: CfgOptions, 121 pub cfg_options: CfgOptions,
143 pub env: Env, 122 pub env: Env,
144 pub extern_source: ExternSource,
145 pub dependencies: Vec<Dependency>, 123 pub dependencies: Vec<Dependency>,
146 pub proc_macro: Vec<ProcMacro>, 124 pub proc_macro: Vec<ProcMacro>,
147} 125}
@@ -152,22 +130,11 @@ pub enum Edition {
152 Edition2015, 130 Edition2015,
153} 131}
154 132
155#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
156pub struct ExternSourceId(pub u32);
157
158#[derive(Default, Debug, Clone, PartialEq, Eq)] 133#[derive(Default, Debug, Clone, PartialEq, Eq)]
159pub struct Env { 134pub struct Env {
160 entries: FxHashMap<String, String>, 135 entries: FxHashMap<String, String>,
161} 136}
162 137
163// FIXME: Redesign vfs for solve the following limitation ?
164// Note: Some env variables (e.g. OUT_DIR) are located outside of the
165// crate. We store a map to allow remap it to ExternSourceId
166#[derive(Default, Debug, Clone, PartialEq, Eq)]
167pub struct ExternSource {
168 extern_paths: FxHashMap<PathBuf, ExternSourceId>,
169}
170
171#[derive(Debug, Clone, PartialEq, Eq)] 138#[derive(Debug, Clone, PartialEq, Eq)]
172pub struct Dependency { 139pub struct Dependency {
173 pub crate_id: CrateId, 140 pub crate_id: CrateId,
@@ -182,7 +149,6 @@ impl CrateGraph {
182 display_name: Option<CrateName>, 149 display_name: Option<CrateName>,
183 cfg_options: CfgOptions, 150 cfg_options: CfgOptions,
184 env: Env, 151 env: Env,
185 extern_source: ExternSource,
186 proc_macro: Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)>, 152 proc_macro: Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)>,
187 ) -> CrateId { 153 ) -> CrateId {
188 let proc_macro = 154 let proc_macro =
@@ -194,7 +160,6 @@ impl CrateGraph {
194 display_name, 160 display_name,
195 cfg_options, 161 cfg_options,
196 env, 162 env,
197 extern_source,
198 proc_macro, 163 proc_macro,
199 dependencies: Vec::new(), 164 dependencies: Vec::new(),
200 }; 165 };
@@ -334,20 +299,6 @@ impl Env {
334 } 299 }
335} 300}
336 301
337impl ExternSource {
338 pub fn extern_path(&self, path: &Path) -> Option<(ExternSourceId, RelativePathBuf)> {
339 self.extern_paths.iter().find_map(|(root_path, id)| {
340 let rel_path = path.strip_prefix(root_path).ok()?;
341 let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
342 Some((*id, rel_path))
343 })
344 }
345
346 pub fn set_extern_path(&mut self, root_path: &Path, root: ExternSourceId) {
347 self.extern_paths.insert(root_path.to_path_buf(), root);
348 }
349}
350
351#[derive(Debug)] 302#[derive(Debug)]
352pub struct ParseEditionError { 303pub struct ParseEditionError {
353 invalid_input: String, 304 invalid_input: String,
@@ -378,7 +329,6 @@ mod tests {
378 CfgOptions::default(), 329 CfgOptions::default(),
379 Env::default(), 330 Env::default(),
380 Default::default(), 331 Default::default(),
381 Default::default(),
382 ); 332 );
383 let crate2 = graph.add_crate_root( 333 let crate2 = graph.add_crate_root(
384 FileId(2u32), 334 FileId(2u32),
@@ -387,7 +337,6 @@ mod tests {
387 CfgOptions::default(), 337 CfgOptions::default(),
388 Env::default(), 338 Env::default(),
389 Default::default(), 339 Default::default(),
390 Default::default(),
391 ); 340 );
392 let crate3 = graph.add_crate_root( 341 let crate3 = graph.add_crate_root(
393 FileId(3u32), 342 FileId(3u32),
@@ -396,7 +345,6 @@ mod tests {
396 CfgOptions::default(), 345 CfgOptions::default(),
397 Env::default(), 346 Env::default(),
398 Default::default(), 347 Default::default(),
399 Default::default(),
400 ); 348 );
401 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); 349 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
402 assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); 350 assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
@@ -413,7 +361,6 @@ mod tests {
413 CfgOptions::default(), 361 CfgOptions::default(),
414 Env::default(), 362 Env::default(),
415 Default::default(), 363 Default::default(),
416 Default::default(),
417 ); 364 );
418 let crate2 = graph.add_crate_root( 365 let crate2 = graph.add_crate_root(
419 FileId(2u32), 366 FileId(2u32),
@@ -422,7 +369,6 @@ mod tests {
422 CfgOptions::default(), 369 CfgOptions::default(),
423 Env::default(), 370 Env::default(),
424 Default::default(), 371 Default::default(),
425 Default::default(),
426 ); 372 );
427 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); 373 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
428 assert!(graph.add_dep(crate2, CrateName::new("crate2").unwrap(), crate2).is_err()); 374 assert!(graph.add_dep(crate2, CrateName::new("crate2").unwrap(), crate2).is_err());
@@ -438,7 +384,6 @@ mod tests {
438 CfgOptions::default(), 384 CfgOptions::default(),
439 Env::default(), 385 Env::default(),
440 Default::default(), 386 Default::default(),
441 Default::default(),
442 ); 387 );
443 let crate2 = graph.add_crate_root( 388 let crate2 = graph.add_crate_root(
444 FileId(2u32), 389 FileId(2u32),
@@ -447,7 +392,6 @@ mod tests {
447 CfgOptions::default(), 392 CfgOptions::default(),
448 Env::default(), 393 Env::default(),
449 Default::default(), 394 Default::default(),
450 Default::default(),
451 ); 395 );
452 let crate3 = graph.add_crate_root( 396 let crate3 = graph.add_crate_root(
453 FileId(3u32), 397 FileId(3u32),
@@ -456,7 +400,6 @@ mod tests {
456 CfgOptions::default(), 400 CfgOptions::default(),
457 Env::default(), 401 Env::default(),
458 Default::default(), 402 Default::default(),
459 Default::default(),
460 ); 403 );
461 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); 404 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
462 assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); 405 assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
@@ -472,7 +415,6 @@ mod tests {
472 CfgOptions::default(), 415 CfgOptions::default(),
473 Env::default(), 416 Env::default(),
474 Default::default(), 417 Default::default(),
475 Default::default(),
476 ); 418 );
477 let crate2 = graph.add_crate_root( 419 let crate2 = graph.add_crate_root(
478 FileId(2u32), 420 FileId(2u32),
@@ -481,7 +423,6 @@ mod tests {
481 CfgOptions::default(), 423 CfgOptions::default(),
482 Env::default(), 424 Env::default(),
483 Default::default(), 425 Default::default(),
484 Default::default(),
485 ); 426 );
486 assert!(graph 427 assert!(graph
487 .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) 428 .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)