Skip to content

Commit 5818358

Browse files
committed
import ControlFlow to the module
1 parent f888e85 commit 5818358

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use either::Either;
55
use hir::{HirDisplay, InFile, Local, Semantics, TypeInfo};
66
use ide_db::{
77
defs::{Definition, NameRefClass},
8+
helpers::insert_use::{insert_use, ImportScope},
89
helpers::node_ext::{preorder_expr, walk_expr, walk_pat, walk_patterns_in_expr},
910
search::{FileReference, ReferenceCategory, SearchScope},
1011
RootDatabase,
@@ -86,6 +87,8 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option
8687

8788
let target_range = body.text_range();
8889

90+
let scope = ImportScope::find_insert_use_container_with_macros(&node, &ctx.sema)?;
91+
8992
acc.add(
9093
AssistId("extract_function", crate::AssistKind::RefactorExtract),
9194
"Extract into function",
@@ -118,10 +121,25 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option
118121

119122
let fn_def = format_function(ctx, module, &fun, old_indent, new_indent);
120123
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+
121139
match ctx.config.snippet_cap {
122140
Some(cap) => builder.insert_snippet(cap, insert_offset, fn_def),
123141
None => builder.insert(insert_offset, fn_def),
124-
}
142+
};
125143
},
126144
)
127145
}
@@ -3297,6 +3315,8 @@ fn foo() {
32973315
}
32983316
"#,
32993317
r#"
3318+
use std::ops::ControlFlow;
3319+
33003320
fn foo() {
33013321
loop {
33023322
let mut n = 1;
@@ -3334,6 +3354,8 @@ fn foo() {
33343354
}
33353355
"#,
33363356
r#"
3357+
use std::ops::ControlFlow;
3358+
33373359
fn foo() {
33383360
loop {
33393361
let mut n = 1;

0 commit comments

Comments
 (0)