From 8a57c736404abac81a6de20b0e90c19021e040b9 Mon Sep 17 00:00:00 2001 From: Jade Date: Tue, 1 Jun 2021 16:16:59 -0700 Subject: feat: goto definition on an impl fn goes to that fn in the trait e.g. if you have a trait T and `impl T for S` for some struct, if you goto definition on some function name inside the impl, it will go to the definition of that function inside the `trait T` block, rather than the current behaviour of not going anywhere at all. --- crates/ide/src/goto_definition.rs | 51 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) (limited to 'crates/ide/src/goto_definition.rs') diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs index b0bfd646e..c236039f4 100644 --- a/crates/ide/src/goto_definition.rs +++ b/crates/ide/src/goto_definition.rs @@ -1,10 +1,10 @@ use std::convert::TryInto; use either::Either; -use hir::{InFile, Semantics}; +use hir::{AsAssocItem, InFile, ModuleDef, Semantics}; use ide_db::{ base_db::{AnchoredPath, FileId, FileLoader}, - defs::{NameClass, NameRefClass}, + defs::{Definition, NameClass, NameRefClass}, RootDatabase, }; use syntax::{ @@ -57,7 +57,8 @@ pub(crate) fn goto_definition( }, ast::Name(name) => { let def = NameClass::classify(&sema, &name)?.referenced_or_defined(sema.db); - def.try_to_nav(sema.db) + try_find_trait_fn_definition(&sema.db, &def) + .or_else(|| def.try_to_nav(sema.db)) }, ast::Lifetime(lt) => if let Some(name_class) = NameClass::classify_lifetime(&sema, <) { let def = name_class.referenced_or_defined(sema.db); @@ -99,6 +100,32 @@ fn try_lookup_include_path( }) } +/// finds the trait definition of an impl'd function +/// e.g. +/// ```rust +/// trait A { fn a(); } +/// struct S; +/// impl A for S { fn a(); } // <-- on this function, will get the location of a() in the trait +/// ``` +fn try_find_trait_fn_definition(db: &RootDatabase, def: &Definition) -> Option { + match def { + Definition::ModuleDef(ModuleDef::Function(f)) => { + let name = def.name(db)?; + let assoc = f.as_assoc_item(db)?; + let imp = match assoc.container(db) { + hir::AssocItemContainer::Impl(imp) => imp, + _ => return None, + }; + let trait_ = imp.trait_(db)?; + trait_ + .items(db) + .iter() + .find_map(|itm| (itm.name(db)? == name).then(|| itm.try_to_nav(db)).flatten()) + } + _ => None, + } +} + fn pick_best(tokens: TokenAtOffset) -> Option { return tokens.max_by_key(priority); fn priority(n: &SyntaxToken) -> usize { @@ -1259,6 +1286,24 @@ fn main() { //- /foo.txt // empty //^ file +"#, + ); + } + + #[test] + fn goto_def_of_trait_impl_fn() { + check( + r#" +trait Twait { + fn a(); + // ^ +} + +struct Stwuct; + +impl Twait for Stwuct { + fn a$0(); +} "#, ); } -- cgit v1.2.3