Skip to content

Commit 93ae993

Browse files
committed
resolve ControlFlow ourself instead of hard coding.
1 parent 5818358 commit 93ae993

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ use std::{hash::BuildHasherDefault, iter};
22

33
use ast::make;
44
use either::Either;
5-
use hir::{HirDisplay, InFile, Local, Semantics, TypeInfo};
5+
use hir::{HirDisplay, InFile, Local, ModuleDef, Semantics, TypeInfo};
66
use ide_db::{
77
defs::{Definition, NameRefClass},
8-
helpers::insert_use::{insert_use, ImportScope},
9-
helpers::node_ext::{preorder_expr, walk_expr, walk_pat, walk_patterns_in_expr},
8+
helpers::{
9+
insert_use::{insert_use, ImportScope},
10+
FamousDefs,
11+
},
12+
helpers::{
13+
mod_path_to_ast,
14+
node_ext::{preorder_expr, walk_expr, walk_pat, walk_patterns_in_expr},
15+
},
1016
search::{FileReference, ReferenceCategory, SearchScope},
1117
RootDatabase,
1218
};
@@ -129,11 +135,20 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option
129135
ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)),
130136
};
131137

132-
insert_use(
133-
&scope,
134-
make::path_from_text("std::ops::ControlFlow"),
135-
&ctx.config.insert_use,
136-
);
138+
let control_flow_enum =
139+
FamousDefs(&ctx.sema, Some(module.krate())).core_ops_ControlFlow();
140+
141+
if let Some(control_flow_enum) = control_flow_enum {
142+
let mod_path = module.find_use_path_prefixed(
143+
ctx.sema.db,
144+
ModuleDef::from(control_flow_enum),
145+
ctx.config.insert_use.prefix_kind,
146+
);
147+
148+
if let Some(mod_path) = mod_path {
149+
insert_use(&scope, mod_path_to_ast(&mod_path), &ctx.config.insert_use);
150+
}
151+
}
137152
}
138153

139154
match ctx.config.snippet_cap {
@@ -3304,6 +3319,7 @@ fn foo() {
33043319
check_assist(
33053320
extract_function,
33063321
r#"
3322+
//- minicore: try
33073323
fn foo() {
33083324
loop {
33093325
let mut n = 1;
@@ -3315,7 +3331,7 @@ fn foo() {
33153331
}
33163332
"#,
33173333
r#"
3318-
use std::ops::ControlFlow;
3334+
use core::ops::ControlFlow;
33193335
33203336
fn foo() {
33213337
loop {
@@ -3342,6 +3358,7 @@ fn $0fun_name(n: &mut i32) -> ControlFlow<()> {
33423358
check_assist(
33433359
extract_function,
33443360
r#"
3361+
//- minicore: try
33453362
fn foo() {
33463363
loop {
33473364
let mut n = 1;
@@ -3354,7 +3371,7 @@ fn foo() {
33543371
}
33553372
"#,
33563373
r#"
3357-
use std::ops::ControlFlow;
3374+
use core::ops::ControlFlow;
33583375
33593376
fn foo() {
33603377
loop {

crates/ide_db/src/helpers/famous_defs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ impl FamousDefs<'_, '_> {
6868
self.find_trait("core:ops:Deref")
6969
}
7070

71+
pub fn core_ops_ControlFlow(&self) -> Option<Enum> {
72+
self.find_enum("core:ops:ControlFlow")
73+
}
74+
7175
pub fn alloc(&self) -> Option<Crate> {
7276
self.find_crate("alloc")
7377
}

0 commit comments

Comments
 (0)