apply code review suggestions

This commit is contained in:
Paweł Palenica 2021-10-21 23:41:43 -07:00
parent 91988f46b7
commit bfc86f64c3
2 changed files with 51 additions and 7 deletions

View File

@ -12,7 +12,7 @@ use crate::{
// Assist: qualify_method_call // Assist: qualify_method_call
// //
// If the name is resolvable, provides fully qualified path for it. // Replaces the method call with a qualified function call.
// //
// ``` // ```
// struct Foo; // struct Foo;
@ -36,8 +36,10 @@ use crate::{
// } // }
// ``` // ```
pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let call: ast::MethodCallExpr = ctx.find_node_at_offset()?; let name: ast::NameRef = ctx.find_node_at_offset()?;
let fn_name = &call.name_ref()?; let call = name.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
let ident = name.ident_token()?;
let range = call.syntax().text_range(); let range = call.syntax().text_range();
let resolved_call = ctx.sema.resolve_method_call(&call)?; let resolved_call = ctx.sema.resolve_method_call(&call)?;
@ -52,7 +54,7 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext) -> Opt
acc.add( acc.add(
AssistId("qualify_method_call", AssistKind::RefactorInline), AssistId("qualify_method_call", AssistKind::RefactorInline),
format!("Qualify call `{}`", fn_name), format!("Qualify `{}` method call", ident.text()),
range, range,
|builder| { |builder| {
qualify_candidate.qualify( qualify_candidate.qualify(
@ -68,7 +70,7 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext) -> Opt
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::tests::check_assist; use crate::tests::{check_assist, check_assist_not_applicable};
#[test] #[test]
fn struct_method() { fn struct_method() {
@ -480,6 +482,49 @@ fn main() {
let test_struct = TestStruct {}; let test_struct = TestStruct {};
TestTrait::test_method::<()>(&test_struct) TestTrait::test_method::<()>(&test_struct)
} }
"#,
);
}
#[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()
}
"#, "#,
); );
} }

View File

@ -171,8 +171,7 @@ impl QualifyCandidate<'_> {
let trait_method_name = mcall_expr.name_ref()?; let trait_method_name = mcall_expr.name_ref()?;
let trait_ = item_as_trait(db, item)?; let trait_ = item_as_trait(db, item)?;
let method = find_trait_method(db, trait_, &trait_method_name)?; let method = find_trait_method(db, trait_, &trait_method_name)?;
Self::qualify_fn_call(db, mcall_expr, replacer, import, &method); Self::qualify_fn_call(db, mcall_expr, replacer, import, &method)
Some(())
} }
} }