1
1
use hir:: db:: HirDatabase ;
2
2
use ra_syntax:: {
3
3
ast:: { self , AstNode } ,
4
- SmolStr , SyntaxElement ,
5
- SyntaxKind :: { NAME_REF , USE_ITEM } ,
4
+ SmolStr ,
5
+ SyntaxKind :: USE_ITEM ,
6
6
SyntaxNode ,
7
7
} ;
8
8
@@ -32,25 +32,28 @@ pub(crate) fn auto_import<F: ImportsLocator>(
32
32
ctx : AssistCtx < impl HirDatabase > ,
33
33
imports_locator : & mut F ,
34
34
) -> Option < Assist > {
35
- let path: ast:: Path = ctx. find_node_at_offset ( ) ?;
36
- let module = path. syntax ( ) . ancestors ( ) . find_map ( ast:: Module :: cast) ;
35
+ let path_to_import: ast:: Path = ctx. find_node_at_offset ( ) ?;
36
+ let path_to_import_syntax = path_to_import. syntax ( ) ;
37
+ if path_to_import_syntax. ancestors ( ) . find ( |ancestor| ancestor. kind ( ) == USE_ITEM ) . is_some ( ) {
38
+ return None ;
39
+ }
40
+
41
+ let module = path_to_import_syntax. ancestors ( ) . find_map ( ast:: Module :: cast) ;
37
42
let position = match module. and_then ( |it| it. item_list ( ) ) {
38
43
Some ( item_list) => item_list. syntax ( ) . clone ( ) ,
39
44
None => {
40
- let current_file = path . syntax ( ) . ancestors ( ) . find_map ( ast:: SourceFile :: cast) ?;
45
+ let current_file = path_to_import_syntax . ancestors ( ) . find_map ( ast:: SourceFile :: cast) ?;
41
46
current_file. syntax ( ) . clone ( )
42
47
}
43
48
} ;
44
49
let source_analyzer = ctx. source_analyzer ( & position, None ) ;
45
50
let module_with_name_to_import = source_analyzer. module ( ) ?;
46
- let path_to_import = ctx. covering_element ( ) . ancestors ( ) . find_map ( ast:: Path :: cast) ?;
47
51
if source_analyzer. resolve_path ( ctx. db , & path_to_import) . is_some ( ) {
48
52
return None ;
49
53
}
50
54
51
- let name_to_import = & find_applicable_name_ref ( ctx. covering_element ( ) ) ?. syntax ( ) . to_string ( ) ;
52
55
let proposed_imports = imports_locator
53
- . find_imports ( & name_to_import . to_string ( ) )
56
+ . find_imports ( & path_to_import_syntax . to_string ( ) )
54
57
. into_iter ( )
55
58
. filter_map ( |module_def| module_with_name_to_import. find_use_path ( ctx. db , module_def) )
56
59
. filter ( |use_path| !use_path. segments . is_empty ( ) )
@@ -64,26 +67,11 @@ pub(crate) fn auto_import<F: ImportsLocator>(
64
67
ctx. add_assist_group ( AssistId ( "auto_import" ) , "auto import" , || {
65
68
proposed_imports
66
69
. into_iter ( )
67
- . map ( |import| import_to_action ( import, & position, & path_to_import . syntax ( ) ) )
70
+ . map ( |import| import_to_action ( import, & position, & path_to_import_syntax ) )
68
71
. collect ( )
69
72
} )
70
73
}
71
74
72
- fn find_applicable_name_ref ( element : SyntaxElement ) -> Option < ast:: NameRef > {
73
- if element. ancestors ( ) . find ( |ancestor| ancestor. kind ( ) == USE_ITEM ) . is_some ( ) {
74
- None
75
- } else if element. kind ( ) == NAME_REF {
76
- Some ( element. as_node ( ) . cloned ( ) . and_then ( ast:: NameRef :: cast) ?)
77
- } else {
78
- let parent = element. parent ( ) ?;
79
- if parent. kind ( ) == NAME_REF {
80
- Some ( ast:: NameRef :: cast ( parent) ?)
81
- } else {
82
- None
83
- }
84
- }
85
- }
86
-
87
75
fn import_to_action ( import : String , position : & SyntaxNode , anchor : & SyntaxNode ) -> ActionBuilder {
88
76
let mut action_builder = ActionBuilder :: default ( ) ;
89
77
action_builder. label ( format ! ( "Import `{}`" , & import) ) ;
@@ -110,16 +98,16 @@ mod tests {
110
98
auto_import,
111
99
TestImportsLocator :: new,
112
100
r"
113
- PubStruct <|>
101
+ <|>PubStruct
114
102
115
103
pub mod PubMod {
116
104
pub struct PubStruct;
117
105
}
118
106
" ,
119
107
r"
120
- use PubMod::PubStruct;
108
+ <|> use PubMod::PubStruct;
121
109
122
- PubStruct<|>
110
+ PubStruct
123
111
124
112
pub mod PubMod {
125
113
pub struct PubStruct;
@@ -134,7 +122,7 @@ mod tests {
134
122
auto_import,
135
123
TestImportsLocator :: new,
136
124
r"
137
- PubStruct <|>
125
+ PubSt <|>ruct
138
126
139
127
pub mod PubMod1 {
140
128
pub struct PubStruct;
@@ -149,7 +137,7 @@ mod tests {
149
137
r"
150
138
use PubMod1::PubStruct;
151
139
152
- PubStruct <|>
140
+ PubSt <|>ruct
153
141
154
142
pub mod PubMod1 {
155
143
pub struct PubStruct;
0 commit comments