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_db/src/helpers.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'crates/ide_db') diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs index d96028cbc..bc21977e3 100644 --- a/crates/ide_db/src/helpers.rs +++ b/crates/ide_db/src/helpers.rs @@ -10,7 +10,10 @@ use std::collections::VecDeque; use base_db::FileId; use either::Either; use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait}; -use syntax::ast::{self, make}; +use syntax::{ + ast::{self, make}, + SyntaxKind, SyntaxToken, TokenAtOffset, +}; use crate::RootDatabase; @@ -22,6 +25,14 @@ pub fn item_name(db: &RootDatabase, item: ItemInNs) -> Option { } } +/// Picks the token with the highest rank returned by the passed in function. +pub fn pick_best_token( + tokens: TokenAtOffset, + f: impl Fn(SyntaxKind) -> usize, +) -> Option { + tokens.max_by_key(move |t| f(t.kind())) +} + /// Converts the mod path struct into its ast representation. pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path { let _p = profile::span("mod_path_to_ast"); -- cgit v1.2.3