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/goto_definition.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/goto_definition.rs')
-rw-r--r-- | crates/ide/src/goto_definition.rs | 23 |
1 files changed, 8 insertions, 15 deletions
diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs index d8e0dc4d5..df6c35780 100644 --- a/crates/ide/src/goto_definition.rs +++ b/crates/ide/src/goto_definition.rs | |||
@@ -5,11 +5,10 @@ use hir::{AsAssocItem, InFile, ModuleDef, Semantics}; | |||
5 | use ide_db::{ | 5 | use ide_db::{ |
6 | base_db::{AnchoredPath, FileId, FileLoader}, | 6 | base_db::{AnchoredPath, FileId, FileLoader}, |
7 | defs::{Definition, NameClass, NameRefClass}, | 7 | defs::{Definition, NameClass, NameRefClass}, |
8 | helpers::pick_best_token, | ||
8 | RootDatabase, | 9 | RootDatabase, |
9 | }; | 10 | }; |
10 | use syntax::{ | 11 | use syntax::{ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, TextRange, T}; |
11 | ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, TextRange, TokenAtOffset, T, | ||
12 | }; | ||
13 | 12 | ||
14 | use crate::{ | 13 | use crate::{ |
15 | display::TryToNav, | 14 | display::TryToNav, |
@@ -34,7 +33,12 @@ pub(crate) fn goto_definition( | |||
34 | ) -> Option<RangeInfo<Vec<NavigationTarget>>> { | 33 | ) -> Option<RangeInfo<Vec<NavigationTarget>>> { |
35 | let sema = Semantics::new(db); | 34 | let sema = Semantics::new(db); |
36 | let file = sema.parse(position.file_id).syntax().clone(); | 35 | let file = sema.parse(position.file_id).syntax().clone(); |
37 | let original_token = pick_best(file.token_at_offset(position.offset))?; | 36 | let original_token = |
37 | pick_best_token(file.token_at_offset(position.offset), |kind| match kind { | ||
38 | IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | COMMENT => 2, | ||
39 | kind if kind.is_trivia() => 0, | ||
40 | _ => 1, | ||
41 | })?; | ||
38 | let token = sema.descend_into_macros(original_token.clone()); | 42 | let token = sema.descend_into_macros(original_token.clone()); |
39 | let parent = token.parent()?; | 43 | let parent = token.parent()?; |
40 | if let Some(_) = ast::Comment::cast(token.clone()) { | 44 | if let Some(_) = ast::Comment::cast(token.clone()) { |
@@ -128,17 +132,6 @@ fn try_find_trait_item_definition(db: &RootDatabase, def: &Definition) -> Option | |||
128 | .find_map(|itm| (itm.name(db)? == name).then(|| itm.try_to_nav(db)).flatten()) | 132 | .find_map(|itm| (itm.name(db)? == name).then(|| itm.try_to_nav(db)).flatten()) |
129 | } | 133 | } |
130 | 134 | ||
131 | fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { | ||
132 | return tokens.max_by_key(priority); | ||
133 | fn priority(n: &SyntaxToken) -> usize { | ||
134 | match n.kind() { | ||
135 | IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | COMMENT => 2, | ||
136 | kind if kind.is_trivia() => 0, | ||
137 | _ => 1, | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | |||
142 | pub(crate) fn reference_definition( | 135 | pub(crate) fn reference_definition( |
143 | sema: &Semantics<RootDatabase>, | 136 | sema: &Semantics<RootDatabase>, |
144 | name_ref: Either<&ast::Lifetime, &ast::NameRef>, | 137 | name_ref: Either<&ast::Lifetime, &ast::NameRef>, |