Skip to content

Commit 4685b97

Browse files
committed
Remove hover fallback in favor of ranged hover
1 parent a542bd4 commit 4685b97

File tree

2 files changed

+27
-131
lines changed

2 files changed

+27
-131
lines changed

crates/ide/src/hover.rs

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ide_db::{
1515
FxIndexSet, RootDatabase,
1616
};
1717
use itertools::Itertools;
18-
use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxNode, SyntaxToken, T};
18+
use syntax::{ast, AstNode, SyntaxKind::*, SyntaxNode, T};
1919

2020
use crate::{
2121
doc_links::token_as_doc_comment,
@@ -203,14 +203,10 @@ fn hover_simple(
203203
})
204204
});
205205

206-
result
207-
.map(|mut res: HoverResult| {
208-
res.actions = dedupe_or_merge_hover_actions(res.actions);
209-
RangeInfo::new(original_token.text_range(), res)
210-
})
211-
// fallback to type hover if there aren't any other suggestions
212-
// this finds its own range instead of using the closest token's range
213-
.or_else(|| descended().find_map(|token| hover_type_fallback(sema, config, token, token)))
206+
result.map(|mut res: HoverResult| {
207+
res.actions = dedupe_or_merge_hover_actions(res.actions);
208+
RangeInfo::new(original_token.text_range(), res)
209+
})
214210
}
215211

216212
fn hover_ranged(
@@ -220,8 +216,11 @@ fn hover_ranged(
220216
config: &HoverConfig,
221217
) -> Option<RangeInfo<HoverResult>> {
222218
// FIXME: make this work in attributes
223-
let expr_or_pat =
224-
file.covering_element(range).ancestors().find_map(Either::<ast::Expr, ast::Pat>::cast)?;
219+
let expr_or_pat = file
220+
.covering_element(range)
221+
.ancestors()
222+
.take_while(|it| ast::MacroCall::can_cast(it.kind()) || !ast::Item::can_cast(it.kind()))
223+
.find_map(Either::<ast::Expr, ast::Pat>::cast)?;
225224
let res = match &expr_or_pat {
226225
Either::Left(ast::Expr::TryExpr(try_expr)) => render::try_expr(sema, config, try_expr),
227226
Either::Left(ast::Expr::PrefixExpr(prefix_expr))
@@ -268,39 +267,6 @@ pub(crate) fn hover_for_definition(
268267
})
269268
}
270269

271-
fn hover_type_fallback(
272-
sema: &Semantics<'_, RootDatabase>,
273-
config: &HoverConfig,
274-
token: &SyntaxToken,
275-
original_token: &SyntaxToken,
276-
) -> Option<RangeInfo<HoverResult>> {
277-
let node =
278-
token.parent_ancestors().take_while(|it| !ast::Item::can_cast(it.kind())).find(|n| {
279-
ast::Expr::can_cast(n.kind())
280-
|| ast::Pat::can_cast(n.kind())
281-
|| ast::Type::can_cast(n.kind())
282-
})?;
283-
284-
let expr_or_pat = match_ast! {
285-
match node {
286-
ast::Expr(it) => Either::Left(it),
287-
ast::Pat(it) => Either::Right(it),
288-
// If this node is a MACRO_CALL, it means that `descend_into_macros_many` failed to resolve.
289-
// (e.g expanding a builtin macro). So we give up here.
290-
ast::MacroCall(_it) => return None,
291-
_ => return None,
292-
}
293-
};
294-
295-
let res = render::type_info_of(sema, config, &expr_or_pat)?;
296-
297-
let range = sema
298-
.original_range_opt(&node)
299-
.map(|frange| frange.range)
300-
.unwrap_or_else(|| original_token.text_range());
301-
Some(RangeInfo::new(range, res))
302-
}
303-
304270
fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
305271
fn to_action(nav_target: NavigationTarget) -> HoverAction {
306272
HoverAction::Implementation(FilePosition {

crates/ide/src/hover/tests.rs

Lines changed: 17 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -213,38 +213,21 @@ m!(ab$0c);
213213
);
214214
}
215215

216-
#[test]
217-
fn hover_shows_type_of_an_expression() {
218-
check(
219-
r#"
220-
pub fn foo() -> u32 { 1 }
221-
222-
fn main() {
223-
let foo_test = foo()$0;
224-
}
225-
"#,
226-
expect![[r#"
227-
*foo()*
228-
```rust
229-
u32
230-
```
231-
"#]],
232-
);
233-
}
234-
235216
#[test]
236217
fn hover_remove_markdown_if_configured() {
237218
check_hover_no_markdown(
238219
r#"
239220
pub fn foo() -> u32 { 1 }
240221
241222
fn main() {
242-
let foo_test = foo()$0;
223+
let foo_test = foo$0();
243224
}
244225
"#,
245226
expect![[r#"
246-
*foo()*
247-
u32
227+
*foo*
228+
test
229+
230+
pub fn foo() -> u32
248231
"#]],
249232
);
250233
}
@@ -304,33 +287,6 @@ fn main() { let foo_test = fo$0o(); }
304287
"#]],
305288
);
306289

307-
// Multiple candidates but results are ambiguous.
308-
check(
309-
r#"
310-
//- /a.rs
311-
pub fn foo() -> u32 { 1 }
312-
313-
//- /b.rs
314-
pub fn foo() -> &str { "" }
315-
316-
//- /c.rs
317-
pub fn foo(a: u32, b: u32) {}
318-
319-
//- /main.rs
320-
mod a;
321-
mod b;
322-
mod c;
323-
324-
fn main() { let foo_test = fo$0o(); }
325-
"#,
326-
expect![[r#"
327-
*foo*
328-
```rust
329-
{unknown}
330-
```
331-
"#]],
332-
);
333-
334290
// Use literal `crate` in path
335291
check(
336292
r#"
@@ -1194,33 +1150,19 @@ fn test_hover_through_func_in_macro_recursive() {
11941150
macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } }
11951151
macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } }
11961152
fn bar() -> u32 { 0 }
1197-
fn foo() { let a = id!([0u32, bar($0)] ); }
1153+
fn foo() { let a = id!([0u32, bar$0()] ); }
11981154
"#,
11991155
expect![[r#"
1200-
*bar()*
1201-
```rust
1202-
u32
1203-
```
1204-
"#]],
1205-
);
1206-
}
1156+
*bar*
12071157
1208-
#[test]
1209-
fn test_hover_through_literal_string_in_macro() {
1210-
check(
1211-
r#"
1212-
macro_rules! arr { ($($tt:tt)*) => { [$($tt)*] } }
1213-
fn foo() {
1214-
let mastered_for_itunes = "";
1215-
let _ = arr!("Tr$0acks", &mastered_for_itunes);
1216-
}
1217-
"#,
1218-
expect![[r#"
1219-
*"Tracks"*
1220-
```rust
1221-
&str
1222-
```
1223-
"#]],
1158+
```rust
1159+
test
1160+
```
1161+
1162+
```rust
1163+
fn bar() -> u32
1164+
```
1165+
"#]],
12241166
);
12251167
}
12261168

@@ -5655,30 +5597,18 @@ fn main() {
56555597

56565598
#[test]
56575599
fn hover_underscore_type() {
5658-
check(
5600+
check_hover_no_result(
56595601
r#"
56605602
fn main() {
56615603
let x: _$0 = 0;
56625604
}
56635605
"#,
5664-
expect![[r#"
5665-
*_*
5666-
```rust
5667-
{unknown}
5668-
```
5669-
"#]],
56705606
);
5671-
check(
5607+
check_hover_no_result(
56725608
r#"
56735609
fn main() {
56745610
let x: (_$0,) = (0,);
56755611
}
56765612
"#,
5677-
expect![[r#"
5678-
*_*
5679-
```rust
5680-
{unknown}
5681-
```
5682-
"#]],
56835613
);
56845614
}

0 commit comments

Comments
 (0)