Skip to content

Commit 1e6fbbc

Browse files
bors[bot]lf-
andauthored
Merge #8988
8988: feat: go to implementation on trait functions r=matklad a=lf- Fix #8537. GIF: ![output](https://user-images.githubusercontent.com/6652840/119501981-45a45c00-bd1e-11eb-8336-9145f2888643.gif) Co-authored-by: Jade <[email protected]>
2 parents ddc2fb3 + 3e4dfaf commit 1e6fbbc

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

crates/ide/src/goto_implementation.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hir::{Impl, Semantics};
1+
use hir::{AsAssocItem, Impl, Semantics};
22
use ide_db::{
33
defs::{Definition, NameClass, NameRefClass},
44
RootDatabase,
@@ -36,6 +36,7 @@ pub(crate) fn goto_implementation(
3636
}
3737
ast::NameLike::Lifetime(_) => None,
3838
}?;
39+
3940
let def = match def {
4041
Definition::ModuleDef(def) => def,
4142
_ => return None,
@@ -48,6 +49,12 @@ pub(crate) fn goto_implementation(
4849
let module = sema.to_module_def(position.file_id)?;
4950
impls_for_ty(&sema, builtin.ty(sema.db, module))
5051
}
52+
hir::ModuleDef::Function(f) => {
53+
let assoc = f.as_assoc_item(sema.db)?;
54+
let name = assoc.name(sema.db)?;
55+
let trait_ = assoc.containing_trait(sema.db)?;
56+
impls_for_trait_fn(&sema, trait_, name)
57+
}
5158
_ => return None,
5259
};
5360
Some(RangeInfo { range: node.syntax().text_range(), info: navs })
@@ -64,6 +71,23 @@ fn impls_for_trait(sema: &Semantics<RootDatabase>, trait_: hir::Trait) -> Vec<Na
6471
.collect()
6572
}
6673

74+
fn impls_for_trait_fn(
75+
sema: &Semantics<RootDatabase>,
76+
trait_: hir::Trait,
77+
fun_name: hir::Name,
78+
) -> Vec<NavigationTarget> {
79+
Impl::all_for_trait(sema.db, trait_)
80+
.into_iter()
81+
.filter_map(|imp| {
82+
let item = imp.items(sema.db).iter().find_map(|itm| {
83+
let itm_name = itm.name(sema.db)?;
84+
(itm_name == fun_name).then(|| itm.clone())
85+
})?;
86+
item.try_to_nav(sema.db)
87+
})
88+
.collect()
89+
}
90+
6791
#[cfg(test)]
6892
mod tests {
6993
use ide_db::base_db::FileRange;
@@ -259,6 +283,26 @@ fn foo(_: bool$0) {{}}
259283
#[lang = "bool"]
260284
impl bool {}
261285
//^^^^
286+
"#,
287+
);
288+
}
289+
290+
#[test]
291+
fn goto_implementation_trait_functions() {
292+
check(
293+
r#"
294+
trait Tr {
295+
fn f$0();
296+
}
297+
298+
struct S;
299+
300+
impl Tr for S {
301+
fn f() {
302+
//^
303+
println!("Hello, world!");
304+
}
305+
}
262306
"#,
263307
);
264308
}

0 commit comments

Comments
 (0)