@@ -5,6 +5,7 @@ use either::Either;
5
5
use hir:: { HirDisplay , InFile , Local , Semantics , TypeInfo } ;
6
6
use ide_db:: {
7
7
defs:: { Definition , NameRefClass } ,
8
+ helpers:: insert_use:: { insert_use, ImportScope } ,
8
9
helpers:: node_ext:: { preorder_expr, walk_expr, walk_pat, walk_patterns_in_expr} ,
9
10
search:: { FileReference , ReferenceCategory , SearchScope } ,
10
11
RootDatabase ,
@@ -86,6 +87,8 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option
86
87
87
88
let target_range = body. text_range ( ) ;
88
89
90
+ let scope = ImportScope :: find_insert_use_container_with_macros ( & node, & ctx. sema ) ?;
91
+
89
92
acc. add (
90
93
AssistId ( "extract_function" , crate :: AssistKind :: RefactorExtract ) ,
91
94
"Extract into function" ,
@@ -118,10 +121,25 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option
118
121
119
122
let fn_def = format_function ( ctx, module, & fun, old_indent, new_indent) ;
120
123
let insert_offset = insert_after. text_range ( ) . end ( ) ;
124
+
125
+ if fn_def. contains ( "ControlFlow" ) {
126
+ let scope = match scope {
127
+ ImportScope :: File ( it) => ImportScope :: File ( builder. make_mut ( it) ) ,
128
+ ImportScope :: Module ( it) => ImportScope :: Module ( builder. make_mut ( it) ) ,
129
+ ImportScope :: Block ( it) => ImportScope :: Block ( builder. make_mut ( it) ) ,
130
+ } ;
131
+
132
+ insert_use (
133
+ & scope,
134
+ make:: path_from_text ( "std::ops::ControlFlow" ) ,
135
+ & ctx. config . insert_use ,
136
+ ) ;
137
+ }
138
+
121
139
match ctx. config . snippet_cap {
122
140
Some ( cap) => builder. insert_snippet ( cap, insert_offset, fn_def) ,
123
141
None => builder. insert ( insert_offset, fn_def) ,
124
- }
142
+ } ;
125
143
} ,
126
144
)
127
145
}
@@ -3297,6 +3315,8 @@ fn foo() {
3297
3315
}
3298
3316
"# ,
3299
3317
r#"
3318
+ use std::ops::ControlFlow;
3319
+
3300
3320
fn foo() {
3301
3321
loop {
3302
3322
let mut n = 1;
@@ -3334,6 +3354,8 @@ fn foo() {
3334
3354
}
3335
3355
"# ,
3336
3356
r#"
3357
+ use std::ops::ControlFlow;
3358
+
3337
3359
fn foo() {
3338
3360
loop {
3339
3361
let mut n = 1;
0 commit comments