diff options
author | Kirill Bulatov <[email protected]> | 2020-01-24 07:33:18 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-01-26 22:17:10 +0000 |
commit | d0a782ef1c53ef5c5d3b49b02c498f7a688c3a4d (patch) | |
tree | d0fe872ed30d97994efd3ceac708719ce71ae98a /crates/ra_ide/src/imports_locator.rs | |
parent | bef5cf0b9929539e8d9fece006bfd3db1b68bec4 (diff) |
Have a better trait interface
Diffstat (limited to 'crates/ra_ide/src/imports_locator.rs')
-rw-r--r-- | crates/ra_ide/src/imports_locator.rs | 70 |
1 files changed, 24 insertions, 46 deletions
diff --git a/crates/ra_ide/src/imports_locator.rs b/crates/ra_ide/src/imports_locator.rs index e69fb4070..b2fc48159 100644 --- a/crates/ra_ide/src/imports_locator.rs +++ b/crates/ra_ide/src/imports_locator.rs | |||
@@ -3,13 +3,11 @@ | |||
3 | 3 | ||
4 | use crate::{ | 4 | use crate::{ |
5 | db::RootDatabase, | 5 | db::RootDatabase, |
6 | references::{classify_name, classify_name_ref, NameDefinition, NameKind}, | 6 | references::{classify_name, NameDefinition, NameKind}, |
7 | symbol_index::{self, FileSymbol}, | 7 | symbol_index::{self, FileSymbol}, |
8 | Query, | 8 | Query, |
9 | }; | 9 | }; |
10 | use ast::NameRef; | 10 | use hir::{db::HirDatabase, ModuleDef, SourceBinder}; |
11 | use hir::{db::HirDatabase, InFile, ModPath, Module, SourceBinder}; | ||
12 | use itertools::Itertools; | ||
13 | use ra_assists::ImportsLocator; | 11 | use ra_assists::ImportsLocator; |
14 | use ra_prof::profile; | 12 | use ra_prof::profile; |
15 | use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; | 13 | use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; |
@@ -23,14 +21,30 @@ impl<'a> ImportsLocatorIde<'a> { | |||
23 | Self { source_binder: SourceBinder::new(db) } | 21 | Self { source_binder: SourceBinder::new(db) } |
24 | } | 22 | } |
25 | 23 | ||
26 | fn search_for_imports( | 24 | fn get_name_definition( |
27 | &mut self, | 25 | &mut self, |
28 | name_to_import: &ast::NameRef, | 26 | db: &impl HirDatabase, |
29 | module_with_name_to_import: Module, | 27 | import_candidate: &FileSymbol, |
30 | ) -> Vec<ModPath> { | 28 | ) -> Option<NameDefinition> { |
29 | let _p = profile("get_name_definition"); | ||
30 | let file_id = import_candidate.file_id.into(); | ||
31 | let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?); | ||
32 | let candidate_name_node = if candidate_node.kind() != NAME { | ||
33 | candidate_node.children().find(|it| it.kind() == NAME)? | ||
34 | } else { | ||
35 | candidate_node | ||
36 | }; | ||
37 | classify_name( | ||
38 | &mut self.source_binder, | ||
39 | hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? }, | ||
40 | ) | ||
41 | } | ||
42 | } | ||
43 | |||
44 | impl<'a> ImportsLocator for ImportsLocatorIde<'a> { | ||
45 | fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> { | ||
31 | let _p = profile("search_for_imports"); | 46 | let _p = profile("search_for_imports"); |
32 | let db = self.source_binder.db; | 47 | let db = self.source_binder.db; |
33 | let name_to_import = name_to_import.text(); | ||
34 | 48 | ||
35 | let project_results = { | 49 | let project_results = { |
36 | let mut query = Query::new(name_to_import.to_string()); | 50 | let mut query = Query::new(name_to_import.to_string()); |
@@ -52,47 +66,11 @@ impl<'a> ImportsLocatorIde<'a> { | |||
52 | .filter_map(|import_candidate| self.get_name_definition(db, &import_candidate)) | 66 | .filter_map(|import_candidate| self.get_name_definition(db, &import_candidate)) |
53 | .filter_map(|name_definition_to_import| { | 67 | .filter_map(|name_definition_to_import| { |
54 | if let NameKind::Def(module_def) = name_definition_to_import.kind { | 68 | if let NameKind::Def(module_def) = name_definition_to_import.kind { |
55 | module_with_name_to_import.find_use_path(db, module_def) | 69 | Some(module_def) |
56 | } else { | 70 | } else { |
57 | None | 71 | None |
58 | } | 72 | } |
59 | }) | 73 | }) |
60 | .filter(|use_path| !use_path.segments.is_empty()) | ||
61 | .unique() | ||
62 | .take(20) | ||
63 | .collect() | 74 | .collect() |
64 | } | 75 | } |
65 | |||
66 | fn get_name_definition( | ||
67 | &mut self, | ||
68 | db: &impl HirDatabase, | ||
69 | import_candidate: &FileSymbol, | ||
70 | ) -> Option<NameDefinition> { | ||
71 | let _p = profile("get_name_definition"); | ||
72 | let file_id = import_candidate.file_id.into(); | ||
73 | let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?); | ||
74 | let candidate_name_node = if candidate_node.kind() != NAME { | ||
75 | candidate_node.children().find(|it| it.kind() == NAME)? | ||
76 | } else { | ||
77 | candidate_node | ||
78 | }; | ||
79 | classify_name( | ||
80 | &mut self.source_binder, | ||
81 | hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? }, | ||
82 | ) | ||
83 | } | ||
84 | } | ||
85 | |||
86 | impl<'a> ImportsLocator for ImportsLocatorIde<'a> { | ||
87 | fn find_imports( | ||
88 | &mut self, | ||
89 | name_to_import: InFile<&NameRef>, | ||
90 | module_with_name_to_import: Module, | ||
91 | ) -> Option<Vec<ModPath>> { | ||
92 | if classify_name_ref(&mut self.source_binder, name_to_import).is_none() { | ||
93 | Some(self.search_for_imports(name_to_import.value, module_with_name_to_import)) | ||
94 | } else { | ||
95 | None | ||
96 | } | ||
97 | } | ||
98 | } | 76 | } |