From 0416dec50a8783a1b5c5321e4fa6a464d07d5a65 Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Tue, 16 Jul 2024 22:16:25 +0800 Subject: [PATCH 1/3] feat: add inlay hint support for block expr with lifetime label --- crates/ide/src/inlay_hints/closing_brace.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/ide/src/inlay_hints/closing_brace.rs b/crates/ide/src/inlay_hints/closing_brace.rs index d8aa4ba4e1..de11ca4f69 100644 --- a/crates/ide/src/inlay_hints/closing_brace.rs +++ b/crates/ide/src/inlay_hints/closing_brace.rs @@ -18,7 +18,7 @@ pub(super) fn hints( sema: &Semantics<'_, RootDatabase>, config: &InlayHintsConfig, file_id: EditionedFileId, - node: SyntaxNode, + mut node: SyntaxNode, ) -> Option<()> { let min_lines = config.closing_brace_hints_min_lines?; @@ -52,6 +52,13 @@ pub(super) fn hints( let module = ast::Module::cast(list.syntax().parent()?)?; (format!("mod {}", module.name()?), module.name().map(name)) + } else if let Some(label) = ast::Label::cast(node.clone()) { + // in this case, `ast::Label` could be seen as a part of `ast::BlockExpr`, to respect the `min_lines` config + node = node.parent()?; + let block = label.syntax().parent().and_then(ast::BlockExpr::cast)?; + closing_token = block.stmt_list()?.r_curly_token()?; + let lifetime = label.lifetime().map_or_else(String::new, |it| it.to_string()); + (lifetime, Some(label.syntax().text_range())) } else if let Some(block) = ast::BlockExpr::cast(node.clone()) { closing_token = block.stmt_list()?.r_curly_token()?; From a2d142b9f2ff92663b78d549e8fa567a4752279a Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Fri, 19 Jul 2024 22:31:28 +0800 Subject: [PATCH 2/3] internal: add test case for inlay hint support for block expr with lifetime label --- crates/ide/src/inlay_hints/closing_brace.rs | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/crates/ide/src/inlay_hints/closing_brace.rs b/crates/ide/src/inlay_hints/closing_brace.rs index de11ca4f69..89654e4048 100644 --- a/crates/ide/src/inlay_hints/closing_brace.rs +++ b/crates/ide/src/inlay_hints/closing_brace.rs @@ -196,6 +196,29 @@ fn f() { ]; } //^ fn f +"#, + ); + } + + #[test] + fn hints_closing_brace_for_block_expr() { + check_with_config( + InlayHintsConfig { closing_brace_hints_min_lines: Some(2), ..DISABLED_CONFIG }, + r#" +fn test() { + 'end: { + 'do_a: { + 'do_b: { + + } + //^ 'do_b + break 'end; + } + //^ 'do_a + } + //^ 'end + } +//^ fn test "#, ); } From 18e7299997d05e23e08b0267ab2a3117d0e1cd77 Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Fri, 19 Jul 2024 23:01:52 +0800 Subject: [PATCH 3/3] minor: tweak comment --- crates/ide/src/inlay_hints/closing_brace.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/ide/src/inlay_hints/closing_brace.rs b/crates/ide/src/inlay_hints/closing_brace.rs index 89654e4048..3e255adbe6 100644 --- a/crates/ide/src/inlay_hints/closing_brace.rs +++ b/crates/ide/src/inlay_hints/closing_brace.rs @@ -53,7 +53,8 @@ pub(super) fn hints( let module = ast::Module::cast(list.syntax().parent()?)?; (format!("mod {}", module.name()?), module.name().map(name)) } else if let Some(label) = ast::Label::cast(node.clone()) { - // in this case, `ast::Label` could be seen as a part of `ast::BlockExpr`, to respect the `min_lines` config + // in this case, `ast::Label` could be seen as a part of `ast::BlockExpr` + // the actual number of lines in this case should be the line count of the parent BlockExpr, which the `min_lines` config care about node = node.parent()?; let block = label.syntax().parent().and_then(ast::BlockExpr::cast)?; closing_token = block.stmt_list()?.r_curly_token()?;