Fix hover on the beginning of a nested expression

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 commit is contained in:
Florian Diebold 2019-04-28 15:43:10 +02:00
parent 1dd3d9bc2d
commit 85633656df
3 changed files with 33 additions and 10 deletions

View file

@ -1,7 +1,7 @@
use ra_db::SourceDatabase;
use ra_syntax::{
AstNode, ast,
algo::{find_covering_element, find_node_at_offset, find_token_at_offset},
algo::{find_covering_element, find_node_at_offset, ancestors_at_offset},
};
use hir::HirDisplay;
@ -104,12 +104,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
}
if range.is_none() {
let node = find_token_at_offset(file.syntax(), position.offset).find_map(|token| {
token
.parent()
.ancestors()
.find(|n| ast::Expr::cast(*n).is_some() || ast::Pat::cast(*n).is_some())
})?;
let node = ancestors_at_offset(file.syntax(), position.offset)
.find(|n| ast::Expr::cast(*n).is_some() || ast::Pat::cast(*n).is_some())?;
let frange = FileRange { file_id: position.file_id, range: node.range() };
res.extend(type_of(db, frange).map(rust_code_markup));
range = Some(node.range());
@ -397,6 +393,17 @@ The Some variant
assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
}
#[test]
fn hover_local_var_edge() {
let (analysis, position) = single_file_with_position(
"
fn func(foo: i32) { if true { <|>foo; }; }
",
);
let hover = analysis.hover(position).unwrap().unwrap();
assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
}
#[test]
fn test_type_of_for_function() {
let (analysis, range) = single_file_with_range(