Skip to content

Commit cec8955

Browse files
committed
refactor: improve macro handling in navigation for control-flow kws
1 parent 1880be0 commit cec8955

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

crates/ide/src/goto_definition.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,10 @@ fn nav_for_branches(
420420
.descend_into_macros(token.clone())
421421
.into_iter()
422422
.filter_map(|token| {
423-
let match_expr =
424-
sema.token_ancestors_with_macros(token).find_map(ast::MatchExpr::cast)?;
423+
let match_expr = sema
424+
.token_ancestors_with_macros(token)
425+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
426+
.find_map(ast::MatchExpr::cast)?;
425427
let file_id = sema.hir_file_for(match_expr.syntax());
426428
let focus_range = match_expr.match_token()?.text_range();
427429
let match_expr_in_file = InFile::new(file_id, match_expr.into());
@@ -434,8 +436,10 @@ fn nav_for_branches(
434436
.descend_into_macros(token.clone())
435437
.into_iter()
436438
.filter_map(|token| {
437-
let match_arm =
438-
sema.token_ancestors_with_macros(token).find_map(ast::MatchArm::cast)?;
439+
let match_arm = sema
440+
.token_ancestors_with_macros(token)
441+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
442+
.find_map(ast::MatchArm::cast)?;
439443
let match_expr = sema
440444
.ancestors_with_macros(match_arm.syntax().clone())
441445
.find_map(ast::MatchExpr::cast)?;
@@ -451,8 +455,10 @@ fn nav_for_branches(
451455
.descend_into_macros(token.clone())
452456
.into_iter()
453457
.filter_map(|token| {
454-
let if_expr =
455-
sema.token_ancestors_with_macros(token).find_map(ast::IfExpr::cast)?;
458+
let if_expr = sema
459+
.token_ancestors_with_macros(token)
460+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
461+
.find_map(ast::IfExpr::cast)?;
456462
let file_id = sema.hir_file_for(if_expr.syntax());
457463
let focus_range = if_expr.if_token()?.text_range();
458464
let if_expr_in_file = InFile::new(file_id, if_expr.into());

crates/ide/src/highlight_related.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,10 @@ pub(crate) fn highlight_branches(
335335
match token.kind() {
336336
T![match] => {
337337
for token in sema.descend_into_macros(token.clone()) {
338-
let Some(match_expr) =
339-
sema.token_ancestors_with_macros(token).find_map(ast::MatchExpr::cast)
338+
let Some(match_expr) = sema
339+
.token_ancestors_with_macros(token)
340+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
341+
.find_map(ast::MatchExpr::cast)
340342
else {
341343
continue;
342344
};
@@ -356,11 +358,14 @@ pub(crate) fn highlight_branches(
356358
}
357359
T![=>] => {
358360
for token in sema.descend_into_macros(token.clone()) {
359-
let Some(arm) =
360-
sema.token_ancestors_with_macros(token).find_map(ast::MatchArm::cast)
361+
let Some(arm) = sema
362+
.token_ancestors_with_macros(token)
363+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
364+
.find_map(ast::MatchArm::cast)
361365
else {
362366
continue;
363367
};
368+
364369
let file_id = sema.hir_file_for(arm.syntax());
365370
let range = arm.fat_arrow_token().map(|token| token.text_range());
366371
push_to_highlights(file_id, range, &mut highlights);
@@ -369,9 +374,11 @@ pub(crate) fn highlight_branches(
369374
}
370375
}
371376
T![if] => {
372-
for tok in sema.descend_into_macros(token.clone()) {
373-
let Some(if_expr) =
374-
sema.token_ancestors_with_macros(tok).find_map(ast::IfExpr::cast)
377+
for token in sema.descend_into_macros(token.clone()) {
378+
let Some(if_expr) = sema
379+
.token_ancestors_with_macros(token)
380+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
381+
.find_map(ast::IfExpr::cast)
375382
else {
376383
continue;
377384
};
@@ -2418,6 +2425,27 @@ fn main() {
24182425
)
24192426
}
24202427

2428+
#[test]
2429+
fn match_in_macro() {
2430+
// We should not highlight the outer `match` expression.
2431+
check(
2432+
r#"
2433+
macro_rules! M {
2434+
(match) => { 1 };
2435+
}
2436+
2437+
fn main() {
2438+
match Some(1) {
2439+
Some(x) => x,
2440+
None => {
2441+
M!(match$0)
2442+
}
2443+
}
2444+
}
2445+
"#,
2446+
)
2447+
}
2448+
24212449
#[test]
24222450
fn labeled_block_tail_expr() {
24232451
check(

0 commit comments

Comments
 (0)