@@ -653,14 +653,16 @@ object Parsers {
653
653
lineStart
654
654
}
655
655
656
- val needsBraces = t match {
657
- case Block (Nil , expr) =>
656
+ def needsBraces (t : Any ): Boolean = t match {
657
+ case Block (stats, expr) =>
658
+ stats.nonEmpty || needsBraces(expr)
659
+ case expr : Tree =>
658
660
followsColon ||
659
661
isPartialIf(expr) && in.token == ELSE ||
660
662
isBlockFunction(expr)
661
663
case _ => true
662
664
}
663
- if (needsBraces) {
665
+ if (needsBraces(t) ) {
664
666
patch(source, Span (startOpening, endOpening), " {" )
665
667
patch(source, Span (closingOffset(source.nextLine(in.lastOffset))), indentWidth.toPrefix ++ " }\n " )
666
668
}
@@ -1361,11 +1363,7 @@ object Parsers {
1361
1363
else t
1362
1364
1363
1365
/** The block in a quote or splice */
1364
- def stagedBlock () =
1365
- inDefScopeBraces(block()) match {
1366
- case t @ Block (Nil , expr) if ! expr.isEmpty => expr
1367
- case t => t
1368
- }
1366
+ def stagedBlock () = inDefScopeBraces(block(simplify = true ))
1369
1367
1370
1368
/** SimpleEpxr ::= spliceId | ‘$’ ‘{’ Block ‘}’)
1371
1369
* SimpleType ::= spliceId | ‘$’ ‘{’ Block ‘}’)
@@ -2148,27 +2146,26 @@ object Parsers {
2148
2146
* BlockExprContents ::= CaseClauses | Block
2149
2147
*/
2150
2148
def blockExpr (): Tree = atSpan(in.offset) {
2149
+ val simplify = in.token == INDENT
2151
2150
inDefScopeBraces {
2152
2151
if (in.token == CASE ) Match (EmptyTree , caseClauses(caseClause))
2153
- else block()
2152
+ else block(simplify )
2154
2153
}
2155
2154
}
2156
2155
2157
2156
/** Block ::= BlockStatSeq
2158
2157
* @note Return tree does not have a defined span.
2159
2158
*/
2160
- def block (): Tree = {
2159
+ def block (simplify : Boolean = false ): Tree = {
2161
2160
val stats = blockStatSeq()
2162
2161
def isExpr (stat : Tree ) = ! (stat.isDef || stat.isInstanceOf [Import ])
2163
- stats match {
2164
- case (stat : Block ) :: Nil =>
2165
- stat // A typical case where this happens is creating a block around a region
2166
- // hat is already indented, e.g. something following a =>.
2167
- case _ :: stats1 if isExpr(stats.last) =>
2168
- Block (stats.init, stats.last)
2169
- case _ =>
2170
- Block (stats, EmptyTree )
2162
+ if (stats.nonEmpty && isExpr(stats.last)) {
2163
+ val inits = stats.init
2164
+ val last = stats.last
2165
+ if (inits.isEmpty && (simplify || last.isInstanceOf [Block ])) last
2166
+ else Block (inits, last)
2171
2167
}
2168
+ else Block (stats, EmptyTree )
2172
2169
}
2173
2170
2174
2171
/** Guard ::= if PostfixExpr
0 commit comments