about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-03-18 08:22:26 +0000
committerbors <bors@rust-lang.org>2024-03-18 08:22:26 +0000
commitf07489ada9920c83a16a71667d4792287e137205 (patch)
treed8289bed66d9ea3443fd7c1c6f576b0d2b390b90
parent65c601fa422f92ae1932d70130a9b89d9fb6869d (diff)
parent109344cfb756cabbafccd201b0e658e6a7287f05 (diff)
downloadrust-f07489ada9920c83a16a71667d4792287e137205.tar.gz
rust-f07489ada9920c83a16a71667d4792287e137205.zip
Auto merge of #16868 - roife:fix-issue-16848, r=Veykril
fix: handle attributes when typing curly bracket

fix #16848.

When inserting a `{`, if it is identified that the front part of `expr` is `attr`, we consider it as inserting `{}` around the entire `expr` (excluding the attr part).
-rw-r--r--crates/ide/src/typing.rs33
1 files 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 on_opening_bracket_typed(
             }
         }
 
-        // 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.
@@ -827,6 +839,21 @@ fn f() {
 }
             "#,
         );
+        type_char(
+            '{',
+            r#"
+fn main() {
+    #[allow(unreachable_code)]
+    $0g();
+}
+            "#,
+            r#"
+fn main() {
+    #[allow(unreachable_code)]
+    {g()};
+}
+            "#,
+        );
     }
 
     #[test]