rust-analyzer/crates/ide/src/goto_implementation.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

415 lines
8.6 KiB
Rust
Raw Normal View History

2023-12-03 19:20:38 +00:00
use hir::{AsAssocItem, DescendPreference, Impl, Semantics};
use ide_db::{
defs::{Definition, NameClass, NameRefClass},
helpers::pick_best_token,
RootDatabase,
};
use syntax::{ast, AstNode, SyntaxKind::*, T};
use crate::{FilePosition, NavigationTarget, RangeInfo, TryToNav};
2020-05-31 07:45:41 +00:00
// Feature: Go to Implementation
2020-05-30 23:54:54 +00:00
//
2022-05-27 13:47:31 +00:00
// Navigates to the impl blocks of types.
2020-05-30 23:54:54 +00:00
//
// |===
// | Editor | Shortcut
//
// | VS Code | kbd:[Ctrl+F12]
// |===
//
// image::https://user-images.githubusercontent.com/48062697/113065566-02f85480-91b1-11eb-9288-aaad8abd8841.gif[]
pub(crate) fn goto_implementation(
db: &RootDatabase,
FilePosition { file_id, offset }: FilePosition,
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
let sema = Semantics::new(db);
let source_file = sema.parse(file_id);
let syntax = source_file.syntax().clone();
let original_token = pick_best_token(syntax.token_at_offset(offset), |kind| match kind {
IDENT | T![self] | INT_NUMBER => 1,
_ => 0,
})?;
let range = original_token.text_range();
2023-08-02 09:52:55 +00:00
let navs =
2023-12-19 07:30:48 +00:00
sema.descend_into_macros_single(DescendPreference::SameText, original_token)
.parent()
.and_then(ast::NameLike::cast)
.and_then(|node| match &node {
2023-08-02 09:52:55 +00:00
ast::NameLike::Name(name) => {
NameClass::classify(&sema, name).and_then(|class| match class {
NameClass::Definition(it) | NameClass::ConstReference(it) => Some(it),
NameClass::PatFieldShorthand { .. } => None,
})
}
ast::NameLike::NameRef(name_ref) => NameRefClass::classify(&sema, name_ref)
.and_then(|class| match class {
NameRefClass::Definition(def) => Some(def),
NameRefClass::FieldShorthand { .. }
| NameRefClass::ExternCrateShorthand { .. } => None,
}),
ast::NameLike::Lifetime(_) => None,
})
2023-12-19 07:30:48 +00:00
.and_then(|def| {
2023-08-02 09:52:55 +00:00
let navs = match def {
Definition::Trait(trait_) => impls_for_trait(&sema, trait_),
Definition::Adt(adt) => impls_for_ty(&sema, adt.ty(sema.db)),
Definition::TypeAlias(alias) => impls_for_ty(&sema, alias.ty(sema.db)),
Definition::BuiltinType(builtin) => impls_for_ty(&sema, builtin.ty(sema.db)),
Definition::Function(f) => {
let assoc = f.as_assoc_item(sema.db)?;
let name = assoc.name(sema.db)?;
2024-02-10 00:48:41 +00:00
let trait_ = assoc.container_or_implemented_trait(sema.db)?;
2023-08-02 09:52:55 +00:00
impls_for_trait_item(&sema, trait_, name)
}
2023-08-02 09:52:55 +00:00
Definition::Const(c) => {
let assoc = c.as_assoc_item(sema.db)?;
let name = assoc.name(sema.db)?;
2024-02-10 00:48:41 +00:00
let trait_ = assoc.container_or_implemented_trait(sema.db)?;
2023-08-02 09:52:55 +00:00
impls_for_trait_item(&sema, trait_, name)
}
2023-08-02 09:52:55 +00:00
_ => return None,
};
Some(navs)
})
2023-12-19 07:30:48 +00:00
.unwrap_or_default();
Some(RangeInfo { range, info: navs })
2019-01-31 23:34:52 +00:00
}
2022-07-20 13:02:08 +00:00
fn impls_for_ty(sema: &Semantics<'_, RootDatabase>, ty: hir::Type) -> Vec<NavigationTarget> {
Impl::all_for_type(sema.db, ty)
.into_iter()
.filter_map(|imp| imp.try_to_nav(sema.db))
.flatten()
.collect()
}
2019-01-31 23:34:52 +00:00
2022-07-20 13:06:15 +00:00
fn impls_for_trait(
sema: &Semantics<'_, RootDatabase>,
trait_: hir::Trait,
) -> Vec<NavigationTarget> {
Impl::all_for_trait(sema.db, trait_)
.into_iter()
.filter_map(|imp| imp.try_to_nav(sema.db))
.flatten()
.collect()
}
fn impls_for_trait_item(
2022-07-20 13:02:08 +00:00
sema: &Semantics<'_, RootDatabase>,
trait_: hir::Trait,
fun_name: hir::Name,
) -> Vec<NavigationTarget> {
Impl::all_for_trait(sema.db, trait_)
.into_iter()
.filter_map(|imp| {
let item = imp.items(sema.db).iter().find_map(|itm| {
let itm_name = itm.name(sema.db)?;
2022-12-30 08:30:23 +00:00
(itm_name == fun_name).then_some(*itm)
})?;
item.try_to_nav(sema.db)
})
.flatten()
.collect()
}
#[cfg(test)]
mod tests {
2020-10-24 08:39:57 +00:00
use ide_db::base_db::FileRange;
2021-07-15 19:28:30 +00:00
use itertools::Itertools;
2020-10-02 15:34:31 +00:00
use crate::fixture;
2020-06-30 11:20:16 +00:00
fn check(ra_fixture: &str) {
2021-07-15 19:28:30 +00:00
let (analysis, position, expected) = fixture::annotations(ra_fixture);
2020-06-30 11:20:16 +00:00
let navs = analysis.goto_implementation(position).unwrap().unwrap().info;
2021-07-15 19:28:30 +00:00
let cmp = |frange: &FileRange| (frange.file_id, frange.range.start());
2020-06-30 11:20:16 +00:00
2021-07-15 19:28:30 +00:00
let actual = navs
2020-06-30 11:20:16 +00:00
.into_iter()
2020-07-17 10:42:48 +00:00
.map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
2021-07-15 19:28:30 +00:00
.sorted_by_key(cmp)
2020-06-30 11:20:16 +00:00
.collect::<Vec<_>>();
2021-07-15 19:28:30 +00:00
let expected =
expected.into_iter().map(|(range, _)| range).sorted_by_key(cmp).collect::<Vec<_>>();
2020-06-30 11:20:16 +00:00
assert_eq!(expected, actual);
}
#[test]
fn goto_implementation_works() {
2020-06-30 11:20:16 +00:00
check(
r#"
2021-01-06 20:15:48 +00:00
struct Foo$0;
2020-06-30 11:20:16 +00:00
impl Foo {}
//^^^
"#,
);
}
#[test]
fn goto_implementation_works_multiple_blocks() {
2020-06-30 11:20:16 +00:00
check(
r#"
2021-01-06 20:15:48 +00:00
struct Foo$0;
2020-06-30 11:20:16 +00:00
impl Foo {}
//^^^
impl Foo {}
//^^^
"#,
);
}
#[test]
fn goto_implementation_works_multiple_mods() {
2020-06-30 11:20:16 +00:00
check(
r#"
2021-01-06 20:15:48 +00:00
struct Foo$0;
2020-06-30 11:20:16 +00:00
mod a {
impl super::Foo {}
//^^^^^^^^^^
}
mod b {
impl super::Foo {}
//^^^^^^^^^^
}
"#,
);
}
#[test]
fn goto_implementation_works_multiple_files() {
2020-06-30 11:20:16 +00:00
check(
r#"
//- /lib.rs
2021-01-06 20:15:48 +00:00
struct Foo$0;
2020-06-30 11:20:16 +00:00
mod a;
mod b;
//- /a.rs
impl crate::Foo {}
//^^^^^^^^^^
//- /b.rs
impl crate::Foo {}
//^^^^^^^^^^
"#,
);
}
2019-01-31 23:34:52 +00:00
#[test]
fn goto_implementation_for_trait() {
2020-06-30 11:20:16 +00:00
check(
r#"
2021-01-06 20:15:48 +00:00
trait T$0 {}
2020-06-30 11:20:16 +00:00
struct Foo;
impl T for Foo {}
//^^^
"#,
2019-01-31 23:34:52 +00:00
);
}
#[test]
fn goto_implementation_for_trait_multiple_files() {
2020-06-30 11:20:16 +00:00
check(
r#"
//- /lib.rs
2021-01-06 20:15:48 +00:00
trait T$0 {};
2020-06-30 11:20:16 +00:00
struct Foo;
mod a;
mod b;
//- /a.rs
impl crate::T for crate::Foo {}
//^^^^^^^^^^
//- /b.rs
impl crate::T for crate::Foo {}
//^^^^^^^^^^
"#,
2019-01-31 23:34:52 +00:00
);
}
#[test]
fn goto_implementation_all_impls() {
2020-06-30 11:20:16 +00:00
check(
r#"
//- /lib.rs
trait T {}
2021-01-06 20:15:48 +00:00
struct Foo$0;
2020-06-30 11:20:16 +00:00
impl Foo {}
//^^^
impl T for Foo {}
//^^^
impl T for &Foo {}
//^^^^
"#,
);
}
2020-01-12 11:24:34 +00:00
#[test]
fn goto_implementation_to_builtin_derive() {
2020-06-30 11:20:16 +00:00
check(
r#"
2021-06-19 09:03:59 +00:00
//- minicore: copy, derive
2020-06-30 11:20:16 +00:00
#[derive(Copy)]
2023-11-24 15:38:48 +00:00
//^^^^
2021-01-06 20:15:48 +00:00
struct Foo$0;
"#,
);
}
#[test]
fn goto_implementation_type_alias() {
check(
r#"
struct Foo;
type Bar$0 = Foo;
impl Foo {}
//^^^
impl Bar {}
//^^^
"#,
);
}
#[test]
fn goto_implementation_adt_generic() {
check(
r#"
struct Foo$0<T>;
impl<T> Foo<T> {}
//^^^^^^
impl Foo<str> {}
//^^^^^^^^
"#,
);
}
#[test]
fn goto_implementation_builtin() {
check(
r#"
//- /lib.rs crate:main deps:core
fn foo(_: bool$0) {{}}
//- /libcore.rs crate:core
2023-03-14 18:36:28 +00:00
#![rustc_coherence_is_core]
#[lang = "bool"]
impl bool {}
//^^^^
"#,
);
}
#[test]
fn goto_implementation_trait_functions() {
check(
r#"
trait Tr {
fn f$0();
}
struct S;
impl Tr for S {
fn f() {
//^
println!("Hello, world!");
}
}
"#,
);
}
#[test]
fn goto_implementation_trait_assoc_const() {
check(
r#"
trait Tr {
const C$0: usize;
}
struct S;
impl Tr for S {
const C: usize = 4;
//^
}
"#,
);
}
#[test]
fn goto_adt_implementation_inside_block() {
check(
r#"
//- minicore: copy, derive
trait Bar {}
fn test() {
#[derive(Copy)]
//^^^^^^^^^^^^^^^
struct Foo$0;
impl Foo {}
//^^^
trait Baz {}
impl Bar for Foo {}
//^^^
impl Baz for Foo {}
//^^^
}
"#,
);
}
#[test]
fn goto_trait_implementation_inside_block() {
check(
r#"
struct Bar;
fn test() {
trait Foo$0 {}
struct Baz;
impl Foo for Bar {}
//^^^
impl Foo for Baz {}
//^^^
}
"#,
);
check(
r#"
struct Bar;
fn test() {
trait Foo {
fn foo$0() {}
}
struct Baz;
impl Foo for Bar {
fn foo() {}
//^^^
}
impl Foo for Baz {
fn foo() {}
//^^^
}
}
2020-06-30 11:20:16 +00:00
"#,
2020-01-12 11:24:34 +00:00
);
}
}