Skip to content

Commit 77bbc8d

Browse files
committed
import ControlFlow to the module
1 parent b9a24b2 commit 77bbc8d

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
search::{FileReference, ReferenceAccess, SearchScope},
910
RootDatabase,
1011
};
@@ -85,6 +86,8 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option
8586

8687
let target_range = body.text_range();
8788

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

118121
let fn_def = format_function(ctx, module, &fun, old_indent, new_indent);
119122
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+
120138
match ctx.config.snippet_cap {
121139
Some(cap) => builder.insert_snippet(cap, insert_offset, fn_def),
122140
None => builder.insert(insert_offset, fn_def),
123-
}
141+
};
124142
},
125143
)
126144
}
@@ -3290,6 +3308,8 @@ fn foo() {
32903308
}
32913309
"#,
32923310
r#"
3311+
use std::ops::ControlFlow;
3312+
32933313
fn foo() {
32943314
loop {
32953315
let mut n = 1;
@@ -3327,6 +3347,8 @@ fn foo() {
33273347
}
33283348
"#,
33293349
r#"
3350+
use std::ops::ControlFlow;
3351+
33303352
fn foo() {
33313353
loop {
33323354
let mut n = 1;

0 commit comments

Comments
 (0)