diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-06-22 18:17:54 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-22 18:17:54 +0100 |
commit | 76c88e9ca9ea9000d7d9eda1b2dae3173296f738 (patch) | |
tree | 8e7d42ad63269c668b189c0a59d55ebd53328f90 /crates/ide/src/doc_links.rs | |
parent | 4e2ec914f4b9609d162c3fd1776e8d293428fe5a (diff) | |
parent | f615efdfc3b4655e4f74068769905404cd911b5e (diff) |
Merge #9373
9373: internal: Factor out `pick_best_token` ide pattern into `ide_db` r=Veykril a=Veykril
bors r+
Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ide/src/doc_links.rs')
-rw-r--r-- | crates/ide/src/doc_links.rs | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index 7ac0118fe..dfab8d313 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs | |||
@@ -16,11 +16,10 @@ use hir::{ | |||
16 | }; | 16 | }; |
17 | use ide_db::{ | 17 | use ide_db::{ |
18 | defs::{Definition, NameClass, NameRefClass}, | 18 | defs::{Definition, NameClass, NameRefClass}, |
19 | helpers::pick_best_token, | ||
19 | RootDatabase, | 20 | RootDatabase, |
20 | }; | 21 | }; |
21 | use syntax::{ | 22 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxNode, TextRange, T}; |
22 | ast, match_ast, AstNode, SyntaxKind::*, SyntaxNode, SyntaxToken, TextRange, TokenAtOffset, T, | ||
23 | }; | ||
24 | 23 | ||
25 | use crate::{FilePosition, Semantics}; | 24 | use crate::{FilePosition, Semantics}; |
26 | 25 | ||
@@ -102,7 +101,12 @@ pub(crate) fn external_docs( | |||
102 | ) -> Option<DocumentationLink> { | 101 | ) -> Option<DocumentationLink> { |
103 | let sema = Semantics::new(db); | 102 | let sema = Semantics::new(db); |
104 | let file = sema.parse(position.file_id).syntax().clone(); | 103 | let file = sema.parse(position.file_id).syntax().clone(); |
105 | let token = pick_best(file.token_at_offset(position.offset))?; | 104 | let token = pick_best_token(file.token_at_offset(position.offset), |kind| match kind { |
105 | IDENT | INT_NUMBER => 3, | ||
106 | T!['('] | T![')'] => 2, | ||
107 | kind if kind.is_trivia() => 0, | ||
108 | _ => 1, | ||
109 | })?; | ||
106 | let token = sema.descend_into_macros(token); | 110 | let token = sema.descend_into_macros(token); |
107 | 111 | ||
108 | let node = token.parent()?; | 112 | let node = token.parent()?; |
@@ -522,18 +526,6 @@ fn get_symbol_fragment(db: &dyn HirDatabase, field_or_assoc: &FieldOrAssocItem) | |||
522 | }) | 526 | }) |
523 | } | 527 | } |
524 | 528 | ||
525 | fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { | ||
526 | return tokens.max_by_key(priority); | ||
527 | fn priority(n: &SyntaxToken) -> usize { | ||
528 | match n.kind() { | ||
529 | IDENT | INT_NUMBER => 3, | ||
530 | T!['('] | T![')'] => 2, | ||
531 | kind if kind.is_trivia() => 0, | ||
532 | _ => 1, | ||
533 | } | ||
534 | } | ||
535 | } | ||
536 | |||
537 | #[cfg(test)] | 529 | #[cfg(test)] |
538 | mod tests { | 530 | mod tests { |
539 | use expect_test::{expect, Expect}; | 531 | use expect_test::{expect, Expect}; |