From f615efdfc3b4655e4f74068769905404cd911b5e Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 22 Jun 2021 17:28:07 +0200 Subject: Factor out `pick_best_token` ide pattern into `ide_db` --- crates/ide/src/doc_links.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'crates/ide/src/doc_links.rs') diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index 7ac0118fe..dfab8d313 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs @@ -16,11 +16,10 @@ use hir::{ }; use ide_db::{ defs::{Definition, NameClass, NameRefClass}, + helpers::pick_best_token, RootDatabase, }; -use syntax::{ - ast, match_ast, AstNode, SyntaxKind::*, SyntaxNode, SyntaxToken, TextRange, TokenAtOffset, T, -}; +use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxNode, TextRange, T}; use crate::{FilePosition, Semantics}; @@ -102,7 +101,12 @@ pub(crate) fn external_docs( ) -> Option { let sema = Semantics::new(db); let file = sema.parse(position.file_id).syntax().clone(); - let token = pick_best(file.token_at_offset(position.offset))?; + let token = pick_best_token(file.token_at_offset(position.offset), |kind| match kind { + IDENT | INT_NUMBER => 3, + T!['('] | T![')'] => 2, + kind if kind.is_trivia() => 0, + _ => 1, + })?; let token = sema.descend_into_macros(token); let node = token.parent()?; @@ -522,18 +526,6 @@ fn get_symbol_fragment(db: &dyn HirDatabase, field_or_assoc: &FieldOrAssocItem) }) } -fn pick_best(tokens: TokenAtOffset) -> Option { - return tokens.max_by_key(priority); - fn priority(n: &SyntaxToken) -> usize { - match n.kind() { - IDENT | INT_NUMBER => 3, - T!['('] | T![')'] => 2, - kind if kind.is_trivia() => 0, - _ => 1, - } - } -} - #[cfg(test)] mod tests { use expect_test::{expect, Expect}; -- cgit v1.2.3