From 722020e63f3d28a90f1a39d9457e2aee23f9769a Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Thu, 1 Aug 2024 22:06:15 +0800 Subject: [PATCH 1/3] feat: support inlay hint for more expr with label --- crates/ide/src/inlay_hints/closing_brace.rs | 27 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/ide/src/inlay_hints/closing_brace.rs b/crates/ide/src/inlay_hints/closing_brace.rs index ea96c9504e55..318fe1c93967 100644 --- a/crates/ide/src/inlay_hints/closing_brace.rs +++ b/crates/ide/src/inlay_hints/closing_brace.rs @@ -7,7 +7,7 @@ use hir::{HirDisplay, Semantics}; use ide_db::{FileRange, RootDatabase}; use span::EditionedFileId; use syntax::{ - ast::{self, AstNode, HasName}, + ast::{self, AstNode, HasLoopBody, HasName}, match_ast, SyntaxKind, SyntaxNode, T, }; @@ -57,9 +57,30 @@ pub(super) fn hints( // the actual number of lines in this case should be the line count of the parent BlockExpr, // which the `min_lines` config cares about node = node.parent()?; - let block = label.syntax().parent().and_then(ast::BlockExpr::cast)?; - closing_token = block.stmt_list()?.r_curly_token()?; + + let parent = label.syntax().parent()?; + let block; + match_ast! { + match parent { + ast::BlockExpr(block_expr) => { + block = block_expr.stmt_list()?; + }, + ast::LoopExpr(loop_expr) => { + block = loop_expr.loop_body()?.stmt_list()?; + }, + ast::WhileExpr(while_expr) => { + block = while_expr.loop_body()?.stmt_list()?; + }, + ast::ForExpr(for_expr) => { + block = for_expr.loop_body()?.stmt_list()?; + }, + _ => return None, + } + } + closing_token = block.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 747615d95bcb674b05afc4b62ec56b8f584780dc Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Sun, 4 Aug 2024 19:19:15 +0800 Subject: [PATCH 2/3] test: add test case for inlay hint support for expr with label --- crates/ide/src/inlay_hints/closing_brace.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/ide/src/inlay_hints/closing_brace.rs b/crates/ide/src/inlay_hints/closing_brace.rs index 318fe1c93967..9b7e708848b8 100644 --- a/crates/ide/src/inlay_hints/closing_brace.rs +++ b/crates/ide/src/inlay_hints/closing_brace.rs @@ -240,6 +240,19 @@ fn test() { //^ 'do_a } //^ 'end + + 'a: loop { + 'b: for i in 0..5 { + 'c: while true { + + + } + //^ 'c + } + //^ 'b + } + //^ 'a + } //^ fn test "#, From c9e1cb4887457c0a841e8911d8f5ad47f7b0f2d8 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 5 Aug 2024 13:58:01 +0200 Subject: [PATCH 3/3] Simplify --- crates/ide/src/inlay_hints/closing_brace.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/crates/ide/src/inlay_hints/closing_brace.rs b/crates/ide/src/inlay_hints/closing_brace.rs index 9b7e708848b8..8f2777f3928d 100644 --- a/crates/ide/src/inlay_hints/closing_brace.rs +++ b/crates/ide/src/inlay_hints/closing_brace.rs @@ -65,15 +65,9 @@ pub(super) fn hints( ast::BlockExpr(block_expr) => { block = block_expr.stmt_list()?; }, - ast::LoopExpr(loop_expr) => { + ast::AnyHasLoopBody(loop_expr) => { block = loop_expr.loop_body()?.stmt_list()?; }, - ast::WhileExpr(while_expr) => { - block = while_expr.loop_body()?.stmt_list()?; - }, - ast::ForExpr(for_expr) => { - block = for_expr.loop_body()?.stmt_list()?; - }, _ => return None, } }