Skip to content

Commit 9ea8d58

Browse files
committed
fix test in qualify_method: stay in trait path
1 parent 9e6bff7 commit 9ea8d58

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

crates/ide-assists/src/handlers/qualify_method_call.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
use hir::{ItemInNs, ModuleDef};
2-
use ide_db::{
3-
assists::{AssistId, AssistKind},
4-
imports::import_assets::item_for_path_search,
5-
};
1+
use hir::{db::HirDatabase, AsAssocItem, AssocItem, AssocItemContainer, ItemInNs, ModuleDef};
2+
use ide_db::assists::{AssistId, AssistKind};
63
use syntax::{ast, AstNode};
74

85
use crate::{
@@ -67,6 +64,33 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext) -> Opt
6764
Some(())
6865
}
6966

67+
fn item_for_path_search(db: &dyn HirDatabase, item: ItemInNs) -> Option<ItemInNs> {
68+
Some(match item {
69+
ItemInNs::Types(_) | ItemInNs::Values(_) => match item_as_assoc(db, item) {
70+
Some(assoc_item) => match assoc_item.container(db) {
71+
AssocItemContainer::Trait(trait_) => ItemInNs::from(ModuleDef::from(trait_)),
72+
AssocItemContainer::Impl(impl_) => {
73+
let impled_trait = if matches!(assoc_item, AssocItem::Function(..)) {
74+
impl_.trait_(db)
75+
} else {
76+
None
77+
};
78+
match impled_trait {
79+
None => ItemInNs::from(ModuleDef::from(impl_.self_ty(db).as_adt()?)),
80+
Some(t) => ItemInNs::from(ModuleDef::from(t)),
81+
}
82+
}
83+
},
84+
None => item,
85+
},
86+
ItemInNs::Macros(_) => item,
87+
})
88+
}
89+
90+
fn item_as_assoc(db: &dyn HirDatabase, item: ItemInNs) -> Option<AssocItem> {
91+
item.as_module_def().and_then(|module_def| module_def.as_assoc_item(db))
92+
}
93+
7094
#[cfg(test)]
7195
mod tests {
7296
use super::*;
@@ -281,7 +305,7 @@ use test_mod::*;
281305
282306
fn main() {
283307
let test_struct = test_mod::TestStruct {};
284-
TestStruct::test_method(&test_struct)
308+
TestTrait::test_method(&test_struct)
285309
}
286310
"#,
287311
);
@@ -324,7 +348,7 @@ use test_mod::*;
324348
325349
fn main() {
326350
let test_struct = test_mod::TestStruct {};
327-
TestStruct::test_method(&test_struct, 12, 32u)
351+
TestTrait::test_method(&test_struct, 12, 32u)
328352
}
329353
"#,
330354
);
@@ -367,7 +391,7 @@ use test_mod::*;
367391
368392
fn main() {
369393
let test_struct = test_mod::TestStruct {};
370-
TestStruct::test_method(test_struct, 12, 32u)
394+
TestTrait::test_method(test_struct, 12, 32u)
371395
}
372396
"#,
373397
);
@@ -410,7 +434,7 @@ use test_mod::*;
410434
411435
fn main() {
412436
let test_struct = test_mod::TestStruct {};
413-
TestStruct::test_method(&mut test_struct, 12, 32u)
437+
TestTrait::test_method(&mut test_struct, 12, 32u)
414438
}
415439
"#,
416440
);
@@ -480,7 +504,7 @@ use test_mod::*;
480504
481505
fn main() {
482506
let test_struct = TestStruct {};
483-
TestStruct::test_method::<()>(&test_struct)
507+
TestTrait::test_method::<()>(&test_struct)
484508
}
485509
"#,
486510
);

crates/ide-db/src/imports/import_assets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ fn import_for_item(
401401
})
402402
}
403403

404-
pub fn item_for_path_search(db: &RootDatabase, item: ItemInNs) -> Option<ItemInNs> {
404+
fn item_for_path_search(db: &RootDatabase, item: ItemInNs) -> Option<ItemInNs> {
405405
Some(match item {
406406
ItemInNs::Types(_) | ItemInNs::Values(_) => match item_as_assoc(db, item) {
407407
Some(assoc_item) => match assoc_item.container(db) {

0 commit comments

Comments
 (0)