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/src/hover.rs | |
parent | 4e2ec914f4b9609d162c3fd1776e8d293428fe5a (diff) |
Factor out `pick_best_token` ide pattern into `ide_db`
Diffstat (limited to 'crates/ide/src/hover.rs')
-rw-r--r-- | crates/ide/src/hover.rs | 27 |
1 files changed, 8 insertions, 19 deletions
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index c6d6bb74a..0eeea4efb 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs | |||
@@ -5,16 +5,13 @@ use ide_db::{ | |||
5 | defs::{Definition, NameClass, NameRefClass}, | 5 | defs::{Definition, NameClass, NameRefClass}, |
6 | helpers::{ | 6 | helpers::{ |
7 | generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES}, | 7 | generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES}, |
8 | FamousDefs, | 8 | pick_best_token, FamousDefs, |
9 | }, | 9 | }, |
10 | RootDatabase, | 10 | RootDatabase, |
11 | }; | 11 | }; |
12 | use itertools::Itertools; | 12 | use itertools::Itertools; |
13 | use stdx::format_to; | 13 | use stdx::format_to; |
14 | use syntax::{ | 14 | use syntax::{algo, ast, match_ast, AstNode, AstToken, Direction, SyntaxKind::*, SyntaxToken, T}; |
15 | algo, ast, match_ast, AstNode, AstToken, Direction, SyntaxKind::*, SyntaxToken, TokenAtOffset, | ||
16 | T, | ||
17 | }; | ||
18 | 15 | ||
19 | use crate::{ | 16 | use crate::{ |
20 | display::{macro_label, TryToNav}, | 17 | display::{macro_label, TryToNav}, |
@@ -80,7 +77,12 @@ pub(crate) fn hover( | |||
80 | ) -> Option<RangeInfo<HoverResult>> { | 77 | ) -> Option<RangeInfo<HoverResult>> { |
81 | let sema = hir::Semantics::new(db); | 78 | let sema = hir::Semantics::new(db); |
82 | let file = sema.parse(position.file_id).syntax().clone(); | 79 | let file = sema.parse(position.file_id).syntax().clone(); |
83 | let token = pick_best(file.token_at_offset(position.offset))?; | 80 | let token = pick_best_token(file.token_at_offset(position.offset), |kind| match kind { |
81 | IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | T![super] | T![crate] => 3, | ||
82 | T!['('] | T![')'] => 2, | ||
83 | kind if kind.is_trivia() => 0, | ||
84 | _ => 1, | ||
85 | })?; | ||
84 | let token = sema.descend_into_macros(token); | 86 | let token = sema.descend_into_macros(token); |
85 | 87 | ||
86 | let mut res = HoverResult::default(); | 88 | let mut res = HoverResult::default(); |
@@ -519,19 +521,6 @@ fn find_std_module(famous_defs: &FamousDefs, name: &str) -> Option<hir::Module> | |||
519 | .find(|module| module.name(db).map_or(false, |module| module.to_string() == name)) | 521 | .find(|module| module.name(db).map_or(false, |module| module.to_string() == name)) |
520 | } | 522 | } |
521 | 523 | ||
522 | fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { | ||
523 | return tokens.max_by_key(priority); | ||
524 | |||
525 | fn priority(n: &SyntaxToken) -> usize { | ||
526 | match n.kind() { | ||
527 | IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | T![super] | T![crate] => 3, | ||
528 | T!['('] | T![')'] => 2, | ||
529 | kind if kind.is_trivia() => 0, | ||
530 | _ => 1, | ||
531 | } | ||
532 | } | ||
533 | } | ||
534 | |||
535 | #[cfg(test)] | 524 | #[cfg(test)] |
536 | mod tests { | 525 | mod tests { |
537 | use expect_test::{expect, Expect}; | 526 | use expect_test::{expect, Expect}; |