diff options
author | Lukas Wirth <[email protected]> | 2021-06-22 16:28:07 +0100 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-06-22 16:50:15 +0100 |
commit | f615efdfc3b4655e4f74068769905404cd911b5e (patch) | |
tree | 8e7d42ad63269c668b189c0a59d55ebd53328f90 /crates/ide_db/src | |
parent | 4e2ec914f4b9609d162c3fd1776e8d293428fe5a (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.rs | 13 |
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; | |||
10 | use base_db::FileId; | 10 | use base_db::FileId; |
11 | use either::Either; | 11 | use either::Either; |
12 | use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait}; | 12 | use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait}; |
13 | use syntax::ast::{self, make}; | 13 | use syntax::{ |
14 | ast::{self, make}, | ||
15 | SyntaxKind, SyntaxToken, TokenAtOffset, | ||
16 | }; | ||
14 | 17 | ||
15 | use crate::RootDatabase; | 18 | use 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. | ||
29 | pub 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. |
26 | pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path { | 37 | pub 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"); |