diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-04-28 15:11:07 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-04-28 15:11:07 +0100 |
commit | 8138b1da4f1564913a1a22407c65e77aa5320d56 (patch) | |
tree | 6b037199384ddf1021c2ad81886425dfcb8271a8 /crates/ra_ide_api | |
parent | 1dd3d9bc2d328e164c594f3d2022dcf174f907a9 (diff) | |
parent | 85633656dfc3ad4516a99e3062599e3ee85a578d (diff) |
Merge #1215
1215: Fix hover on the beginning of a nested expression r=matklad a=flodiebold
E.g. in
```
let foo = 1u32;
if true {
<|>foo;
}
```
the hover shows `()`, the type of the whole if expression, instead of the more
sensible `u32`. The reason for this was that the search for an expression was
slightly left-biased: When on the edge between two tokens, it first looked at
all ancestors of the left token and then of the right token. Instead merge the
ancestors in ascending order, so that we get the smaller of the two possible
expressions.
This might seem rather inconsequential, but emacs-lsp's sideline requests hover for the beginning of each word in the current line, so it tends to hit this a lot :smile: Actually, as I think about this now, another solution might be just making hover right-biased, since if I'm hovering a certain character I really mean that character and not the one to its left...
Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r-- | crates/ra_ide_api/src/goto_definition.rs | 1 | ||||
-rw-r--r-- | crates/ra_ide_api/src/hover.rs | 21 |
2 files changed, 15 insertions, 7 deletions
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 40a2bd148..163781f88 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs | |||
@@ -25,6 +25,7 @@ pub(crate) fn goto_definition( | |||
25 | None | 25 | None |
26 | } | 26 | } |
27 | 27 | ||
28 | #[derive(Debug)] | ||
28 | pub(crate) enum ReferenceResult { | 29 | pub(crate) enum ReferenceResult { |
29 | Exact(NavigationTarget), | 30 | Exact(NavigationTarget), |
30 | Approximate(Vec<NavigationTarget>), | 31 | Approximate(Vec<NavigationTarget>), |
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 397b56786..6545a2581 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use ra_db::SourceDatabase; | 1 | use ra_db::SourceDatabase; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | AstNode, ast, | 3 | AstNode, ast, |
4 | algo::{find_covering_element, find_node_at_offset, find_token_at_offset}, | 4 | algo::{find_covering_element, find_node_at_offset, ancestors_at_offset}, |
5 | }; | 5 | }; |
6 | use hir::HirDisplay; | 6 | use hir::HirDisplay; |
7 | 7 | ||
@@ -104,12 +104,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn | |||
104 | } | 104 | } |
105 | 105 | ||
106 | if range.is_none() { | 106 | if range.is_none() { |
107 | let node = find_token_at_offset(file.syntax(), position.offset).find_map(|token| { | 107 | let node = ancestors_at_offset(file.syntax(), position.offset) |
108 | token | 108 | .find(|n| ast::Expr::cast(*n).is_some() || ast::Pat::cast(*n).is_some())?; |
109 | .parent() | ||
110 | .ancestors() | ||
111 | .find(|n| ast::Expr::cast(*n).is_some() || ast::Pat::cast(*n).is_some()) | ||
112 | })?; | ||
113 | let frange = FileRange { file_id: position.file_id, range: node.range() }; | 109 | let frange = FileRange { file_id: position.file_id, range: node.range() }; |
114 | res.extend(type_of(db, frange).map(rust_code_markup)); | 110 | res.extend(type_of(db, frange).map(rust_code_markup)); |
115 | range = Some(node.range()); | 111 | range = Some(node.range()); |
@@ -398,6 +394,17 @@ The Some variant | |||
398 | } | 394 | } |
399 | 395 | ||
400 | #[test] | 396 | #[test] |
397 | fn hover_local_var_edge() { | ||
398 | let (analysis, position) = single_file_with_position( | ||
399 | " | ||
400 | fn func(foo: i32) { if true { <|>foo; }; } | ||
401 | ", | ||
402 | ); | ||
403 | let hover = analysis.hover(position).unwrap().unwrap(); | ||
404 | assert_eq!(trim_markup_opt(hover.info.first()), Some("i32")); | ||
405 | } | ||
406 | |||
407 | #[test] | ||
401 | fn test_type_of_for_function() { | 408 | fn test_type_of_for_function() { |
402 | let (analysis, range) = single_file_with_range( | 409 | let (analysis, range) = single_file_with_range( |
403 | " | 410 | " |