From c82529a97f10b1302d2944f1946bcb3479f64e2d Mon Sep 17 00:00:00 2001 From: succcubbus <16743652+succcubbus@users.noreply.github.com> Date: Fri, 13 Dec 2019 22:00:05 +0100 Subject: for goto and hover pick the token based on a priority --- crates/ra_ide/src/hover.rs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'crates/ra_ide/src/hover.rs') diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 5b48b1998..51e320128 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -1,11 +1,13 @@ //! FIXME: write short doc here -use hir::{db::AstDatabase, Adt, HasSource, HirDisplay, InFile}; +use hir::{db::AstDatabase, Adt, HasSource, HirDisplay}; use ra_db::SourceDatabase; use ra_syntax::{ algo::find_covering_element, ast::{self, DocCommentsOwner}, - match_ast, AstNode, SyntaxToken, + match_ast, AstNode, + SyntaxKind::*, + SyntaxToken, TokenAtOffset, }; use crate::{ @@ -156,17 +158,9 @@ fn hover_text_from_name_kind( pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option> { let file = db.parse_or_expand(position.file_id.into())?; - file.token_at_offset(position.offset) - .filter(|token| !token.kind().is_trivia()) - .map(|token| descend_into_macros(db, position.file_id, token)) - .find_map(|token| hover_token(db, position, token)) -} + let token = pick_best(file.token_at_offset(position.offset))?; + let token = descend_into_macros(db, position.file_id, token); -fn hover_token( - db: &RootDatabase, - position: FilePosition, - token: InFile, -) -> Option> { let mut res = HoverResult::new(); let mut range = match_ast! { @@ -226,6 +220,18 @@ fn hover_token( Some(RangeInfo::new(range, res)) } +fn pick_best(tokens: TokenAtOffset) -> Option { + return tokens.max_by_key(priority); + fn priority(n: &SyntaxToken) -> usize { + match n.kind() { + IDENT | INT_NUMBER => 3, + L_PAREN | R_PAREN => 2, + kind if kind.is_trivia() => 0, + _ => 1, + } + } +} + pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option { let parse = db.parse(frange.file_id); let leaf_node = find_covering_element(parse.tree().syntax(), frange.range); -- cgit v1.2.3