Skip to content

Commit f07489a

Browse files
committed
Auto merge of rust-lang#16868 - roife:fix-issue-16848, r=Veykril
fix: handle attributes when typing curly bracket fix rust-lang#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).
2 parents 65c601f + 109344c commit f07489a

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

crates/ide/src/typing.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,21 @@ fn on_opening_bracket_typed(
175175
}
176176
}
177177

178-
// If it's a statement in a block, we don't know how many statements should be included
179-
if ast::ExprStmt::can_cast(expr.syntax().parent()?.kind()) {
180-
return None;
178+
if let Some(parent) = expr.syntax().parent().and_then(ast::Expr::cast) {
179+
let mut node = expr.syntax().clone();
180+
let all_prev_sib_attr = loop {
181+
match node.prev_sibling() {
182+
Some(sib) if sib.kind().is_trivia() || sib.kind() == SyntaxKind::ATTR => {
183+
node = sib
184+
}
185+
Some(_) => break false,
186+
None => break true,
187+
};
188+
};
189+
190+
if all_prev_sib_attr {
191+
expr = parent;
192+
}
181193
}
182194

183195
// Insert the closing bracket right after the expression.
@@ -824,6 +836,21 @@ fn f() {
824836
0 => {()},
825837
1 => (),
826838
}
839+
}
840+
"#,
841+
);
842+
type_char(
843+
'{',
844+
r#"
845+
fn main() {
846+
#[allow(unreachable_code)]
847+
$0g();
848+
}
849+
"#,
850+
r#"
851+
fn main() {
852+
#[allow(unreachable_code)]
853+
{g()};
827854
}
828855
"#,
829856
);

0 commit comments

Comments
 (0)