aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db/src/imports_locator.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-02-18 17:35:10 +0000
committerAleksey Kladov <[email protected]>2020-02-26 11:55:50 +0000
commitc3a4c4429de83450654795534e64e878a774a088 (patch)
tree12d89798f61b276f8bd640db07276a7d4e92b1c2 /crates/ra_ide_db/src/imports_locator.rs
parent04deae3dba7c9b7054f7a1d64e4b93a05aecc132 (diff)
Refactor primary IDE API
This introduces the new type -- Semantics. Semantics maps SyntaxNodes to various semantic info, such as type, name resolution or macro expansions. To do so, Semantics maintains a HashMap which maps every node it saw to the file from which the node originated. This is enough to get all the necessary hir bits just from syntax.
Diffstat (limited to 'crates/ra_ide_db/src/imports_locator.rs')
-rw-r--r--crates/ra_ide_db/src/imports_locator.rs26
1 files changed, 10 insertions, 16 deletions
diff --git a/crates/ra_ide_db/src/imports_locator.rs b/crates/ra_ide_db/src/imports_locator.rs
index b8dd358a9..e590d2a5c 100644
--- a/crates/ra_ide_db/src/imports_locator.rs
+++ b/crates/ra_ide_db/src/imports_locator.rs
@@ -1,7 +1,7 @@
1//! This module contains an import search funcionality that is provided to the ra_assists module. 1//! This module contains an import search funcionality that is provided to the ra_assists module.
2//! Later, this should be moved away to a separate crate that is accessible from the ra_assists module. 2//! Later, this should be moved away to a separate crate that is accessible from the ra_assists module.
3 3
4use hir::{db::HirDatabase, ModuleDef, SourceBinder}; 4use hir::{ModuleDef, Semantics};
5use ra_prof::profile; 5use ra_prof::profile;
6use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; 6use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
7 7
@@ -12,17 +12,17 @@ use crate::{
12}; 12};
13 13
14pub struct ImportsLocator<'a> { 14pub struct ImportsLocator<'a> {
15 source_binder: SourceBinder<'a, RootDatabase>, 15 sema: Semantics<'a, RootDatabase>,
16} 16}
17 17
18impl<'a> ImportsLocator<'a> { 18impl<'a> ImportsLocator<'a> {
19 pub fn new(db: &'a RootDatabase) -> Self { 19 pub fn new(db: &'a RootDatabase) -> Self {
20 Self { source_binder: SourceBinder::new(db) } 20 Self { sema: Semantics::new(db) }
21 } 21 }
22 22
23 pub fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> { 23 pub fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> {
24 let _p = profile("search_for_imports"); 24 let _p = profile("search_for_imports");
25 let db = self.source_binder.db; 25 let db = self.sema.db;
26 26
27 let project_results = { 27 let project_results = {
28 let mut query = Query::new(name_to_import.to_string()); 28 let mut query = Query::new(name_to_import.to_string());
@@ -41,7 +41,7 @@ impl<'a> ImportsLocator<'a> {
41 project_results 41 project_results
42 .into_iter() 42 .into_iter()
43 .chain(lib_results.into_iter()) 43 .chain(lib_results.into_iter())
44 .filter_map(|import_candidate| self.get_name_definition(db, &import_candidate)) 44 .filter_map(|import_candidate| self.get_name_definition(&import_candidate))
45 .filter_map(|name_definition_to_import| match name_definition_to_import { 45 .filter_map(|name_definition_to_import| match name_definition_to_import {
46 NameDefinition::ModuleDef(module_def) => Some(module_def), 46 NameDefinition::ModuleDef(module_def) => Some(module_def),
47 _ => None, 47 _ => None,
@@ -49,22 +49,16 @@ impl<'a> ImportsLocator<'a> {
49 .collect() 49 .collect()
50 } 50 }
51 51
52 fn get_name_definition( 52 fn get_name_definition(&mut self, import_candidate: &FileSymbol) -> Option<NameDefinition> {
53 &mut self,
54 db: &impl HirDatabase,
55 import_candidate: &FileSymbol,
56 ) -> Option<NameDefinition> {
57 let _p = profile("get_name_definition"); 53 let _p = profile("get_name_definition");
58 let file_id = import_candidate.file_id.into(); 54 let file_id = import_candidate.file_id;
59 let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?); 55
56 let candidate_node = import_candidate.ptr.to_node(self.sema.parse(file_id).syntax());
60 let candidate_name_node = if candidate_node.kind() != NAME { 57 let candidate_name_node = if candidate_node.kind() != NAME {
61 candidate_node.children().find(|it| it.kind() == NAME)? 58 candidate_node.children().find(|it| it.kind() == NAME)?
62 } else { 59 } else {
63 candidate_node 60 candidate_node
64 }; 61 };
65 classify_name( 62 classify_name(&self.sema, &ast::Name::cast(candidate_name_node)?)
66 &mut self.source_binder,
67 hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? },
68 )
69 } 63 }
70} 64}