From cb863390f23bc2eac6561d55def9bd3ba54605fc Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 15 Jan 2021 18:57:32 +0100 Subject: Handle self/super/crate in PathSegment as NameRef --- crates/ide/src/hover.rs | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) (limited to 'crates/ide/src/hover.rs') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 317b6f011..6022bd275 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -98,6 +98,7 @@ pub(crate) fn hover( ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)), ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime) .map_or_else(|| NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(sema.db)), |d| d.defined(sema.db)), + ast::SelfParam(self_param) => NameClass::classify_self_param(&sema, &self_param).and_then(|d| d.defined(sema.db)), _ => None, } }; @@ -134,17 +135,14 @@ pub(crate) fn hover( return None; } - let node = token.ancestors().find(|n| { - ast::Expr::can_cast(n.kind()) - || ast::Pat::can_cast(n.kind()) - || ast::SelfParam::can_cast(n.kind()) - })?; + let node = token + .ancestors() + .find(|n| ast::Expr::can_cast(n.kind()) || ast::Pat::can_cast(n.kind()))?; let ty = match_ast! { match node { ast::Expr(it) => sema.type_of_expr(&it)?, ast::Pat(it) => sema.type_of_pat(&it)?, - ast::SelfParam(self_param) => sema.type_of_self(&self_param)?, // If this node is a MACRO_CALL, it means that `descend_into_macros` failed to resolve. // (e.g expanding a builtin macro). So we give up here. ast::MacroCall(_it) => return None, @@ -386,7 +384,7 @@ fn pick_best(tokens: TokenAtOffset) -> Option { return tokens.max_by_key(priority); fn priority(n: &SyntaxToken) -> usize { match n.kind() { - IDENT | INT_NUMBER | LIFETIME_IDENT => 3, + IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] => 3, T!['('] | T![')'] => 2, kind if kind.is_trivia() => 0, _ => 1, @@ -3129,6 +3127,39 @@ fn foo(t: T$0){} ); } + #[test] + fn test_hover_self_has_go_to_type() { + check_actions( + r#" +struct Foo; +impl Foo { + fn foo(&self$0) {} +} +"#, + expect![[r#" + [ + GoToType( + [ + HoverGotoTypeData { + mod_path: "test::Foo", + nav: NavigationTarget { + file_id: FileId( + 0, + ), + full_range: 0..11, + focus_range: 7..10, + name: "Foo", + kind: Struct, + description: "struct Foo", + }, + }, + ], + ), + ] + "#]], + ); + } + #[test] fn hover_displays_normalized_crate_names() { check( @@ -3193,6 +3224,7 @@ impl Foo { "#, expect![[r#" *&self* + ```rust &Foo ``` @@ -3212,6 +3244,7 @@ impl Foo { "#, expect![[r#" *self: Arc* + ```rust Arc ``` -- cgit v1.2.3