Skip to content

Commit bfc86f6

Browse files
committed
apply code review suggestions
1 parent 91988f4 commit bfc86f6

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

crates/ide_assists/src/handlers/qualify_method_call.rs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212

1313
// Assist: qualify_method_call
1414
//
15-
// If the name is resolvable, provides fully qualified path for it.
15+
// Replaces the method call with a qualified function call.
1616
//
1717
// ```
1818
// struct Foo;
@@ -36,8 +36,10 @@ use crate::{
3636
// }
3737
// ```
3838
pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
39-
let call: ast::MethodCallExpr = ctx.find_node_at_offset()?;
40-
let fn_name = &call.name_ref()?;
39+
let name: ast::NameRef = ctx.find_node_at_offset()?;
40+
let call = name.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
41+
42+
let ident = name.ident_token()?;
4143

4244
let range = call.syntax().text_range();
4345
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
5254

5355
acc.add(
5456
AssistId("qualify_method_call", AssistKind::RefactorInline),
55-
format!("Qualify call `{}`", fn_name),
57+
format!("Qualify `{}` method call", ident.text()),
5658
range,
5759
|builder| {
5860
qualify_candidate.qualify(
@@ -68,7 +70,7 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext) -> Opt
6870
#[cfg(test)]
6971
mod tests {
7072
use super::*;
71-
use crate::tests::check_assist;
73+
use crate::tests::{check_assist, check_assist_not_applicable};
7274

7375
#[test]
7476
fn struct_method() {
@@ -480,6 +482,49 @@ fn main() {
480482
let test_struct = TestStruct {};
481483
TestTrait::test_method::<()>(&test_struct)
482484
}
485+
"#,
486+
);
487+
}
488+
489+
#[test]
490+
fn struct_method_over_stuct_instance() {
491+
check_assist_not_applicable(
492+
qualify_method_call,
493+
r#"
494+
struct Foo;
495+
impl Foo {
496+
fn foo(&self) {}
497+
}
498+
499+
fn main() {
500+
let foo = Foo {};
501+
f$0oo.foo()
502+
}
503+
"#,
504+
);
505+
}
506+
507+
#[test]
508+
fn trait_method_over_stuct_instance() {
509+
check_assist_not_applicable(
510+
qualify_method_call,
511+
r#"
512+
mod test_mod {
513+
pub trait TestTrait {
514+
fn test_method(&self);
515+
}
516+
pub struct TestStruct {}
517+
impl TestTrait for TestStruct {
518+
fn test_method(&self) {}
519+
}
520+
}
521+
522+
use test_mod::*;
523+
524+
fn main() {
525+
let test_struct = test_mod::TestStruct {};
526+
tes$0t_struct.test_method()
527+
}
483528
"#,
484529
);
485530
}

crates/ide_assists/src/handlers/qualify_path.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ impl QualifyCandidate<'_> {
171171
let trait_method_name = mcall_expr.name_ref()?;
172172
let trait_ = item_as_trait(db, item)?;
173173
let method = find_trait_method(db, trait_, &trait_method_name)?;
174-
Self::qualify_fn_call(db, mcall_expr, replacer, import, &method);
175-
Some(())
174+
Self::qualify_fn_call(db, mcall_expr, replacer, import, &method)
176175
}
177176
}
178177

0 commit comments

Comments
 (0)