@@ -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
search:: { FileReference , ReferenceAccess , SearchScope } ,
9
10
RootDatabase ,
10
11
} ;
@@ -85,6 +86,8 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option
85
86
86
87
let target_range = body. text_range ( ) ;
87
88
89
+ let scope = ImportScope :: find_insert_use_container_with_macros ( & node, & ctx. sema ) ?;
90
+
88
91
acc. add (
89
92
AssistId ( "extract_function" , crate :: AssistKind :: RefactorExtract ) ,
90
93
"Extract into function" ,
@@ -117,10 +120,25 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option
117
120
118
121
let fn_def = format_function ( ctx, module, & fun, old_indent, new_indent) ;
119
122
let insert_offset = insert_after. text_range ( ) . end ( ) ;
123
+
124
+ if fn_def. contains ( "ControlFlow" ) {
125
+ let scope = match scope {
126
+ ImportScope :: File ( it) => ImportScope :: File ( builder. make_mut ( it) ) ,
127
+ ImportScope :: Module ( it) => ImportScope :: Module ( builder. make_mut ( it) ) ,
128
+ ImportScope :: Block ( it) => ImportScope :: Block ( builder. make_mut ( it) ) ,
129
+ } ;
130
+
131
+ insert_use (
132
+ & scope,
133
+ make:: path_from_text ( "std::ops::ControlFlow" ) ,
134
+ & ctx. config . insert_use ,
135
+ ) ;
136
+ }
137
+
120
138
match ctx. config . snippet_cap {
121
139
Some ( cap) => builder. insert_snippet ( cap, insert_offset, fn_def) ,
122
140
None => builder. insert ( insert_offset, fn_def) ,
123
- }
141
+ } ;
124
142
} ,
125
143
)
126
144
}
@@ -3290,6 +3308,8 @@ fn foo() {
3290
3308
}
3291
3309
"# ,
3292
3310
r#"
3311
+ use std::ops::ControlFlow;
3312
+
3293
3313
fn foo() {
3294
3314
loop {
3295
3315
let mut n = 1;
@@ -3327,6 +3347,8 @@ fn foo() {
3327
3347
}
3328
3348
"# ,
3329
3349
r#"
3350
+ use std::ops::ControlFlow;
3351
+
3330
3352
fn foo() {
3331
3353
loop {
3332
3354
let mut n = 1;
0 commit comments