diff options
author | Aleksey Kladov <[email protected]> | 2018-12-18 14:22:48 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-12-20 09:15:38 +0000 |
commit | cb6205c09da9fd6fc0bd9f88106f5e9bd3f471aa (patch) | |
tree | ceff920e800aff83b23df5d75eb07a993a6a1b9f /crates | |
parent | a422d480a188a28c6b5e7862fbf07817eb2c7447 (diff) |
use relpaths for module resolve
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_db/src/input.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_hir/src/module/imp.rs | 37 |
3 files changed, 34 insertions, 18 deletions
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 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use rustc_hash::{FxHashSet, FxHashMap}; | 3 | use rustc_hash::{FxHashSet, FxHashMap}; |
4 | use relative_path::RelativePathBuf; | ||
4 | use ra_syntax::SmolStr; | 5 | use ra_syntax::SmolStr; |
5 | use salsa; | 6 | use salsa; |
6 | 7 | ||
@@ -85,10 +86,23 @@ salsa::query_group! { | |||
85 | type FileTextQuery; | 86 | type FileTextQuery; |
86 | storage input; | 87 | storage input; |
87 | } | 88 | } |
89 | /// Path to a file, relative to the root of its source root. | ||
90 | fn file_relative_path(file_id: FileId) -> RelativePathBuf { | ||
91 | type FileRelativePathQuery; | ||
92 | storage input; | ||
93 | } | ||
88 | fn file_source_root(file_id: FileId) -> SourceRootId { | 94 | fn file_source_root(file_id: FileId) -> SourceRootId { |
89 | type FileSourceRootQuery; | 95 | type FileSourceRootQuery; |
90 | storage input; | 96 | storage input; |
91 | } | 97 | } |
98 | fn source_root_files(id: SourceRootId) -> Arc<FxHashSet<FileId>> { | ||
99 | type SourceRootFilesQuery; | ||
100 | storage input; | ||
101 | } | ||
102 | fn source_root_file_by_path(id: SourceRootId, path: RelativePathBuf) -> Option<FileId> { | ||
103 | type SourceRootFilesByPathQuery; | ||
104 | storage input; | ||
105 | } | ||
92 | fn source_root(id: SourceRootId) -> Arc<SourceRoot> { | 106 | fn source_root(id: SourceRootId) -> Arc<SourceRoot> { |
93 | type SourceRootQuery; | 107 | type SourceRootQuery; |
94 | storage input; | 108 | 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" | |||
5 | authors = ["Aleksey Kladov <[email protected]>"] | 5 | authors = ["Aleksey Kladov <[email protected]>"] |
6 | 6 | ||
7 | [dependencies] | 7 | [dependencies] |
8 | arrayvec = "0.4.9" | ||
8 | log = "0.4.5" | 9 | log = "0.4.5" |
9 | relative-path = "0.4.0" | 10 | relative-path = "0.4.0" |
10 | salsa = "0.8.0" | 11 | 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::{ | |||
4 | ast::{self, NameOwner}, | 4 | ast::{self, NameOwner}, |
5 | SmolStr, | 5 | SmolStr, |
6 | }; | 6 | }; |
7 | use relative_path::RelativePathBuf; | ||
8 | use rustc_hash::{FxHashMap, FxHashSet}; | 7 | use rustc_hash::{FxHashMap, FxHashSet}; |
9 | use ra_db::{SourceRoot, SourceRootId, FileResolverImp, Cancelable, FileId,}; | 8 | use arrayvec::ArrayVec; |
9 | use ra_db::{SourceRoot, SourceRootId, Cancelable, FileId}; | ||
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | HirDatabase, | 12 | HirDatabase, |
@@ -110,8 +110,7 @@ fn build_subtree( | |||
110 | 110 | ||
111 | let (points_to, problem) = match sub { | 111 | let (points_to, problem) = match sub { |
112 | Submodule::Declaration(name) => { | 112 | Submodule::Declaration(name) => { |
113 | let (points_to, problem) = | 113 | let (points_to, problem) = resolve_submodule(db, source, &name); |
114 | resolve_submodule(source, &name, &source_root.file_resolver); | ||
115 | let points_to = points_to | 114 | let points_to = points_to |
116 | .into_iter() | 115 | .into_iter() |
117 | .map(|file_id| match roots.remove(&file_id) { | 116 | .map(|file_id| match roots.remove(&file_id) { |
@@ -153,30 +152,32 @@ fn build_subtree( | |||
153 | } | 152 | } |
154 | 153 | ||
155 | fn resolve_submodule( | 154 | fn resolve_submodule( |
155 | db: &impl HirDatabase, | ||
156 | source: ModuleSource, | 156 | source: ModuleSource, |
157 | name: &SmolStr, | 157 | name: &SmolStr, |
158 | file_resolver: &FileResolverImp, | ||
159 | ) -> (Vec<FileId>, Option<Problem>) { | 158 | ) -> (Vec<FileId>, Option<Problem>) { |
160 | // TODO: handle submodules of inline modules properly | 159 | // FIXME: handle submodules of inline modules properly |
161 | let file_id = source.file_id(); | 160 | let file_id = source.file_id(); |
162 | let mod_name = file_resolver.file_stem(file_id); | 161 | let source_root_id = db.file_source_root(file_id); |
162 | let path = db.file_relative_path(file_id); | ||
163 | let dir_path = path.parent().unwrap(); | ||
164 | let mod_name = path.file_stem().unwrap_or("unknown"); | ||
163 | let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; | 165 | let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; |
164 | 166 | ||
165 | let file_mod = RelativePathBuf::from(format!("../{}.rs", name)); | 167 | let file_mod = dir_path.join(format!("{}.rs", name)); |
166 | let dir_mod = RelativePathBuf::from(format!("../{}/mod.rs", name)); | 168 | let dir_mod = dir_path.join(format!("{}/mod.rs", name)); |
167 | let file_dir_mod = RelativePathBuf::from(format!("../{}/{}.rs", mod_name, name)); | 169 | let file_dir_mod = dir_path.join(format!("{}/{}.rs", mod_name, name)); |
168 | let tmp1; | 170 | let mut candidates = ArrayVec::<[_; 2]>::new(); |
169 | let tmp2; | 171 | if is_dir_owner { |
170 | let candidates = if is_dir_owner { | 172 | candidates.push(file_mod.clone()); |
171 | tmp1 = [&file_mod, &dir_mod]; | 173 | candidates.push(dir_mod); |
172 | tmp1.iter() | ||
173 | } else { | 174 | } else { |
174 | tmp2 = [&file_dir_mod]; | 175 | candidates.push(file_dir_mod.clone()); |
175 | tmp2.iter() | ||
176 | }; | 176 | }; |
177 | 177 | ||
178 | let points_to = candidates | 178 | let points_to = candidates |
179 | .filter_map(|path| file_resolver.resolve(file_id, path)) | 179 | .into_iter() |
180 | .filter_map(|path| db.source_root_file_by_path(source_root_id, path)) | ||
180 | .collect::<Vec<_>>(); | 181 | .collect::<Vec<_>>(); |
181 | let problem = if points_to.is_empty() { | 182 | let problem = if points_to.is_empty() { |
182 | Some(Problem::UnresolvedModule { | 183 | Some(Problem::UnresolvedModule { |