Skip to content

Commit

Permalink
Auto merge of #16868 - roife:fix-issue-16848, r=Veykril
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
bors committed Mar 18, 2024
2 parents 65c601f + 109344c commit f07489a
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions crates/ide/src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -824,6 +836,21 @@ fn f() {
0 => {()},
1 => (),
}
}
"#,
);
type_char(
'{',
r#"
fn main() {
#[allow(unreachable_code)]
$0g();
}
"#,
r#"
fn main() {
#[allow(unreachable_code)]
{g()};
}
"#,
);
Expand Down

0 comments on commit f07489a

Please sign in to comment.