Factor out pick_best_token ide pattern into ide_db

This commit is contained in:
Lukas Wirth 2021-06-22 17:28:07 +02:00
parent 4e2ec914f4
commit f615efdfc3
9 changed files with 62 additions and 101 deletions

View file

@ -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<Name> {
}
}
/// Picks the token with the highest rank returned by the passed in function.
pub fn pick_best_token(
tokens: TokenAtOffset<SyntaxToken>,
f: impl Fn(SyntaxKind) -> usize,
) -> Option<SyntaxToken> {
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");