mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Merge #9368
9368: fix: Prefer identifier tokens in expand_macro r=Veykril a=Veykril Fixes #9366 bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
ff92afb4c1
@ -2,7 +2,10 @@ use std::iter;
|
|||||||
|
|
||||||
use hir::Semantics;
|
use hir::Semantics;
|
||||||
use ide_db::RootDatabase;
|
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;
|
use crate::FilePosition;
|
||||||
|
|
||||||
@ -26,7 +29,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
|
|||||||
let sema = Semantics::new(db);
|
let sema = Semantics::new(db);
|
||||||
let file = sema.parse(position.file_id);
|
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 expanded = None;
|
||||||
let mut name = None;
|
let mut name = None;
|
||||||
for node in tok.ancestors() {
|
for node in tok.ancestors() {
|
||||||
@ -54,6 +57,16 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
|
|||||||
Some(ExpandedMacro { name: name?, expansion })
|
Some(ExpandedMacro { name: name?, expansion })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
|
||||||
|
return tokens.max_by_key(priority);
|
||||||
|
fn priority(n: &SyntaxToken) -> usize {
|
||||||
|
match n.kind() {
|
||||||
|
IDENT => 1,
|
||||||
|
_ => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn expand_macro_recur(
|
fn expand_macro_recur(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
macro_call: &ast::MacroCall,
|
macro_call: &ast::MacroCall,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user