From 4a0bb3d7c53c2a914649087bf206d52ed5768576 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Thu, 7 Mar 2019 10:32:39 +0200 Subject: Add support for goto definition and hover on Self This fixes #943 --- crates/ra_ide_api/src/goto_definition.rs | 96 +++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide_api/src/goto_definition.rs') diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index dd5f9f31c..d4e10b69c 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -121,8 +121,12 @@ pub(crate) fn reference_definition( Some(Resolution::GenericParam(..)) => { // TODO: go to the generic param def } - Some(Resolution::SelfType(_impl_block)) => { - // TODO: go to the implemented type + Some(Resolution::SelfType(impl_block)) => { + let ty = impl_block.target_ty(db); + + if let hir::Ty::Adt { def_id, .. } = ty { + return Exact(NavigationTarget::from_adt_def(db, def_id)); + } } None => { // If we failed to resolve then check associated items @@ -337,6 +341,94 @@ mod tests { "spam NAMED_FIELD_DEF FileId(1) [17; 26) [17; 21)", ); } + #[test] + fn goto_definition_on_self() { + check_goto( + " + //- /lib.rs + struct Foo; + impl Foo { + pub fn new() -> Self { + Self<|> {} + } + } + ", + "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + ); + + check_goto( + " + //- /lib.rs + struct Foo; + impl Foo { + pub fn new() -> Self<|> { + Self {} + } + } + ", + "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + ); + + check_goto( + " + //- /lib.rs + enum Foo { A } + impl Foo { + pub fn new() -> Self<|> { + Foo::A + } + } + ", + "Foo ENUM_DEF FileId(1) [0; 14) [5; 8)", + ); + + check_goto( + " + //- /lib.rs + enum Foo { A } + impl Foo { + pub fn thing(a: &Self<|>) { + } + } + ", + "Foo ENUM_DEF FileId(1) [0; 14) [5; 8)", + ); + } + + #[test] + fn goto_definition_on_self_in_trait_impl() { + check_goto( + " + //- /lib.rs + struct Foo; + trait Make { + fn new() -> Self; + } + impl Make for Foo { + fn new() -> Self { + Self<|> {} + } + } + ", + "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + ); + + check_goto( + " + //- /lib.rs + struct Foo; + trait Make { + fn new() -> Self; + } + impl Make for Foo { + fn new() -> Self<|> { + Self{} + } + } + ", + "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + ); + } #[test] fn goto_definition_works_when_used_on_definition_name_itself() { -- cgit v1.2.3