From 61e292fab1a5c5f3c97ace967268b6197a687ae1 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 7 Apr 2021 01:24:24 +0200 Subject: Complete braces more aggressively --- crates/ide/src/typing.rs | 55 ++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/crates/ide/src/typing.rs b/crates/ide/src/typing.rs index de65632e3..b0234d7fd 100644 --- a/crates/ide/src/typing.rs +++ b/crates/ide/src/typing.rs @@ -86,26 +86,13 @@ fn on_opening_brace_typed(file: &SourceFile, offset: TextSize) -> Option { - if block.statements().next().is_some() { - return None; - } - expr.syntax().text_range().end() - } - 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, - } - } + match block.statements().next() { + Some(ast::Stmt::ExprStmt(it)) => { + // Use the expression span to place `}` before the `;` + it.expr()?.syntax().text_range().end() + }, + None => block.tail_expr()?.syntax().text_range().end(), + _ => return None, } }; @@ -417,5 +404,33 @@ fn main() { 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() { 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 => (), + } + }", + ); } } -- cgit v1.2.3