33
44use crate :: {
55 db:: RootDatabase ,
6- references:: { classify_name, classify_name_ref , NameDefinition , NameKind } ,
6+ references:: { classify_name, NameDefinition , NameKind } ,
77 symbol_index:: { self , FileSymbol } ,
88 Query ,
99} ;
10- use ast:: NameRef ;
11- use hir:: { db:: HirDatabase , InFile , ModPath , Module , SourceBinder } ;
12- use itertools:: Itertools ;
10+ use hir:: { db:: HirDatabase , ModuleDef , SourceBinder } ;
1311use ra_assists:: ImportsLocator ;
1412use ra_prof:: profile;
1513use ra_syntax:: { ast, AstNode , SyntaxKind :: NAME } ;
@@ -23,14 +21,30 @@ impl<'a> ImportsLocatorIde<'a> {
2321 Self { source_binder : SourceBinder :: new ( db) }
2422 }
2523
26- fn search_for_imports (
24+ fn get_name_definition (
2725 & mut self ,
28- name_to_import : & ast:: NameRef ,
29- module_with_name_to_import : Module ,
30- ) -> Vec < ModPath > {
26+ db : & impl HirDatabase ,
27+ import_candidate : & FileSymbol ,
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 > {
3146 let _p = profile ( "search_for_imports" ) ;
3247 let db = self . source_binder . db ;
33- let name_to_import = name_to_import. text ( ) ;
3448
3549 let project_results = {
3650 let mut query = Query :: new ( name_to_import. to_string ( ) ) ;
@@ -52,47 +66,11 @@ impl<'a> ImportsLocatorIde<'a> {
5266 . filter_map ( |import_candidate| self . get_name_definition ( db, & import_candidate) )
5367 . filter_map ( |name_definition_to_import| {
5468 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)
5670 } else {
5771 None
5872 }
5973 } )
60- . filter ( |use_path| !use_path. segments . is_empty ( ) )
61- . unique ( )
62- . take ( 20 )
6374 . collect ( )
6475 }
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- }
9876}
0 commit comments