diff options
-rw-r--r-- | crates/ide/src/expand_macro.rs | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index cc43c5772..12a091ac4 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs | |||
@@ -2,7 +2,10 @@ use std::iter; | |||
2 | 2 | ||
3 | use hir::Semantics; | 3 | use hir::Semantics; |
4 | use ide_db::RootDatabase; | 4 | use ide_db::RootDatabase; |
5 | use syntax::{ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, WalkEvent, T}; | 5 | use syntax::{ |
6 | ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, SyntaxToken, | ||
7 | TokenAtOffset, WalkEvent, T, | ||
8 | }; | ||
6 | 9 | ||
7 | use crate::FilePosition; | 10 | use crate::FilePosition; |
8 | 11 | ||
@@ -26,7 +29,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< | |||
26 | let sema = Semantics::new(db); | 29 | let sema = Semantics::new(db); |
27 | let file = sema.parse(position.file_id); | 30 | let file = sema.parse(position.file_id); |
28 | 31 | ||
29 | let tok = file.syntax().token_at_offset(position.offset).left_biased()?; | 32 | let tok = pick_best(file.syntax().token_at_offset(position.offset))?; |
30 | let mut expanded = None; | 33 | let mut expanded = None; |
31 | let mut name = None; | 34 | let mut name = None; |
32 | for node in tok.ancestors() { | 35 | for node in tok.ancestors() { |
@@ -54,6 +57,16 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< | |||
54 | Some(ExpandedMacro { name: name?, expansion }) | 57 | Some(ExpandedMacro { name: name?, expansion }) |
55 | } | 58 | } |
56 | 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 | |||
57 | fn expand_macro_recur( | 70 | fn expand_macro_recur( |
58 | sema: &Semantics<RootDatabase>, | 71 | sema: &Semantics<RootDatabase>, |
59 | macro_call: &ast::MacroCall, | 72 | macro_call: &ast::MacroCall, |