1 use hir::{AsAssocItem, Impl, Semantics};
3 defs::{Definition, NameClass, NameRefClass},
6 use syntax::{ast, AstNode};
8 use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo};
10 // Feature: Go to Implementation
12 // Navigates to the impl block of structs, enums or traits. Also implemented as a code lens.
15 // | Editor | Shortcut
17 // | VS Code | kbd:[Ctrl+F12]
20 // image::https://user-images.githubusercontent.com/48062697/113065566-02f85480-91b1-11eb-9288-aaad8abd8841.gif[]
21 pub(crate) fn goto_implementation(
23 position: FilePosition,
24 ) -> Option<RangeInfo<Vec<NavigationTarget>>> {
25 let sema = Semantics::new(db);
26 let source_file = sema.parse(position.file_id);
27 let syntax = source_file.syntax().clone();
29 let node = sema.find_node_at_offset_with_descend(&syntax, position.offset)?;
30 let def = match &node {
31 ast::NameLike::Name(name) => {
32 NameClass::classify(&sema, name).map(|class| class.referenced_or_defined())
34 ast::NameLike::NameRef(name_ref) => {
35 NameRefClass::classify(&sema, name_ref).map(|class| class.referenced())
37 ast::NameLike::Lifetime(_) => None,
41 Definition::ModuleDef(def) => def,
44 let navs = match def {
45 hir::ModuleDef::Trait(trait_) => impls_for_trait(&sema, trait_),
46 hir::ModuleDef::Adt(adt) => impls_for_ty(&sema, adt.ty(sema.db)),
47 hir::ModuleDef::TypeAlias(alias) => impls_for_ty(&sema, alias.ty(sema.db)),
48 hir::ModuleDef::BuiltinType(builtin) => {
49 let module = sema.to_module_def(position.file_id)?;
50 impls_for_ty(&sema, builtin.ty(sema.db, module))
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_or_trait_impl(sema.db)?;
56 impls_for_trait_item(&sema, trait_, name)
58 hir::ModuleDef::Const(c) => {
59 let assoc = c.as_assoc_item(sema.db)?;
60 let name = assoc.name(sema.db)?;
61 let trait_ = assoc.containing_trait_or_trait_impl(sema.db)?;
62 impls_for_trait_item(&sema, trait_, name)
66 Some(RangeInfo { range: node.syntax().text_range(), info: navs })
69 fn impls_for_ty(sema: &Semantics<RootDatabase>, ty: hir::Type) -> Vec<NavigationTarget> {
70 Impl::all_for_type(sema.db, ty).into_iter().filter_map(|imp| imp.try_to_nav(sema.db)).collect()
73 fn impls_for_trait(sema: &Semantics<RootDatabase>, trait_: hir::Trait) -> Vec<NavigationTarget> {
74 Impl::all_for_trait(sema.db, trait_)
76 .filter_map(|imp| imp.try_to_nav(sema.db))
80 fn impls_for_trait_item(
81 sema: &Semantics<RootDatabase>,
84 ) -> Vec<NavigationTarget> {
85 Impl::all_for_trait(sema.db, trait_)
88 let item = imp.items(sema.db).iter().find_map(|itm| {
89 let itm_name = itm.name(sema.db)?;
90 (itm_name == fun_name).then(|| *itm)
92 item.try_to_nav(sema.db)
99 use ide_db::base_db::FileRange;
103 fn check(ra_fixture: &str) {
104 let (analysis, position, annotations) = fixture::annotations(ra_fixture);
106 let navs = analysis.goto_implementation(position).unwrap().unwrap().info;
108 let key = |frange: &FileRange| (frange.file_id, frange.range.start());
110 let mut expected = annotations
112 .map(|(range, data)| {
113 assert!(data.is_empty());
116 .collect::<Vec<_>>();
117 expected.sort_by_key(key);
119 let mut actual = navs
121 .map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
122 .collect::<Vec<_>>();
123 actual.sort_by_key(key);
125 assert_eq!(expected, actual);
129 fn goto_implementation_works() {
140 fn goto_implementation_works_multiple_blocks() {
153 fn goto_implementation_works_multiple_mods() {
170 fn goto_implementation_works_multiple_files() {
188 fn goto_implementation_for_trait() {
200 fn goto_implementation_for_trait_multiple_files() {
209 impl crate::T for crate::Foo {}
212 impl crate::T for crate::Foo {}
219 fn goto_implementation_all_impls() {
236 fn goto_implementation_to_builtin_derive() {
239 //- minicore: copy, derive
248 fn goto_implementation_type_alias() {
264 fn goto_implementation_adt_generic() {
278 fn goto_implementation_builtin() {
281 //- /lib.rs crate:main deps:core
282 fn foo(_: bool$0) {{}}
283 //- /libcore.rs crate:core
292 fn goto_implementation_trait_functions() {
304 println!("Hello, world!");
312 fn goto_implementation_trait_assoc_const() {