2021-10-21 01:28:19 -05:00
|
|
|
use hir::{ItemInNs, ModuleDef};
|
2021-10-21 01:35:14 -05:00
|
|
|
use ide_db::{
|
|
|
|
assists::{AssistId, AssistKind},
|
|
|
|
helpers::import_assets::item_for_path_search,
|
|
|
|
};
|
|
|
|
use syntax::{ast, AstNode};
|
2021-10-21 01:28:19 -05:00
|
|
|
|
2021-10-21 01:35:14 -05:00
|
|
|
use crate::{
|
|
|
|
assist_context::{AssistContext, Assists},
|
|
|
|
handlers::qualify_path::QualifyCandidate,
|
|
|
|
};
|
2021-10-21 01:28:19 -05:00
|
|
|
|
|
|
|
// Assist: qualify_method_call
|
|
|
|
//
|
2021-10-22 01:41:43 -05:00
|
|
|
// Replaces the method call with a qualified function call.
|
2021-10-21 01:28:19 -05:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// struct Foo;
|
|
|
|
// impl Foo {
|
2021-10-21 01:35:14 -05:00
|
|
|
// fn foo(&self) {}
|
2021-10-21 01:28:19 -05:00
|
|
|
// }
|
|
|
|
// fn main() {
|
|
|
|
// let foo = Foo;
|
|
|
|
// foo.fo$0o();
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// struct Foo;
|
|
|
|
// impl Foo {
|
2021-10-21 01:35:14 -05:00
|
|
|
// fn foo(&self) {}
|
2021-10-21 01:28:19 -05:00
|
|
|
// }
|
|
|
|
// fn main() {
|
|
|
|
// let foo = Foo;
|
|
|
|
// Foo::foo(&foo);
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2021-10-22 01:41:43 -05:00
|
|
|
let name: ast::NameRef = ctx.find_node_at_offset()?;
|
|
|
|
let call = name.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
|
|
|
|
|
|
|
|
let ident = name.ident_token()?;
|
2021-10-21 01:35:14 -05:00
|
|
|
|
2021-10-21 01:28:19 -05:00
|
|
|
let range = call.syntax().text_range();
|
|
|
|
let resolved_call = ctx.sema.resolve_method_call(&call)?;
|
|
|
|
|
|
|
|
let current_module = ctx.sema.scope(&call.syntax()).module()?;
|
|
|
|
let target_module_def = ModuleDef::from(resolved_call);
|
|
|
|
let item_in_ns = ItemInNs::from(target_module_def);
|
2021-10-21 01:35:14 -05:00
|
|
|
let receiver_path = current_module
|
|
|
|
.find_use_path(ctx.sema.db, item_for_path_search(ctx.sema.db, item_in_ns)?)?;
|
2021-10-21 01:28:19 -05:00
|
|
|
|
|
|
|
let qualify_candidate = QualifyCandidate::ImplMethod(ctx.sema.db, call, resolved_call);
|
2021-10-21 01:35:14 -05:00
|
|
|
|
2021-10-21 01:28:19 -05:00
|
|
|
acc.add(
|
|
|
|
AssistId("qualify_method_call", AssistKind::RefactorInline),
|
2021-10-22 01:41:43 -05:00
|
|
|
format!("Qualify `{}` method call", ident.text()),
|
2021-10-21 01:28:19 -05:00
|
|
|
range,
|
|
|
|
|builder| {
|
|
|
|
qualify_candidate.qualify(
|
|
|
|
|replace_with: String| builder.replace(range, replace_with),
|
|
|
|
&receiver_path,
|
2021-10-21 01:35:14 -05:00
|
|
|
item_in_ns,
|
2021-10-21 01:28:19 -05:00
|
|
|
)
|
2021-10-21 01:35:14 -05:00
|
|
|
},
|
2021-10-21 01:28:19 -05:00
|
|
|
);
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-10-22 01:41:43 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2021-10-21 01:28:19 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_method() {
|
|
|
|
check_assist(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo {};
|
|
|
|
foo.fo$0o()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo {};
|
|
|
|
Foo::foo(&foo)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_method_multi_params() {
|
|
|
|
check_assist(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self, p1: i32, p2: u32) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo {};
|
|
|
|
foo.fo$0o(9, 9u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self, p1: i32, p2: u32) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo {};
|
|
|
|
Foo::foo(&foo, 9, 9u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_method_consume() {
|
|
|
|
check_assist(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(self, p1: i32, p2: u32) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo {};
|
|
|
|
foo.fo$0o(9, 9u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(self, p1: i32, p2: u32) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo {};
|
|
|
|
Foo::foo(foo, 9, 9u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_method_exclusive() {
|
|
|
|
check_assist(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&mut self, p1: i32, p2: u32) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo {};
|
|
|
|
foo.fo$0o(9, 9u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&mut self, p1: i32, p2: u32) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo {};
|
|
|
|
Foo::foo(&mut foo, 9, 9u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_method_cross_crate() {
|
|
|
|
check_assist(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
|
|
|
let foo = dep::test_mod::Foo {};
|
|
|
|
foo.fo$0o(9, 9u)
|
|
|
|
}
|
|
|
|
//- /dep.rs crate:dep
|
|
|
|
pub mod test_mod {
|
|
|
|
pub struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
pub fn foo(&mut self, p1: i32, p2: u32) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let foo = dep::test_mod::Foo {};
|
|
|
|
dep::test_mod::Foo::foo(&mut foo, 9, 9u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_method_generic() {
|
|
|
|
check_assist(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo<T>(&self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo {};
|
|
|
|
foo.fo$0o::<()>()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo<T>(&self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo {};
|
|
|
|
Foo::foo::<()>(&foo)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trait_method() {
|
|
|
|
check_assist(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(&self);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(&self) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = test_mod::TestStruct {};
|
|
|
|
test_struct.test_meth$0od()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(&self);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(&self) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = test_mod::TestStruct {};
|
|
|
|
TestTrait::test_method(&test_struct)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trait_method_multi_params() {
|
|
|
|
check_assist(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(&self, p1: i32, p2: u32);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(&self, p1: i32, p2: u32) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = test_mod::TestStruct {};
|
|
|
|
test_struct.test_meth$0od(12, 32u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(&self, p1: i32, p2: u32);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(&self, p1: i32, p2: u32) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = test_mod::TestStruct {};
|
|
|
|
TestTrait::test_method(&test_struct, 12, 32u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trait_method_consume() {
|
|
|
|
check_assist(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(self, p1: i32, p2: u32);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(self, p1: i32, p2: u32) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = test_mod::TestStruct {};
|
|
|
|
test_struct.test_meth$0od(12, 32u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(self, p1: i32, p2: u32);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(self, p1: i32, p2: u32) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = test_mod::TestStruct {};
|
|
|
|
TestTrait::test_method(test_struct, 12, 32u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trait_method_exclusive() {
|
|
|
|
check_assist(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(&mut self, p1: i32, p2: u32);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(&mut self, p1: i32, p2: u32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = test_mod::TestStruct {};
|
|
|
|
test_struct.test_meth$0od(12, 32u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(&mut self, p1: i32, p2: u32);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(&mut self, p1: i32, p2: u32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = test_mod::TestStruct {};
|
|
|
|
TestTrait::test_method(&mut test_struct, 12, 32u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trait_method_cross_crate() {
|
|
|
|
check_assist(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn main() {
|
|
|
|
let foo = dep::test_mod::Foo {};
|
|
|
|
foo.fo$0o(9, 9u)
|
|
|
|
}
|
|
|
|
//- /dep.rs crate:dep
|
|
|
|
pub mod test_mod {
|
|
|
|
pub struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
pub fn foo(&mut self, p1: i32, p2: u32) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let foo = dep::test_mod::Foo {};
|
|
|
|
dep::test_mod::Foo::foo(&mut foo, 9, 9u)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trait_method_generic() {
|
|
|
|
check_assist(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method<T>(&self);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method<T>(&self) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = TestStruct {};
|
|
|
|
test_struct.test_meth$0od::<()>()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method<T>(&self);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method<T>(&self) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = TestStruct {};
|
|
|
|
TestTrait::test_method::<()>(&test_struct)
|
|
|
|
}
|
2021-10-22 01:41:43 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_method_over_stuct_instance() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo {};
|
|
|
|
f$0oo.foo()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trait_method_over_stuct_instance() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
qualify_method_call,
|
|
|
|
r#"
|
|
|
|
mod test_mod {
|
|
|
|
pub trait TestTrait {
|
|
|
|
fn test_method(&self);
|
|
|
|
}
|
|
|
|
pub struct TestStruct {}
|
|
|
|
impl TestTrait for TestStruct {
|
|
|
|
fn test_method(&self) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use test_mod::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test_struct = test_mod::TestStruct {};
|
|
|
|
tes$0t_struct.test_method()
|
|
|
|
}
|
2021-10-21 01:28:19 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|