Skip to content

Commit f2d2ce0

Browse files
committed
Also search for pattern nodes in ranged hover
1 parent 486603d commit f2d2ce0

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

crates/ide/src/hover.rs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ use ide_db::{
1212
use itertools::Itertools;
1313
use stdx::format_to;
1414
use syntax::{
15-
algo::{self, find_node_at_range},
16-
ast,
17-
display::fn_as_proc_macro_label,
18-
match_ast, AstNode, AstToken, Direction,
19-
SyntaxKind::*,
20-
SyntaxToken, T,
15+
algo, ast, display::fn_as_proc_macro_label, match_ast, AstNode, AstToken, Direction,
16+
SyntaxKind::*, SyntaxToken, T,
2117
};
2218

2319
use crate::{
@@ -88,7 +84,15 @@ pub(crate) fn hover(
8884
let offset = if range.is_empty() {
8985
range.start()
9086
} else {
91-
let expr = find_node_at_range::<ast::Expr>(&file, range).map(Either::Left)?;
87+
let expr = file.covering_element(range).ancestors().find_map(|it| {
88+
match_ast! {
89+
match it {
90+
ast::Expr(expr) => Some(Either::Left(expr)),
91+
ast::Pat(pat) => Some(Either::Right(pat)),
92+
_ => None,
93+
}
94+
}
95+
})?;
9296
return hover_type_info(&sema, config, expr).map(|it| RangeInfo::new(range, it));
9397
};
9498

@@ -4097,15 +4101,42 @@ fn f() { let expr$0 = $0[1, 2, 3, 4] }
40974101
}
40984102

40994103
#[test]
4100-
fn hover_range_shows_coercions_if_applicable() {
4104+
fn hover_range_for_pat() {
4105+
check_hover_range(
4106+
r#"
4107+
fn foo() {
4108+
let $0x$0 = 0;
4109+
}
4110+
"#,
4111+
expect![[r#"
4112+
```rust
4113+
i32
4114+
```"#]],
4115+
);
4116+
4117+
check_hover_range(
4118+
r#"
4119+
fn foo() {
4120+
let $0x$0 = "";
4121+
}
4122+
"#,
4123+
expect![[r#"
4124+
```rust
4125+
&str
4126+
```"#]],
4127+
);
4128+
}
4129+
4130+
#[test]
4131+
fn hover_range_shows_coercions_if_applicable_expr() {
41014132
check_hover_range(
41024133
r#"
41034134
fn foo() {
41044135
let x: &u32 = $0&&&&&0$0;
41054136
}
41064137
"#,
41074138
expect![[r#"
4108-
```
4139+
```text
41094140
Type: &&&&&u32
41104141
Coerced to: &u32
41114142
```
@@ -4118,7 +4149,7 @@ fn foo() {
41184149
}
41194150
"#,
41204151
expect![[r#"
4121-
```
4152+
```text
41224153
Type: &u32
41234154
Coerced to: *const u32
41244155
```

0 commit comments

Comments
 (0)