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/expand_macro.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/expand_macro.rs')
-rw-r--r-- | crates/ide/src/expand_macro.rs | 22 |
1 files changed, 6 insertions, 16 deletions
diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index 12a091ac4..854d54b71 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs | |||
@@ -1,11 +1,8 @@ | |||
1 | use std::iter; | 1 | use std::iter; |
2 | 2 | ||
3 | use hir::Semantics; | 3 | use hir::Semantics; |
4 | use ide_db::RootDatabase; | 4 | use ide_db::{helpers::pick_best_token, RootDatabase}; |
5 | use syntax::{ | 5 | use syntax::{ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, WalkEvent, T}; |
6 | ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, SyntaxToken, | ||
7 | TokenAtOffset, WalkEvent, T, | ||
8 | }; | ||
9 | 6 | ||
10 | use crate::FilePosition; | 7 | use crate::FilePosition; |
11 | 8 | ||
@@ -29,7 +26,10 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< | |||
29 | let sema = Semantics::new(db); | 26 | let sema = Semantics::new(db); |
30 | let file = sema.parse(position.file_id); | 27 | let file = sema.parse(position.file_id); |
31 | 28 | ||
32 | let tok = pick_best(file.syntax().token_at_offset(position.offset))?; | 29 | let tok = pick_best_token(file.syntax().token_at_offset(position.offset), |kind| match kind { |
30 | SyntaxKind::IDENT => 1, | ||
31 | _ => 0, | ||
32 | })?; | ||
33 | let mut expanded = None; | 33 | let mut expanded = None; |
34 | let mut name = None; | 34 | let mut name = None; |
35 | for node in tok.ancestors() { | 35 | for node in tok.ancestors() { |
@@ -57,16 +57,6 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< | |||
57 | Some(ExpandedMacro { name: name?, expansion }) | 57 | Some(ExpandedMacro { name: name?, expansion }) |
58 | } | 58 | } |
59 | 59 | ||
60 | fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { | ||
61 | return tokens.max_by_key(priority); | ||
62 | fn priority(n: &SyntaxToken) -> usize { | ||
63 | match n.kind() { | ||
64 | IDENT => 1, | ||
65 | _ => 0, | ||
66 | } | ||
67 | } | ||
68 | } | ||
69 | |||
70 | fn expand_macro_recur( | 60 | fn expand_macro_recur( |
71 | sema: &Semantics<RootDatabase>, | 61 | sema: &Semantics<RootDatabase>, |
72 | macro_call: &ast::MacroCall, | 62 | macro_call: &ast::MacroCall, |