From ee8afff71486a59334a812b69b8ebe0c4c2afba9 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 28 Nov 2020 22:46:25 +0100 Subject: Show type of self param on hover --- crates/ide/src/hover.rs | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 832192881..94ad800a0 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -139,14 +139,17 @@ pub(crate) fn hover( } } - let node = token - .ancestors() - .find(|n| ast::Expr::cast(n.clone()).is_some() || ast::Pat::cast(n.clone()).is_some())?; + 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 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, @@ -3282,4 +3285,41 @@ fn main() { "#]], ); } + + #[test] + fn hover_self_param_shows_type() { + check( + r#" +struct Foo {} +impl Foo { + fn bar(&sel<|>f) {} +} +"#, + expect![[r#" + *&self* + ```rust + &Foo + ``` + "#]], + ); + } + + #[test] + fn hover_self_param_shows_type_for_arbitrary_self_type() { + check( + r#" +struct Arc(T); +struct Foo {} +impl Foo { + fn bar(sel<|>f: Arc) {} +} +"#, + expect![[r#" + *self: Arc* + ```rust + Arc + ``` + "#]], + ); + } } -- cgit v1.2.3