1 use hir::{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(sema.db))
34 ast::NameLike::NameRef(name_ref) => {
35 NameRefClass::classify(&sema, name_ref).map(|class| class.referenced(sema.db))
37 ast::NameLike::Lifetime(_) => None,
40 Definition::ModuleDef(def) => def,
43 let navs = match def {
44 hir::ModuleDef::Trait(trait_) => impls_for_trait(&sema, trait_),
45 hir::ModuleDef::Adt(adt) => impls_for_ty(&sema, adt.ty(sema.db)),
46 hir::ModuleDef::TypeAlias(alias) => impls_for_ty(&sema, alias.ty(sema.db)),
47 hir::ModuleDef::BuiltinType(builtin) => {
48 let module = sema.to_module_def(position.file_id)?;
49 impls_for_ty(&sema, builtin.ty(sema.db, module))
53 Some(RangeInfo { range: node.syntax().text_range(), info: navs })
56 fn impls_for_ty(sema: &Semantics<RootDatabase>, ty: hir::Type) -> Vec<NavigationTarget> {
57 Impl::all_for_type(sema.db, ty).into_iter().filter_map(|imp| imp.try_to_nav(sema.db)).collect()
60 fn impls_for_trait(sema: &Semantics<RootDatabase>, trait_: hir::Trait) -> Vec<NavigationTarget> {
61 Impl::all_for_trait(sema.db, trait_)
63 .filter_map(|imp| imp.try_to_nav(sema.db))
69 use ide_db::base_db::FileRange;
73 fn check(ra_fixture: &str) {
74 let (analysis, position, annotations) = fixture::annotations(ra_fixture);
76 let navs = analysis.goto_implementation(position).unwrap().unwrap().info;
78 let key = |frange: &FileRange| (frange.file_id, frange.range.start());
80 let mut expected = annotations
82 .map(|(range, data)| {
83 assert!(data.is_empty());
87 expected.sort_by_key(key);
91 .map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
93 actual.sort_by_key(key);
95 assert_eq!(expected, actual);
99 fn goto_implementation_works() {
110 fn goto_implementation_works_multiple_blocks() {
123 fn goto_implementation_works_multiple_mods() {
140 fn goto_implementation_works_multiple_files() {
158 fn goto_implementation_for_trait() {
170 fn goto_implementation_for_trait_multiple_files() {
179 impl crate::T for crate::Foo {}
182 impl crate::T for crate::Foo {}
189 fn goto_implementation_all_impls() {
206 fn goto_implementation_to_builtin_derive() {
216 #[rustc_builtin_macro]
223 fn goto_implementation_type_alias() {
239 fn goto_implementation_adt_generic() {
253 fn goto_implementation_builtin() {
256 //- /lib.rs crate:main deps:core
257 fn foo(_: bool$0) {{}}
258 //- /libcore.rs crate:core