aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db/src
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-06-22 16:28:07 +0100
committerLukas Wirth <[email protected]>2021-06-22 16:50:15 +0100
commitf615efdfc3b4655e4f74068769905404cd911b5e (patch)
tree8e7d42ad63269c668b189c0a59d55ebd53328f90 /crates/ide_db/src
parent4e2ec914f4b9609d162c3fd1776e8d293428fe5a (diff)
Factor out `pick_best_token` ide pattern into `ide_db`
Diffstat (limited to 'crates/ide_db/src')
-rw-r--r--crates/ide_db/src/helpers.rs13
1 files changed, 12 insertions, 1 deletions
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;
10use base_db::FileId; 10use base_db::FileId;
11use either::Either; 11use either::Either;
12use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait}; 12use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait};
13use syntax::ast::{self, make}; 13use syntax::{
14 ast::{self, make},
15 SyntaxKind, SyntaxToken, TokenAtOffset,
16};
14 17
15use crate::RootDatabase; 18use crate::RootDatabase;
16 19
@@ -22,6 +25,14 @@ pub fn item_name(db: &RootDatabase, item: ItemInNs) -> Option<Name> {
22 } 25 }
23} 26}
24 27
28/// Picks the token with the highest rank returned by the passed in function.
29pub fn pick_best_token(
30 tokens: TokenAtOffset<SyntaxToken>,
31 f: impl Fn(SyntaxKind) -> usize,
32) -> Option<SyntaxToken> {
33 tokens.max_by_key(move |t| f(t.kind()))
34}
35
25/// Converts the mod path struct into its ast representation. 36/// Converts the mod path struct into its ast representation.
26pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path { 37pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
27 let _p = profile::span("mod_path_to_ast"); 38 let _p = profile::span("mod_path_to_ast");