Complete braces more aggressively

This commit is contained in:
Jonas Schievink 2021-04-07 01:24:24 +02:00
parent 36cd724b7b
commit 61e292fab1

View File

@ -86,26 +86,13 @@ fn on_opening_brace_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdi
// We expect a block expression enclosing exactly 1 preexisting expression. It can be parsed as // We expect a block expression enclosing exactly 1 preexisting expression. It can be parsed as
// either the trailing expr or an ExprStmt. // either the trailing expr or an ExprStmt.
let offset = { let offset = {
match block.tail_expr() { match block.statements().next() {
Some(expr) => { Some(ast::Stmt::ExprStmt(it)) => {
if block.statements().next().is_some() { // Use the expression span to place `}` before the `;`
return None; it.expr()?.syntax().text_range().end()
} },
expr.syntax().text_range().end() None => block.tail_expr()?.syntax().text_range().end(),
} _ => return None,
None => {
if block.statements().count() != 1 {
return None;
}
match block.statements().next()? {
ast::Stmt::ExprStmt(it) => {
// Use the expression span to place `}` before the `;`
it.expr()?.syntax().text_range().end()
}
_ => return None,
}
}
} }
}; };
@ -417,5 +404,33 @@ fn adds_closing_brace() {
type_char('{', r"fn f() { match () { _ => $0() } }", r"fn f() { match () { _ => {()} } }"); type_char('{', r"fn f() { match () { _ => $0() } }", r"fn f() { match () { _ => {()} } }");
type_char('{', r"fn f() { $0(); }", r"fn f() { {()}; }"); type_char('{', r"fn f() { $0(); }", r"fn f() { {()}; }");
type_char('{', r"fn f() { let x = $0(); }", r"fn f() { let x = {()}; }"); type_char('{', r"fn f() { let x = $0(); }", r"fn f() { let x = {()}; }");
type_char(
'{',
r"
const S: () = $0();
fn f() {}
",
r"
const S: () = {()};
fn f() {}
",
);
type_char(
'{',
r"
fn f() {
match x {
0 => $0(),
1 => (),
}
}",
r"
fn f() {
match x {
0 => {()},
1 => (),
}
}",
);
} }
} }