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/goto_definition.rs | 18 +++++++++++++++--- crates/ra_ide/src/goto_type_definition.rs | 15 +++++++++++++-- crates/ra_ide/src/hover.rs | 30 ++++++++++++++++++------------ 3 files changed, 46 insertions(+), 17 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 30118b43f..27052d72b 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -3,7 +3,9 @@ use hir::{db::AstDatabase, InFile}; use ra_syntax::{ ast::{self, DocCommentsOwner}, - match_ast, AstNode, SyntaxKind, SyntaxNode, + match_ast, AstNode, + SyntaxKind::*, + SyntaxNode, SyntaxToken, TokenAtOffset, }; use crate::{ @@ -19,8 +21,7 @@ pub(crate) fn goto_definition( position: FilePosition, ) -> Option>> { let file = db.parse_or_expand(position.file_id.into())?; - let original_token = - file.token_at_offset(position.offset).find(|it| it.kind() == SyntaxKind::IDENT)?; + let original_token = pick_best(file.token_at_offset(position.offset))?; let token = descend_into_macros(db, position.file_id, original_token.clone()); let nav_targets = match_ast! { @@ -38,6 +39,17 @@ pub(crate) fn goto_definition( Some(RangeInfo::new(original_token.text_range(), nav_targets)) } +fn pick_best(tokens: TokenAtOffset) -> Option { + return tokens.max_by_key(priority); + fn priority(n: &SyntaxToken) -> usize { + match n.kind() { + IDENT | INT_NUMBER => 2, + kind if kind.is_trivia() => 0, + _ => 1, + } + } +} + #[derive(Debug)] pub(crate) enum ReferenceResult { Exact(NavigationTarget), diff --git a/crates/ra_ide/src/goto_type_definition.rs b/crates/ra_ide/src/goto_type_definition.rs index 5501bb742..ce8b6c72a 100644 --- a/crates/ra_ide/src/goto_type_definition.rs +++ b/crates/ra_ide/src/goto_type_definition.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use hir::db::AstDatabase; -use ra_syntax::{ast, AstNode, SyntaxKind}; +use ra_syntax::{ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset}; use crate::{ db::RootDatabase, display::ToNav, expand::descend_into_macros, FilePosition, NavigationTarget, @@ -13,7 +13,7 @@ pub(crate) fn goto_type_definition( position: FilePosition, ) -> Option>> { let file = db.parse_or_expand(position.file_id.into())?; - let token = file.token_at_offset(position.offset).find(|it| it.kind() == SyntaxKind::IDENT)?; + let token = pick_best(file.token_at_offset(position.offset))?; let token = descend_into_macros(db, position.file_id, token); let node = token.value.ancestors().find_map(|token| { @@ -41,6 +41,17 @@ pub(crate) fn goto_type_definition( Some(RangeInfo::new(node.text_range(), vec![nav])) } +fn pick_best(tokens: TokenAtOffset) -> Option { + return tokens.max_by_key(priority); + fn priority(n: &SyntaxToken) -> usize { + match n.kind() { + IDENT | INT_NUMBER => 2, + kind if kind.is_trivia() => 0, + _ => 1, + } + } +} + #[cfg(test)] mod tests { use crate::mock_analysis::analysis_and_position; 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