From b423c61ce662817a491fb08461c38e8c8cf08f73 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 22 Jun 2021 12:03:51 +0200 Subject: Prefer identifier tokens in expand_macro --- crates/ide/src/expand_macro.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'crates/ide/src') 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; use hir::Semantics; use ide_db::RootDatabase; -use syntax::{ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, WalkEvent, T}; +use syntax::{ + ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, SyntaxToken, + TokenAtOffset, WalkEvent, T, +}; use crate::FilePosition; @@ -26,7 +29,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< let sema = Semantics::new(db); let file = sema.parse(position.file_id); - let tok = file.syntax().token_at_offset(position.offset).left_biased()?; + let tok = pick_best(file.syntax().token_at_offset(position.offset))?; let mut expanded = None; let mut name = None; for node in tok.ancestors() { @@ -54,6 +57,16 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< Some(ExpandedMacro { name: name?, expansion }) } +fn pick_best(tokens: TokenAtOffset) -> Option { + return tokens.max_by_key(priority); + fn priority(n: &SyntaxToken) -> usize { + match n.kind() { + IDENT => 1, + _ => 0, + } + } +} + fn expand_macro_recur( sema: &Semantics, macro_call: &ast::MacroCall, -- cgit v1.2.3