From 78503f2d1f26d529d5e236f960d288a5dde2b10f Mon Sep 17 00:00:00 2001 From: roifewu Date: Thu, 10 Apr 2025 02:45:11 +0800 Subject: [PATCH 1/2] feat: highlight tail expression in labeled block --- crates/ide/src/highlight_related.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/crates/ide/src/highlight_related.rs b/crates/ide/src/highlight_related.rs index 201f86876088..8f84f35bb453 100644 --- a/crates/ide/src/highlight_related.rs +++ b/crates/ide/src/highlight_related.rs @@ -446,6 +446,18 @@ pub(crate) fn highlight_break_points( push_to_highlights(file_id, text_range); }); + if matches!(expr, ast::Expr::BlockExpr(_)) { + for_each_tail_expr(&expr, &mut |tail| { + if matches!(tail, ast::Expr::BreakExpr(_)) { + return; + } + + let file_id = sema.hir_file_for(tail.syntax()); + let range = tail.syntax().text_range(); + push_to_highlights(file_id, Some(range)); + }); + } + Some(highlights) } @@ -2072,4 +2084,21 @@ pub unsafe fn bootstrap() -> ! { "#, ) } + + #[test] + fn labeled_block_tail_expr() { + check( + r#" +fn foo() { + 'a: { + // ^^^ + if true { break$0 'a 0; } + // ^^^^^^^^ + 5 + // ^ + } +} +"#, + ); + } } From cfc28f3148accc0da42323f49a6d6895ddbbc3a2 Mon Sep 17 00:00:00 2001 From: roifewu Date: Tue, 15 Apr 2025 12:59:22 +0800 Subject: [PATCH 2/2] feat: highlight tail expr when cursor is on label --- crates/ide/src/highlight_related.rs | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/crates/ide/src/highlight_related.rs b/crates/ide/src/highlight_related.rs index 8f84f35bb453..bb96c925190b 100644 --- a/crates/ide/src/highlight_related.rs +++ b/crates/ide/src/highlight_related.rs @@ -232,6 +232,23 @@ fn highlight_references( } } + // highlight the tail expr of the labelled block + if matches!(def, Definition::Label(_)) { + let label = token.parent_ancestors().nth(1).and_then(ast::Label::cast); + if let Some(block) = + label.and_then(|label| label.syntax().parent()).and_then(ast::BlockExpr::cast) + { + for_each_tail_expr(&block.into(), &mut |tail| { + if !matches!(tail, ast::Expr::BreakExpr(_)) { + res.insert(HighlightedRange { + range: tail.syntax().text_range(), + category: ReferenceCategory::empty(), + }); + } + }); + } + } + // highlight the defs themselves match def { Definition::Local(local) => { @@ -2098,6 +2115,26 @@ fn foo() { // ^ } } +"#, + ); + } + + #[test] + fn labeled_block_tail_expr_2() { + check( + r#" +fn foo() { + let _ = 'b$0lk: { + // ^^^^ + let x = 1; + if true { break 'blk 42; } + // ^^^^ + if false { break 'blk 24; } + // ^^^^ + 100 + // ^^^ + }; +} "#, ); }