Skip to content

Commit 4373a5d

Browse files
committed
refactor: improve macro handling in navigation for control-flow kws
1 parent 62e112a commit 4373a5d

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,10 @@ fn nav_for_branches(
419419
.descend_into_macros(token.clone())
420420
.into_iter()
421421
.filter_map(|token| {
422-
let match_expr =
423-
sema.token_ancestors_with_macros(token).find_map(ast::MatchExpr::cast)?;
422+
let match_expr = sema
423+
.token_ancestors_with_macros(token)
424+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
425+
.find_map(ast::MatchExpr::cast)?;
424426
let file_id = sema.hir_file_for(match_expr.syntax());
425427
let focus_range = match_expr.match_token()?.text_range();
426428
let match_expr_in_file = InFile::new(file_id, match_expr.into());
@@ -433,8 +435,10 @@ fn nav_for_branches(
433435
.descend_into_macros(token.clone())
434436
.into_iter()
435437
.filter_map(|token| {
436-
let match_arm =
437-
sema.token_ancestors_with_macros(token).find_map(ast::MatchArm::cast)?;
438+
let match_arm = sema
439+
.token_ancestors_with_macros(token)
440+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
441+
.find_map(ast::MatchArm::cast)?;
438442
let match_expr = sema
439443
.ancestors_with_macros(match_arm.syntax().clone())
440444
.find_map(ast::MatchExpr::cast)?;
@@ -450,8 +454,10 @@ fn nav_for_branches(
450454
.descend_into_macros(token.clone())
451455
.into_iter()
452456
.filter_map(|token| {
453-
let if_expr =
454-
sema.token_ancestors_with_macros(token).find_map(ast::IfExpr::cast)?;
457+
let if_expr = sema
458+
.token_ancestors_with_macros(token)
459+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
460+
.find_map(ast::IfExpr::cast)?;
455461
let file_id = sema.hir_file_for(if_expr.syntax());
456462
let focus_range = if_expr.if_token()?.text_range();
457463
let if_expr_in_file = InFile::new(file_id, if_expr.into());

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,10 @@ pub(crate) fn highlight_branches(
334334
match token.kind() {
335335
T![match] => {
336336
for token in sema.descend_into_macros(token.clone()) {
337-
let Some(match_expr) =
338-
sema.token_ancestors_with_macros(token).find_map(ast::MatchExpr::cast)
337+
let Some(match_expr) = sema
338+
.token_ancestors_with_macros(token)
339+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
340+
.find_map(ast::MatchExpr::cast)
339341
else {
340342
continue;
341343
};
@@ -355,11 +357,14 @@ pub(crate) fn highlight_branches(
355357
}
356358
T![=>] => {
357359
for token in sema.descend_into_macros(token.clone()) {
358-
let Some(arm) =
359-
sema.token_ancestors_with_macros(token).find_map(ast::MatchArm::cast)
360+
let Some(arm) = sema
361+
.token_ancestors_with_macros(token)
362+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
363+
.find_map(ast::MatchArm::cast)
360364
else {
361365
continue;
362366
};
367+
363368
let file_id = sema.hir_file_for(arm.syntax());
364369
let range = arm.fat_arrow_token().map(|token| token.text_range());
365370
push_to_highlights(file_id, range, &mut highlights);
@@ -368,9 +373,11 @@ pub(crate) fn highlight_branches(
368373
}
369374
}
370375
T![if] => {
371-
for tok in sema.descend_into_macros(token.clone()) {
372-
let Some(if_expr) =
373-
sema.token_ancestors_with_macros(tok).find_map(ast::IfExpr::cast)
376+
for token in sema.descend_into_macros(token.clone()) {
377+
let Some(if_expr) = sema
378+
.token_ancestors_with_macros(token)
379+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
380+
.find_map(ast::IfExpr::cast)
374381
else {
375382
continue;
376383
};
@@ -2481,6 +2488,27 @@ fn main() {
24812488
)
24822489
}
24832490

2491+
#[test]
2492+
fn match_in_macro() {
2493+
// We should not highlight the outer `match` expression.
2494+
check(
2495+
r#"
2496+
macro_rules! M {
2497+
(match) => { 1 };
2498+
}
2499+
2500+
fn main() {
2501+
match Some(1) {
2502+
Some(x) => x,
2503+
None => {
2504+
M!(match$0)
2505+
}
2506+
}
2507+
}
2508+
"#,
2509+
)
2510+
}
2511+
24842512
#[test]
24852513
fn labeled_block_tail_expr() {
24862514
check(

0 commit comments

Comments
 (0)