Skip to content

Commit 6d3a9e1

Browse files
committed
refactor: improve macro handling in navigation for control-flow kws
1 parent 63144ed commit 6d3a9e1

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

crates/ide/src/goto_definition.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,10 @@ fn nav_for_branches(
416416
.descend_into_macros(token.clone())
417417
.into_iter()
418418
.filter_map(|token| {
419-
let match_expr =
420-
sema.token_ancestors_with_macros(token).find_map(ast::MatchExpr::cast)?;
419+
let match_expr = sema
420+
.token_ancestors_with_macros(token)
421+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
422+
.find_map(ast::MatchExpr::cast)?;
421423
let file_id = sema.hir_file_for(match_expr.syntax());
422424
let focus_range = match_expr.match_token()?.text_range();
423425
let match_expr_in_file = InFile::new(file_id, match_expr.into());
@@ -430,8 +432,10 @@ fn nav_for_branches(
430432
.descend_into_macros(token.clone())
431433
.into_iter()
432434
.filter_map(|token| {
433-
let match_arm =
434-
sema.token_ancestors_with_macros(token).find_map(ast::MatchArm::cast)?;
435+
let match_arm = sema
436+
.token_ancestors_with_macros(token)
437+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
438+
.find_map(ast::MatchArm::cast)?;
435439
let match_expr = sema
436440
.ancestors_with_macros(match_arm.syntax().clone())
437441
.find_map(ast::MatchExpr::cast)?;
@@ -447,8 +451,10 @@ fn nav_for_branches(
447451
.descend_into_macros(token.clone())
448452
.into_iter()
449453
.filter_map(|token| {
450-
let if_expr =
451-
sema.token_ancestors_with_macros(token).find_map(ast::IfExpr::cast)?;
454+
let if_expr = sema
455+
.token_ancestors_with_macros(token)
456+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
457+
.find_map(ast::IfExpr::cast)?;
452458
let file_id = sema.hir_file_for(if_expr.syntax());
453459
let focus_range = if_expr.if_token()?.text_range();
454460
let if_expr_in_file = InFile::new(file_id, if_expr.into());

crates/ide/src/highlight_related.rs

+35-7
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,10 @@ pub(crate) fn highlight_branches(
318318
match token.kind() {
319319
T![match] => {
320320
for token in sema.descend_into_macros(token.clone()) {
321-
let Some(match_expr) =
322-
sema.token_ancestors_with_macros(token).find_map(ast::MatchExpr::cast)
321+
let Some(match_expr) = sema
322+
.token_ancestors_with_macros(token)
323+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
324+
.find_map(ast::MatchExpr::cast)
323325
else {
324326
continue;
325327
};
@@ -339,11 +341,14 @@ pub(crate) fn highlight_branches(
339341
}
340342
T![=>] => {
341343
for token in sema.descend_into_macros(token.clone()) {
342-
let Some(arm) =
343-
sema.token_ancestors_with_macros(token).find_map(ast::MatchArm::cast)
344+
let Some(arm) = sema
345+
.token_ancestors_with_macros(token)
346+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
347+
.find_map(ast::MatchArm::cast)
344348
else {
345349
continue;
346350
};
351+
347352
let file_id = sema.hir_file_for(arm.syntax());
348353
let range = arm.fat_arrow_token().map(|token| token.text_range());
349354
push_to_highlights(file_id, range, &mut highlights);
@@ -352,9 +357,11 @@ pub(crate) fn highlight_branches(
352357
}
353358
}
354359
T![if] => {
355-
for tok in sema.descend_into_macros(token.clone()) {
356-
let Some(if_expr) =
357-
sema.token_ancestors_with_macros(tok).find_map(ast::IfExpr::cast)
360+
for token in sema.descend_into_macros(token.clone()) {
361+
let Some(if_expr) = sema
362+
.token_ancestors_with_macros(token)
363+
.take_while(|node| !ast::MacroCall::can_cast(node.kind()))
364+
.find_map(ast::IfExpr::cast)
358365
else {
359366
continue;
360367
};
@@ -2388,4 +2395,25 @@ fn main() {
23882395
"#,
23892396
)
23902397
}
2398+
2399+
#[test]
2400+
fn match_in_macro() {
2401+
// We should not highlight the outer `match` expression.
2402+
check(
2403+
r#"
2404+
macro_rules! M {
2405+
(match) => { 1 };
2406+
}
2407+
2408+
fn main() {
2409+
match Some(1) {
2410+
Some(x) => x,
2411+
None => {
2412+
M!(match$0)
2413+
}
2414+
}
2415+
}
2416+
"#,
2417+
)
2418+
}
23912419
}

0 commit comments

Comments
 (0)