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/expand_macro.rs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) (limited to 'crates/ide/src/expand_macro.rs') diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index 12a091ac4..854d54b71 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs @@ -1,11 +1,8 @@ use std::iter; use hir::Semantics; -use ide_db::RootDatabase; -use syntax::{ - ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, SyntaxToken, - TokenAtOffset, WalkEvent, T, -}; +use ide_db::{helpers::pick_best_token, RootDatabase}; +use syntax::{ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, WalkEvent, T}; use crate::FilePosition; @@ -29,7 +26,10 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< let sema = Semantics::new(db); let file = sema.parse(position.file_id); - let tok = pick_best(file.syntax().token_at_offset(position.offset))?; + let tok = pick_best_token(file.syntax().token_at_offset(position.offset), |kind| match kind { + SyntaxKind::IDENT => 1, + _ => 0, + })?; let mut expanded = None; let mut name = None; for node in tok.ancestors() { @@ -57,16 +57,6 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< Some(ExpandedMacro { name: name?, expansion }) } -fn pick_best(tokens: TokenAtOffset) -> Option { - return tokens.max_by_key(priority); - fn priority(n: &SyntaxToken) -> usize { - match n.kind() { - IDENT => 1, - _ => 0, - } - } -} - fn expand_macro_recur( sema: &Semantics, macro_call: &ast::MacroCall, -- cgit v1.2.3