From 85633656dfc3ad4516a99e3062599e3ee85a578d Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 28 Apr 2019 15:43:10 +0200 Subject: 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. --- crates/ra_ide_api/src/goto_definition.rs | 1 + crates/ra_ide_api/src/hover.rs | 21 ++++++++++++++------- crates/ra_syntax/src/algo.rs | 21 ++++++++++++++++++--- 3 files changed, 33 insertions(+), 10 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( None } +#[derive(Debug)] pub(crate) enum ReferenceResult { Exact(NavigationTarget), Approximate(Vec), 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 @@ 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) -> Optionfoo; }; } +", + ); + 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( diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 1f68fe467..d31d00343 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -1,5 +1,7 @@ pub mod visit; +use itertools::Itertools; + use crate::{SyntaxNode, TextRange, TextUnit, AstNode, Direction, SyntaxToken, SyntaxElement}; pub use rowan::TokenAtOffset; @@ -12,6 +14,20 @@ pub fn find_token_at_offset(node: &SyntaxNode, offset: TextUnit) -> TokenAtOffse } } +/// Returns ancestors of the node at the offset, sorted by length. This should +/// do the right thing at an edge, e.g. when searching for expressions at `{ +/// <|>foo }` we will get the name reference instead of the whole block, which +/// we would get if we just did `find_token_at_offset(...).flat_map(|t| +/// t.parent().ancestors())`. +pub fn ancestors_at_offset( + node: &SyntaxNode, + offset: TextUnit, +) -> impl Iterator { + find_token_at_offset(node, offset) + .map(|token| token.parent().ancestors()) + .kmerge_by(|node1, node2| node1.range().len() < node2.range().len()) +} + /// Finds a node of specific Ast type at offset. Note that this is slightly /// imprecise: if the cursor is strictly between two nodes of the desired type, /// as in @@ -20,10 +36,9 @@ pub fn find_token_at_offset(node: &SyntaxNode, offset: TextUnit) -> TokenAtOffse /// struct Foo {}|struct Bar; /// ``` /// -/// then the left node will be silently preferred. +/// then the shorter node will be silently preferred. pub fn find_node_at_offset(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> { - find_token_at_offset(syntax, offset) - .find_map(|leaf| leaf.parent().ancestors().find_map(N::cast)) + ancestors_at_offset(syntax, offset).find_map(N::cast) } /// Finds the first sibling in the given direction which is not `trivia` -- cgit v1.2.3