From cb6205c09da9fd6fc0bd9f88106f5e9bd3f471aa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Dec 2018 17:22:48 +0300 Subject: use relpaths for module resolve --- crates/ra_db/src/input.rs | 14 ++++++++++++++ crates/ra_hir/Cargo.toml | 1 + crates/ra_hir/src/module/imp.rs | 37 +++++++++++++++++++------------------ 3 files changed, 34 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index ac144b991..b09014dc6 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use rustc_hash::{FxHashSet, FxHashMap}; +use relative_path::RelativePathBuf; use ra_syntax::SmolStr; use salsa; @@ -85,10 +86,23 @@ salsa::query_group! { type FileTextQuery; storage input; } + /// Path to a file, relative to the root of its source root. + fn file_relative_path(file_id: FileId) -> RelativePathBuf { + type FileRelativePathQuery; + storage input; + } fn file_source_root(file_id: FileId) -> SourceRootId { type FileSourceRootQuery; storage input; } + fn source_root_files(id: SourceRootId) -> Arc> { + type SourceRootFilesQuery; + storage input; + } + fn source_root_file_by_path(id: SourceRootId, path: RelativePathBuf) -> Option { + type SourceRootFilesByPathQuery; + storage input; + } fn source_root(id: SourceRootId) -> Arc { type SourceRootQuery; storage input; diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml index 1b9e148b2..61650cee9 100644 --- a/crates/ra_hir/Cargo.toml +++ b/crates/ra_hir/Cargo.toml @@ -5,6 +5,7 @@ version = "0.1.0" authors = ["Aleksey Kladov "] [dependencies] +arrayvec = "0.4.9" log = "0.4.5" relative-path = "0.4.0" salsa = "0.8.0" diff --git a/crates/ra_hir/src/module/imp.rs b/crates/ra_hir/src/module/imp.rs index 4a19842c4..d04d24a61 100644 --- a/crates/ra_hir/src/module/imp.rs +++ b/crates/ra_hir/src/module/imp.rs @@ -4,9 +4,9 @@ use ra_syntax::{ ast::{self, NameOwner}, SmolStr, }; -use relative_path::RelativePathBuf; use rustc_hash::{FxHashMap, FxHashSet}; -use ra_db::{SourceRoot, SourceRootId, FileResolverImp, Cancelable, FileId,}; +use arrayvec::ArrayVec; +use ra_db::{SourceRoot, SourceRootId, Cancelable, FileId}; use crate::{ HirDatabase, @@ -110,8 +110,7 @@ fn build_subtree( let (points_to, problem) = match sub { Submodule::Declaration(name) => { - let (points_to, problem) = - resolve_submodule(source, &name, &source_root.file_resolver); + let (points_to, problem) = resolve_submodule(db, source, &name); let points_to = points_to .into_iter() .map(|file_id| match roots.remove(&file_id) { @@ -153,30 +152,32 @@ fn build_subtree( } fn resolve_submodule( + db: &impl HirDatabase, source: ModuleSource, name: &SmolStr, - file_resolver: &FileResolverImp, ) -> (Vec, Option) { - // TODO: handle submodules of inline modules properly + // FIXME: handle submodules of inline modules properly let file_id = source.file_id(); - let mod_name = file_resolver.file_stem(file_id); + let source_root_id = db.file_source_root(file_id); + let path = db.file_relative_path(file_id); + let dir_path = path.parent().unwrap(); + let mod_name = path.file_stem().unwrap_or("unknown"); let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; - let file_mod = RelativePathBuf::from(format!("../{}.rs", name)); - let dir_mod = RelativePathBuf::from(format!("../{}/mod.rs", name)); - let file_dir_mod = RelativePathBuf::from(format!("../{}/{}.rs", mod_name, name)); - let tmp1; - let tmp2; - let candidates = if is_dir_owner { - tmp1 = [&file_mod, &dir_mod]; - tmp1.iter() + let file_mod = dir_path.join(format!("{}.rs", name)); + let dir_mod = dir_path.join(format!("{}/mod.rs", name)); + let file_dir_mod = dir_path.join(format!("{}/{}.rs", mod_name, name)); + let mut candidates = ArrayVec::<[_; 2]>::new(); + if is_dir_owner { + candidates.push(file_mod.clone()); + candidates.push(dir_mod); } else { - tmp2 = [&file_dir_mod]; - tmp2.iter() + candidates.push(file_dir_mod.clone()); }; let points_to = candidates - .filter_map(|path| file_resolver.resolve(file_id, path)) + .into_iter() + .filter_map(|path| db.source_root_file_by_path(source_root_id, path)) .collect::>(); let problem = if points_to.is_empty() { Some(Problem::UnresolvedModule { -- cgit v1.2.3