From d08c63cb9e3574fa97374a8529136814530bf416 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 20 May 2020 23:51:20 +0200 Subject: Add an ImportMap --- crates/ra_db/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index fd4280de2..e7868268b 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -11,8 +11,8 @@ use ra_syntax::{ast, Parse, SourceFile, TextRange, TextSize}; pub use crate::{ cancellation::Canceled, input::{ - CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSource, ExternSourceId, - FileId, ProcMacroId, SourceRoot, SourceRootId, + CrateData, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSource, + ExternSourceId, FileId, ProcMacroId, SourceRoot, SourceRootId, }, }; pub use relative_path::{RelativePath, RelativePathBuf}; -- cgit v1.2.3 From e63c00f100e960f7b72997026b4b2cd3cd29774b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 5 Jun 2020 14:55:23 +0200 Subject: Rename resolve_relative_path -> resolve_path For things like `concant!(env!("OUT_DIR"))`, we need to support abs paths --- crates/ra_db/src/lib.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index fd4280de2..07e1b8aba 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -89,8 +89,7 @@ pub const DEFAULT_LRU_CAP: usize = 128; pub trait FileLoader { /// Text of the file. fn file_text(&self, file_id: FileId) -> Arc; - fn resolve_relative_path(&self, anchor: FileId, relative_path: &RelativePath) - -> Option; + fn resolve_path(&self, anchor: FileId, relative_path: &RelativePath) -> Option; fn relevant_crates(&self, file_id: FileId) -> Arc>; fn resolve_extern_path( @@ -155,11 +154,7 @@ impl FileLoader for FileLoaderDelegate<&'_ T> { fn file_text(&self, file_id: FileId) -> Arc { SourceDatabaseExt::file_text(self.0, file_id) } - fn resolve_relative_path( - &self, - anchor: FileId, - relative_path: &RelativePath, - ) -> Option { + fn resolve_path(&self, anchor: FileId, relative_path: &RelativePath) -> Option { let path = { let mut path = self.0.file_relative_path(anchor); assert!(path.pop()); -- cgit v1.2.3 From bba374bab2ee53643a899e7ea459b4ab27663bd0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 5 Jun 2020 15:07:30 +0200 Subject: More direct signature for resolve_path --- crates/ra_db/src/lib.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 07e1b8aba..2e63cb46e 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -89,7 +89,7 @@ pub const DEFAULT_LRU_CAP: usize = 128; pub trait FileLoader { /// Text of the file. fn file_text(&self, file_id: FileId) -> Arc; - fn resolve_path(&self, anchor: FileId, relative_path: &RelativePath) -> Option; + fn resolve_path(&self, anchor: FileId, path: &str) -> Option; fn relevant_crates(&self, file_id: FileId) -> Arc>; fn resolve_extern_path( @@ -154,16 +154,21 @@ impl FileLoader for FileLoaderDelegate<&'_ T> { fn file_text(&self, file_id: FileId) -> Arc { SourceDatabaseExt::file_text(self.0, file_id) } - fn resolve_path(&self, anchor: FileId, relative_path: &RelativePath) -> Option { - let path = { - let mut path = self.0.file_relative_path(anchor); - assert!(path.pop()); - path.push(relative_path); - path.normalize() + /// Note that we intentionally accept a `&str` and not a `&Path` here. This + /// method exists to handle `#[path = "/some/path.rs"] mod foo;` and such, + /// so the input is guaranteed to be utf-8 string. We might introduce + /// `struct StrPath(str)` for clarity some day, but it's a bit messy, so we + /// get by with a `&str` for the time being. + fn resolve_path(&self, anchor: FileId, path: &str) -> Option { + let rel_path = { + let mut rel_path = self.0.file_relative_path(anchor); + assert!(rel_path.pop()); + rel_path.push(path); + rel_path.normalize() }; let source_root = self.0.file_source_root(anchor); let source_root = self.0.source_root(source_root); - source_root.file_by_relative_path(&path) + source_root.file_by_relative_path(&rel_path) } fn relevant_crates(&self, file_id: FileId) -> Arc> { -- cgit v1.2.3 From bbb40d746368eb6a38498649a723c7ead0ef8136 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 5 Jun 2020 16:45:20 +0200 Subject: Minimize FileLoader interface --- crates/ra_db/src/lib.rs | 53 ++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 29 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 2e63cb46e..7354b3832 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -89,14 +89,13 @@ pub const DEFAULT_LRU_CAP: usize = 128; pub trait FileLoader { /// Text of the file. fn file_text(&self, file_id: FileId) -> Arc; + /// Note that we intentionally accept a `&str` and not a `&Path` here. This + /// method exists to handle `#[path = "/some/path.rs"] mod foo;` and such, + /// so the input is guaranteed to be utf-8 string. We might introduce + /// `struct StrPath(str)` for clarity some day, but it's a bit messy, so we + /// get by with a `&str` for the time being. fn resolve_path(&self, anchor: FileId, path: &str) -> Option; fn relevant_crates(&self, file_id: FileId) -> Arc>; - - fn resolve_extern_path( - &self, - extern_id: ExternSourceId, - relative_path: &RelativePath, - ) -> Option; } /// Database which stores all significant input facts: source code and project @@ -154,34 +153,30 @@ impl FileLoader for FileLoaderDelegate<&'_ T> { fn file_text(&self, file_id: FileId) -> Arc { SourceDatabaseExt::file_text(self.0, file_id) } - /// Note that we intentionally accept a `&str` and not a `&Path` here. This - /// method exists to handle `#[path = "/some/path.rs"] mod foo;` and such, - /// so the input is guaranteed to be utf-8 string. We might introduce - /// `struct StrPath(str)` for clarity some day, but it's a bit messy, so we - /// get by with a `&str` for the time being. fn resolve_path(&self, anchor: FileId, path: &str) -> Option { - let rel_path = { - let mut rel_path = self.0.file_relative_path(anchor); - assert!(rel_path.pop()); - rel_path.push(path); - rel_path.normalize() - }; - let source_root = self.0.file_source_root(anchor); - let source_root = self.0.source_root(source_root); - source_root.file_by_relative_path(&rel_path) + // FIXME: this *somehow* should be platform agnostic... + if std::path::Path::new(path).is_absolute() { + let krate = *self.relevant_crates(anchor).get(0)?; + let (extern_source_id, relative_file) = + self.0.crate_graph()[krate].extern_source.extern_path(path)?; + + let source_root = self.0.source_root(SourceRootId(extern_source_id.0)); + source_root.file_by_relative_path(&relative_file) + } else { + let rel_path = { + let mut rel_path = self.0.file_relative_path(anchor); + assert!(rel_path.pop()); + rel_path.push(path); + rel_path.normalize() + }; + let source_root = self.0.file_source_root(anchor); + let source_root = self.0.source_root(source_root); + source_root.file_by_relative_path(&rel_path) + } } fn relevant_crates(&self, file_id: FileId) -> Arc> { let source_root = self.0.file_source_root(file_id); self.0.source_root_crates(source_root) } - - fn resolve_extern_path( - &self, - extern_id: ExternSourceId, - relative_path: &RelativePath, - ) -> Option { - let source_root = self.0.source_root(SourceRootId(extern_id.0)); - source_root.file_by_relative_path(&relative_path) - } } -- cgit v1.2.3 From d8571e076c0bbb3219fb3ed250279b158d75e683 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 9 Jun 2020 13:17:22 +0200 Subject: Simplify --- crates/ra_db/src/input.rs | 12 ++++-------- crates/ra_db/src/lib.rs | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 4d2d3b48a..a8d6466ea 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -337,15 +337,11 @@ impl Env { } impl ExternSource { - pub fn extern_path(&self, path: impl AsRef) -> Option<(ExternSourceId, RelativePathBuf)> { - let path = path.as_ref(); + pub fn extern_path(&self, path: &Path) -> Option<(ExternSourceId, RelativePathBuf)> { self.extern_paths.iter().find_map(|(root_path, id)| { - if let Ok(rel_path) = path.strip_prefix(root_path) { - let rel_path = RelativePathBuf::from_path(rel_path).ok()?; - Some((*id, rel_path)) - } else { - None - } + let rel_path = path.strip_prefix(root_path).ok()?; + let rel_path = RelativePathBuf::from_path(rel_path).ok()?; + Some((*id, rel_path)) }) } diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 91e0ee619..2ab314884 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -158,7 +158,7 @@ impl FileLoader for FileLoaderDelegate<&'_ T> { if std::path::Path::new(path).is_absolute() { let krate = *self.relevant_crates(anchor).get(0)?; let (extern_source_id, relative_file) = - self.0.crate_graph()[krate].extern_source.extern_path(path)?; + self.0.crate_graph()[krate].extern_source.extern_path(path.as_ref())?; let source_root = self.0.source_root(SourceRootId(extern_source_id.0)); source_root.file_by_relative_path(&relative_file) -- cgit v1.2.3 From d8a5d39c2d05fb59b6c243935111714e18334599 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 11 Jun 2020 11:30:06 +0200 Subject: Make relevant_crates return a Set --- crates/ra_db/src/input.rs | 8 +++----- crates/ra_db/src/lib.rs | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index a8d6466ea..bf26048f2 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -15,12 +15,10 @@ use std::{ use ra_cfg::CfgOptions; use ra_syntax::SmolStr; -use rustc_hash::FxHashMap; -use rustc_hash::FxHashSet; +use ra_tt::TokenExpander; +use rustc_hash::{FxHashMap, FxHashSet}; use crate::{RelativePath, RelativePathBuf}; -use fmt::Display; -use ra_tt::TokenExpander; /// `FileId` is an integer which uniquely identifies a file. File paths are /// messy and system-dependent, so most of the code should work directly with @@ -111,7 +109,7 @@ impl CrateName { } } -impl Display for CrateName { +impl fmt::Display for CrateName { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) } diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 2ab314884..80ddb6058 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -7,6 +7,7 @@ use std::{panic, sync::Arc}; use ra_prof::profile; use ra_syntax::{ast, Parse, SourceFile, TextRange, TextSize}; +use rustc_hash::FxHashSet; pub use crate::{ cancellation::Canceled, @@ -95,7 +96,7 @@ pub trait FileLoader { /// `struct StrPath(str)` for clarity some day, but it's a bit messy, so we /// get by with a `&str` for the time being. fn resolve_path(&self, anchor: FileId, path: &str) -> Option; - fn relevant_crates(&self, file_id: FileId) -> Arc>; + fn relevant_crates(&self, file_id: FileId) -> Arc>; } /// Database which stores all significant input facts: source code and project @@ -133,16 +134,21 @@ pub trait SourceDatabaseExt: SourceDatabase { #[salsa::input] fn source_root(&self, id: SourceRootId) -> Arc; - fn source_root_crates(&self, id: SourceRootId) -> Arc>; + fn source_root_crates(&self, id: SourceRootId) -> Arc>; } fn source_root_crates( db: &(impl SourceDatabaseExt + SourceDatabase), id: SourceRootId, -) -> Arc> { - let root = db.source_root(id); +) -> Arc> { let graph = db.crate_graph(); - let res = root.walk().filter_map(|it| graph.crate_id_for_crate_root(it)).collect::>(); + let res = graph + .iter() + .filter(|&krate| { + let root_file = graph[krate].root_file_id; + db.file_source_root(root_file) == id + }) + .collect::>(); Arc::new(res) } @@ -156,7 +162,7 @@ impl FileLoader for FileLoaderDelegate<&'_ T> { fn resolve_path(&self, anchor: FileId, path: &str) -> Option { // FIXME: this *somehow* should be platform agnostic... if std::path::Path::new(path).is_absolute() { - let krate = *self.relevant_crates(anchor).get(0)?; + let krate = *self.relevant_crates(anchor).iter().next()?; let (extern_source_id, relative_file) = self.0.crate_graph()[krate].extern_source.extern_path(path.as_ref())?; @@ -175,7 +181,7 @@ impl FileLoader for FileLoaderDelegate<&'_ T> { } } - fn relevant_crates(&self, file_id: FileId) -> Arc> { + fn relevant_crates(&self, file_id: FileId) -> Arc> { let source_root = self.0.file_source_root(file_id); self.0.source_root_crates(source_root) } -- cgit v1.2.3