aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-06-22 11:03:51 +0100
committerLukas Wirth <[email protected]>2021-06-22 11:03:51 +0100
commitb423c61ce662817a491fb08461c38e8c8cf08f73 (patch)
tree814e7c4c68eff5198d06b988b1e2647148ace77f /crates
parentc2aa7782d65c4f2765f68b99f00f4203e1f143c1 (diff)
Prefer identifier tokens in expand_macro
Diffstat (limited to 'crates')
-rw-r--r--crates/ide/src/expand_macro.rs17
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
3use hir::Semantics; 3use hir::Semantics;
4use ide_db::RootDatabase; 4use ide_db::RootDatabase;
5use syntax::{ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, WalkEvent, T}; 5use syntax::{
6 ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, SyntaxToken,
7 TokenAtOffset, WalkEvent, T,
8};
6 9
7use crate::FilePosition; 10use 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
60fn 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
57fn expand_macro_recur( 70fn expand_macro_recur(
58 sema: &Semantics<RootDatabase>, 71 sema: &Semantics<RootDatabase>,
59 macro_call: &ast::MacroCall, 72 macro_call: &ast::MacroCall,