aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-06-22 11:06:34 +0100
committerGitHub <[email protected]>2021-06-22 11:06:34 +0100
commitff92afb4c1934b611ab67d35dc6c6b113e97e525 (patch)
treef123d281c289bfe858150c16603ed84b7d943dfe /crates/ide/src
parente2ca2325f5f61284a4c924114a78cd263b4921cb (diff)
parentb423c61ce662817a491fb08461c38e8c8cf08f73 (diff)
Merge #9368
9368: fix: Prefer identifier tokens in expand_macro r=Veykril a=Veykril Fixes #9366 bors r+ Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ide/src')
-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,