From 109344cfb756cabbafccd201b0e658e6a7287f05 Mon Sep 17 00:00:00 2001 From: roife Date: Mon, 18 Mar 2024 13:19:24 +0800 Subject: [PATCH] fix: handle attributes when typing curly bracket --- crates/ide/src/typing.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/ide/src/typing.rs b/crates/ide/src/typing.rs index e87fc89fea2..d3eee0e02e4 100644 --- a/crates/ide/src/typing.rs +++ b/crates/ide/src/typing.rs @@ -175,9 +175,21 @@ fn bracket_expr( } } - // If it's a statement in a block, we don't know how many statements should be included - if ast::ExprStmt::can_cast(expr.syntax().parent()?.kind()) { - return None; + if let Some(parent) = expr.syntax().parent().and_then(ast::Expr::cast) { + let mut node = expr.syntax().clone(); + let all_prev_sib_attr = loop { + match node.prev_sibling() { + Some(sib) if sib.kind().is_trivia() || sib.kind() == SyntaxKind::ATTR => { + node = sib + } + Some(_) => break false, + None => break true, + }; + }; + + if all_prev_sib_attr { + expr = parent; + } } // Insert the closing bracket right after the expression. @@ -824,6 +836,21 @@ fn f() { 0 => {()}, 1 => (), } +} + "#, + ); + type_char( + '{', + r#" +fn main() { + #[allow(unreachable_code)] + $0g(); +} + "#, + r#" +fn main() { + #[allow(unreachable_code)] + {g()}; } "#, );