mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 06:41:48 +00:00
resolve ControlFlow
ourself instead of hard coding.
This commit is contained in:
parent
5818358bbf
commit
93ae993ec4
2 changed files with 31 additions and 10 deletions
|
@ -2,11 +2,17 @@ use std::{hash::BuildHasherDefault, iter};
|
||||||
|
|
||||||
use ast::make;
|
use ast::make;
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{HirDisplay, InFile, Local, Semantics, TypeInfo};
|
use hir::{HirDisplay, InFile, Local, ModuleDef, Semantics, TypeInfo};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
defs::{Definition, NameRefClass},
|
defs::{Definition, NameRefClass},
|
||||||
helpers::insert_use::{insert_use, ImportScope},
|
helpers::{
|
||||||
helpers::node_ext::{preorder_expr, walk_expr, walk_pat, walk_patterns_in_expr},
|
insert_use::{insert_use, ImportScope},
|
||||||
|
FamousDefs,
|
||||||
|
},
|
||||||
|
helpers::{
|
||||||
|
mod_path_to_ast,
|
||||||
|
node_ext::{preorder_expr, walk_expr, walk_pat, walk_patterns_in_expr},
|
||||||
|
},
|
||||||
search::{FileReference, ReferenceCategory, SearchScope},
|
search::{FileReference, ReferenceCategory, SearchScope},
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
|
@ -129,11 +135,20 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option
|
||||||
ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)),
|
ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)),
|
||||||
};
|
};
|
||||||
|
|
||||||
insert_use(
|
let control_flow_enum =
|
||||||
&scope,
|
FamousDefs(&ctx.sema, Some(module.krate())).core_ops_ControlFlow();
|
||||||
make::path_from_text("std::ops::ControlFlow"),
|
|
||||||
&ctx.config.insert_use,
|
if let Some(control_flow_enum) = control_flow_enum {
|
||||||
|
let mod_path = module.find_use_path_prefixed(
|
||||||
|
ctx.sema.db,
|
||||||
|
ModuleDef::from(control_flow_enum),
|
||||||
|
ctx.config.insert_use.prefix_kind,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if let Some(mod_path) = mod_path {
|
||||||
|
insert_use(&scope, mod_path_to_ast(&mod_path), &ctx.config.insert_use);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match ctx.config.snippet_cap {
|
match ctx.config.snippet_cap {
|
||||||
|
@ -3304,6 +3319,7 @@ fn foo() {
|
||||||
check_assist(
|
check_assist(
|
||||||
extract_function,
|
extract_function,
|
||||||
r#"
|
r#"
|
||||||
|
//- minicore: try
|
||||||
fn foo() {
|
fn foo() {
|
||||||
loop {
|
loop {
|
||||||
let mut n = 1;
|
let mut n = 1;
|
||||||
|
@ -3315,7 +3331,7 @@ fn foo() {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
use std::ops::ControlFlow;
|
use core::ops::ControlFlow;
|
||||||
|
|
||||||
fn foo() {
|
fn foo() {
|
||||||
loop {
|
loop {
|
||||||
|
@ -3342,6 +3358,7 @@ fn $0fun_name(n: &mut i32) -> ControlFlow<()> {
|
||||||
check_assist(
|
check_assist(
|
||||||
extract_function,
|
extract_function,
|
||||||
r#"
|
r#"
|
||||||
|
//- minicore: try
|
||||||
fn foo() {
|
fn foo() {
|
||||||
loop {
|
loop {
|
||||||
let mut n = 1;
|
let mut n = 1;
|
||||||
|
@ -3354,7 +3371,7 @@ fn foo() {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
use std::ops::ControlFlow;
|
use core::ops::ControlFlow;
|
||||||
|
|
||||||
fn foo() {
|
fn foo() {
|
||||||
loop {
|
loop {
|
||||||
|
|
|
@ -68,6 +68,10 @@ impl FamousDefs<'_, '_> {
|
||||||
self.find_trait("core:ops:Deref")
|
self.find_trait("core:ops:Deref")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn core_ops_ControlFlow(&self) -> Option<Enum> {
|
||||||
|
self.find_enum("core:ops:ControlFlow")
|
||||||
|
}
|
||||||
|
|
||||||
pub fn alloc(&self) -> Option<Crate> {
|
pub fn alloc(&self) -> Option<Crate> {
|
||||||
self.find_crate("alloc")
|
self.find_crate("alloc")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue