6664: Show type of self param on hover r=jonas-schievink a=Veykril

Show the type of `self` when hovering the token in a `SelfParam`.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2020-11-28 23:38:18 +00:00 committed by GitHub
commit c8a2ff6ecc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -139,14 +139,17 @@ pub(crate) fn hover(
} }
} }
let node = token let node = token.ancestors().find(|n| {
.ancestors() ast::Expr::can_cast(n.kind())
.find(|n| ast::Expr::cast(n.clone()).is_some() || ast::Pat::cast(n.clone()).is_some())?; || ast::Pat::can_cast(n.kind())
|| ast::SelfParam::can_cast(n.kind())
})?;
let ty = match_ast! { let ty = match_ast! {
match node { match node {
ast::Expr(it) => sema.type_of_expr(&it)?, ast::Expr(it) => sema.type_of_expr(&it)?,
ast::Pat(it) => sema.type_of_pat(&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. // 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. // (e.g expanding a builtin macro). So we give up here.
ast::MacroCall(_it) => return None, 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>(T);
struct Foo {}
impl Foo {
fn bar(sel<|>f: Arc<Foo>) {}
}
"#,
expect![[r#"
*self: Arc<Foo>*
```rust
Arc<Foo>
```
"#]],
);
}
} }