Skip to content

Commit dc9e9fd

Browse files
committed
refactor: enhance highlighting for control flow kws in macros
1 parent bff9f4c commit dc9e9fd

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

src/tools/rust-analyzer/crates/ide/src/goto_definition.rs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -412,45 +412,30 @@ pub(crate) fn find_branch_root(
412412
sema: &Semantics<'_, RootDatabase>,
413413
token: &SyntaxToken,
414414
) -> Vec<SyntaxNode> {
415-
fn find_root(
416-
sema: &Semantics<'_, RootDatabase>,
417-
token: &SyntaxToken,
418-
pred: impl Fn(SyntaxNode) -> Option<SyntaxNode>,
419-
) -> Vec<SyntaxNode> {
420-
let mut result = Vec::new();
421-
for token in sema.descend_into_macros(token.clone()) {
422-
for node in sema.token_ancestors_with_macros(token) {
423-
if ast::MacroCall::can_cast(node.kind()) {
424-
break;
425-
}
426-
427-
if let Some(node) = pred(node) {
428-
result.push(node);
429-
break;
430-
}
431-
}
432-
}
433-
result
434-
}
415+
let find_nodes = |node_filter: fn(SyntaxNode) -> Option<SyntaxNode>| {
416+
sema.descend_into_macros(token.clone())
417+
.into_iter()
418+
.filter_map(|token| node_filter(token.parent()?))
419+
.collect_vec()
420+
};
435421

436422
match token.kind() {
437-
T![match] => {
438-
find_root(sema, token, |node| Some(ast::MatchExpr::cast(node)?.syntax().clone()))
439-
}
440-
T![=>] => find_root(sema, token, |node| Some(ast::MatchArm::cast(node)?.syntax().clone())),
441-
T![if] => find_root(sema, token, |node| {
423+
T![match] => find_nodes(|node| Some(ast::MatchExpr::cast(node)?.syntax().clone())),
424+
T![=>] => find_nodes(|node| Some(ast::MatchArm::cast(node)?.syntax().clone())),
425+
T![if] => find_nodes(|node| {
442426
let if_expr = ast::IfExpr::cast(node)?;
443427

444-
iter::successors(Some(if_expr.clone()), |if_expr| {
428+
let root_if = iter::successors(Some(if_expr.clone()), |if_expr| {
445429
let parent_if = if_expr.syntax().parent().and_then(ast::IfExpr::cast)?;
446-
if let ast::ElseBranch::IfExpr(nested_if) = parent_if.else_branch()? {
447-
(nested_if.syntax() == if_expr.syntax()).then_some(parent_if)
448-
} else {
449-
None
450-
}
430+
let ast::ElseBranch::IfExpr(else_branch) = parent_if.else_branch()? else {
431+
return None;
432+
};
433+
434+
(else_branch.syntax() == if_expr.syntax()).then_some(parent_if)
451435
})
452-
.last()
453-
.map(|if_expr| if_expr.syntax().clone())
436+
.last()?;
437+
438+
Some(root_if.syntax().clone())
454439
}),
455440
_ => vec![],
456441
}

src/tools/rust-analyzer/crates/ide/src/highlight_related.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,6 +2356,33 @@ fn main() {
23562356
)
23572357
}
23582358

2359+
#[test]
2360+
fn match_in_macro_highlight_2() {
2361+
check(
2362+
r#"
2363+
macro_rules! match_ast {
2364+
(match $node:ident { $($tt:tt)* }) => { $crate::match_ast!(match ($node) { $($tt)* }) };
2365+
2366+
(match ($node:expr) {
2367+
$( $( $path:ident )::+ ($it:pat) => $res:expr, )*
2368+
_ => $catch_all:expr $(,)?
2369+
}) => {{
2370+
$( if let Some($it) = $($path::)+cast($node.clone()) { $res } else )*
2371+
{ $catch_all }
2372+
}};
2373+
}
2374+
2375+
fn main() {
2376+
match_ast! {
2377+
match$0 Some(1) {
2378+
Some(x) => x,
2379+
}
2380+
}
2381+
}
2382+
"#,
2383+
);
2384+
}
2385+
23592386
#[test]
23602387
fn nested_if_else() {
23612388
check(

0 commit comments

Comments
 (0)