diff options
author | Andrea Pretto <[email protected]> | 2019-04-15 15:11:32 +0100 |
---|---|---|
committer | Andrea Pretto <[email protected]> | 2019-04-21 23:14:58 +0100 |
commit | 914421495835380b96e0016763fda6eff31a8179 (patch) | |
tree | 86b348e13e9272f1320aad0416313c0f6f93a19c /crates/ra_hir | |
parent | cf0eff2e332f46eda4fcecb043854c9c0d710e4e (diff) |
complete_import: add new import resolver infrastructure with some hardcoded importable name.
Changes complete_scope to support that.
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/resolve.rs | 65 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 19 |
3 files changed, 84 insertions, 2 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 4411715de..f156e3f07 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -52,7 +52,7 @@ use crate::{ | |||
52 | db::{HirDatabase, DefDatabase}, | 52 | db::{HirDatabase, DefDatabase}, |
53 | name::{AsName, KnownName}, | 53 | name::{AsName, KnownName}, |
54 | source_id::{FileAstId, AstId}, | 54 | source_id::{FileAstId, AstId}, |
55 | resolve::Resolver, | 55 | resolve::Resolver, resolve::ImportResolver, |
56 | }; | 56 | }; |
57 | 57 | ||
58 | pub use self::{ | 58 | pub use self::{ |
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index f2c85eb66..0f866e6c2 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs | |||
@@ -3,6 +3,8 @@ use std::sync::Arc; | |||
3 | 3 | ||
4 | use rustc_hash::FxHashMap; | 4 | use rustc_hash::FxHashMap; |
5 | 5 | ||
6 | use ra_syntax::SmolStr; | ||
7 | |||
6 | use crate::{ | 8 | use crate::{ |
7 | ModuleDef, | 9 | ModuleDef, |
8 | code_model_api::Crate, | 10 | code_model_api::Crate, |
@@ -12,8 +14,12 @@ use crate::{ | |||
12 | generics::GenericParams, | 14 | generics::GenericParams, |
13 | expr::{scope::{ExprScopes, ScopeId}, PatId}, | 15 | expr::{scope::{ExprScopes, ScopeId}, PatId}, |
14 | impl_block::ImplBlock, | 16 | impl_block::ImplBlock, |
17 | <<<<<<< HEAD | ||
15 | path::Path, | 18 | path::Path, |
16 | Trait | 19 | Trait |
20 | ======= | ||
21 | path::Path, Trait, | ||
22 | >>>>>>> complete_import: add new import resolver infrastructure with some hardcoded importable name. | ||
17 | }; | 23 | }; |
18 | 24 | ||
19 | #[derive(Debug, Clone, Default)] | 25 | #[derive(Debug, Clone, Default)] |
@@ -21,6 +27,12 @@ pub(crate) struct Resolver { | |||
21 | scopes: Vec<Scope>, | 27 | scopes: Vec<Scope>, |
22 | } | 28 | } |
23 | 29 | ||
30 | #[derive(Debug, Clone, Default)] | ||
31 | pub(crate) struct ImportResolver { | ||
32 | // todo: use fst crate or something like that | ||
33 | dummy_names: Vec<(SmolStr, Vec<SmolStr>)>, | ||
34 | } | ||
35 | |||
24 | // FIXME how to store these best | 36 | // FIXME how to store these best |
25 | #[derive(Debug, Clone)] | 37 | #[derive(Debug, Clone)] |
26 | pub(crate) struct ModuleItemMap { | 38 | pub(crate) struct ModuleItemMap { |
@@ -309,3 +321,56 @@ impl Scope { | |||
309 | } | 321 | } |
310 | } | 322 | } |
311 | } | 323 | } |
324 | |||
325 | impl ImportResolver { | ||
326 | pub(crate) fn new() -> Self { | ||
327 | let dummy_names = vec![ | ||
328 | (SmolStr::new("fmt"), vec![SmolStr::new("std"), SmolStr::new("fmt")]), | ||
329 | (SmolStr::new("io"), vec![SmolStr::new("std"), SmolStr::new("io")]), | ||
330 | (SmolStr::new("iter"), vec![SmolStr::new("std"), SmolStr::new("iter")]), | ||
331 | (SmolStr::new("hash"), vec![SmolStr::new("std"), SmolStr::new("hash")]), | ||
332 | ( | ||
333 | SmolStr::new("Debug"), | ||
334 | vec![SmolStr::new("std"), SmolStr::new("fmt"), SmolStr::new("Debug")], | ||
335 | ), | ||
336 | ( | ||
337 | SmolStr::new("Display"), | ||
338 | vec![SmolStr::new("std"), SmolStr::new("fmt"), SmolStr::new("Display")], | ||
339 | ), | ||
340 | ( | ||
341 | SmolStr::new("Hash"), | ||
342 | vec![SmolStr::new("std"), SmolStr::new("hash"), SmolStr::new("Hash")], | ||
343 | ), | ||
344 | ( | ||
345 | SmolStr::new("Hasher"), | ||
346 | vec![SmolStr::new("std"), SmolStr::new("hash"), SmolStr::new("Hasher")], | ||
347 | ), | ||
348 | ( | ||
349 | SmolStr::new("Iterator"), | ||
350 | vec![SmolStr::new("std"), SmolStr::new("iter"), SmolStr::new("Iterator")], | ||
351 | ), | ||
352 | ]; | ||
353 | |||
354 | ImportResolver { dummy_names } | ||
355 | } | ||
356 | |||
357 | // Returns a map of importable items filtered by name. | ||
358 | // The map associates item name with its full path. | ||
359 | // todo: should return Resolutions | ||
360 | pub(crate) fn all_names( | ||
361 | &self, | ||
362 | _db: &impl HirDatabase, | ||
363 | name: &Name, | ||
364 | ) -> FxHashMap<SmolStr, Vec<SmolStr>> { | ||
365 | let name = name.to_smolstr(); | ||
366 | if name.len() > 1 { | ||
367 | self.dummy_names | ||
368 | .iter() | ||
369 | .filter(|(n, _)| n.as_str().contains(name.as_str())) | ||
370 | .cloned() | ||
371 | .collect() | ||
372 | } else { | ||
373 | FxHashMap::default() | ||
374 | } | ||
375 | } | ||
376 | } | ||
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index f1bb13bc6..a6f0ab289 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -14,14 +14,19 @@ use ra_syntax::{ | |||
14 | ast::{self, AstNode, NameOwner}, | 14 | ast::{self, AstNode, NameOwner}, |
15 | algo::find_node_at_offset, | 15 | algo::find_node_at_offset, |
16 | SyntaxKind::*, | 16 | SyntaxKind::*, |
17 | SmolStr, | ||
17 | }; | 18 | }; |
18 | 19 | ||
19 | use crate::{ | 20 | use crate::{ |
20 | HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, PerNs, Name, | 21 | HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, PerNs, Name, |
22 | <<<<<<< HEAD | ||
21 | AsName, Module, HirFileId, Crate, Trait, Resolver, Ty, | 23 | AsName, Module, HirFileId, Crate, Trait, Resolver, Ty, |
24 | ======= | ||
25 | AsName, Module, HirFileId, Crate, Trait, Resolver, ImportResolver, | ||
26 | >>>>>>> complete_import: add new import resolver infrastructure with some hardcoded importable name. | ||
22 | expr::{BodySourceMap, scope::{ScopeId, ExprScopes}}, | 27 | expr::{BodySourceMap, scope::{ScopeId, ExprScopes}}, |
23 | ids::LocationCtx, | 28 | ids::LocationCtx, |
24 | expr, AstId | 29 | expr, AstId, |
25 | }; | 30 | }; |
26 | 31 | ||
27 | /// Locates the module by `FileId`. Picks topmost module in the file. | 32 | /// Locates the module by `FileId`. Picks topmost module in the file. |
@@ -170,6 +175,7 @@ fn def_with_body_from_child_node( | |||
170 | #[derive(Debug)] | 175 | #[derive(Debug)] |
171 | pub struct SourceAnalyzer { | 176 | pub struct SourceAnalyzer { |
172 | resolver: Resolver, | 177 | resolver: Resolver, |
178 | import_resolver: ImportResolver, | ||
173 | body_source_map: Option<Arc<BodySourceMap>>, | 179 | body_source_map: Option<Arc<BodySourceMap>>, |
174 | infer: Option<Arc<crate::ty::InferenceResult>>, | 180 | infer: Option<Arc<crate::ty::InferenceResult>>, |
175 | scopes: Option<Arc<crate::expr::ExprScopes>>, | 181 | scopes: Option<Arc<crate::expr::ExprScopes>>, |
@@ -217,6 +223,7 @@ impl SourceAnalyzer { | |||
217 | offset: Option<TextUnit>, | 223 | offset: Option<TextUnit>, |
218 | ) -> SourceAnalyzer { | 224 | ) -> SourceAnalyzer { |
219 | let def_with_body = def_with_body_from_child_node(db, file_id, node); | 225 | let def_with_body = def_with_body_from_child_node(db, file_id, node); |
226 | let import_resolver = ImportResolver::new(); | ||
220 | if let Some(def) = def_with_body { | 227 | if let Some(def) = def_with_body { |
221 | let source_map = def.body_source_map(db); | 228 | let source_map = def.body_source_map(db); |
222 | let scopes = db.expr_scopes(def); | 229 | let scopes = db.expr_scopes(def); |
@@ -227,6 +234,7 @@ impl SourceAnalyzer { | |||
227 | let resolver = expr::resolver_for_scope(def.body(db), db, scope); | 234 | let resolver = expr::resolver_for_scope(def.body(db), db, scope); |
228 | SourceAnalyzer { | 235 | SourceAnalyzer { |
229 | resolver, | 236 | resolver, |
237 | import_resolver, | ||
230 | body_source_map: Some(source_map), | 238 | body_source_map: Some(source_map), |
231 | infer: Some(def.infer(db)), | 239 | infer: Some(def.infer(db)), |
232 | scopes: Some(scopes), | 240 | scopes: Some(scopes), |
@@ -237,6 +245,7 @@ impl SourceAnalyzer { | |||
237 | .ancestors() | 245 | .ancestors() |
238 | .find_map(|node| try_get_resolver_for_node(db, file_id, node)) | 246 | .find_map(|node| try_get_resolver_for_node(db, file_id, node)) |
239 | .unwrap_or_default(), | 247 | .unwrap_or_default(), |
248 | import_resolver, | ||
240 | body_source_map: None, | 249 | body_source_map: None, |
241 | infer: None, | 250 | infer: None, |
242 | scopes: None, | 251 | scopes: None, |
@@ -323,6 +332,14 @@ impl SourceAnalyzer { | |||
323 | self.resolver.all_names(db) | 332 | self.resolver.all_names(db) |
324 | } | 333 | } |
325 | 334 | ||
335 | pub fn all_import_names( | ||
336 | &self, | ||
337 | db: &impl HirDatabase, | ||
338 | name: &Name, | ||
339 | ) -> FxHashMap<SmolStr, Vec<SmolStr>> { | ||
340 | self.import_resolver.all_names(db, name) | ||
341 | } | ||
342 | |||
326 | pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> { | 343 | pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> { |
327 | // FIXME: at least, this should work with any DefWithBody, but ideally | 344 | // FIXME: at least, this should work with any DefWithBody, but ideally |
328 | // this should be hir-based altogether | 345 | // this should be hir-based altogether |