@@ -581,11 +581,6 @@ object Parsers {
581
581
*/
582
582
var possibleColonOffset : Int = - 1
583
583
584
- /** A list of pending patches, to be issued if we can rewrite all enclosing braces to
585
- * indentation regions.
586
- */
587
- var pendingPatches : List [() => Unit ] = Nil
588
-
589
584
def testChar (idx : Int , p : Char => Boolean ): Boolean = {
590
585
val txt = source.content
591
586
idx < txt.length && p(txt(idx))
@@ -606,7 +601,8 @@ object Parsers {
606
601
607
602
/** Parse indentation region `body` and rewrite it to be in braces instead */
608
603
def indentedToBraces [T ](body : => T ): T = {
609
- val indentWidth = in.indent.enclosing.width
604
+ val enclRegion = in.currentRegion.enclosing
605
+ def indentWidth = enclRegion.indentWidth
610
606
val followsColon = testChar(in.lastOffset - 1 , ':' )
611
607
val startOpening =
612
608
if (followsColon)
@@ -701,10 +697,12 @@ object Parsers {
701
697
def bracesToIndented [T ](body : => T ): T = {
702
698
val colonRequired = possibleColonOffset == in.lastOffset
703
699
val (startOpening, endOpening) = startingElimRegion(colonRequired)
704
- val isOuterMost = in.sepRegions.isEmpty
705
- val savedPending = pendingPatches
706
- var canRewrite =
707
- in.sepRegions.forall(token => token == RBRACE || token == OUTDENT ) && // test (1)
700
+ val isOutermost = in.currentRegion.isOutermost
701
+ def allBraces (r : Region ): Boolean = r match {
702
+ case r : InBraces => allBraces(r.enclosing)
703
+ case _ => r.isOutermost
704
+ }
705
+ var canRewrite = allBraces(in.currentRegion) && // test (1)
708
706
! testChars(in.lastOffset - 3 , " =>" ) // test(6)
709
707
val t = enclosed(LBRACE , {
710
708
canRewrite &= in.isAfterLineEnd // test (2)
@@ -721,17 +719,9 @@ object Parsers {
721
719
else if (testChar(startOpening - 1 , Chars .isOperatorPart(_))) " :"
722
720
else " :"
723
721
val (startClosing, endClosing) = closingElimRegion()
724
- val applyPatch = () => {
725
- patch(source, Span (startOpening, endOpening), openingPatchStr)
726
- patch(source, Span (startClosing, endClosing), " " )
727
- }
728
- pendingPatches = applyPatch :: pendingPatches
729
- if (isOuterMost) {
730
- pendingPatches.reverse.foreach(_())
731
- pendingPatches = Nil
732
- }
722
+ patch(source, Span (startOpening, endOpening), openingPatchStr)
723
+ patch(source, Span (startClosing, endClosing), " " )
733
724
}
734
- else pendingPatches = savedPending // can't rewrite, cancel all nested patches.
735
725
t
736
726
}
737
727
@@ -1168,7 +1158,7 @@ object Parsers {
1168
1158
}
1169
1159
1170
1160
def indentRegion [T ](tag : EndMarkerTag )(op : => T ): T = {
1171
- val iw = in.indent.width
1161
+ val iw = in.currentRegion.indentWidth
1172
1162
val t = op
1173
1163
in.consumeEndMarker(tag, iw)
1174
1164
t
@@ -1282,6 +1272,7 @@ object Parsers {
1282
1272
}
1283
1273
else { accept(TLARROW ); typ() }
1284
1274
}
1275
+ else if (in.token == INDENT ) enclosed(INDENT , typ())
1285
1276
else infixType()
1286
1277
1287
1278
in.token match {
@@ -2152,8 +2143,15 @@ object Parsers {
2152
2143
def block (): Tree = {
2153
2144
val stats = blockStatSeq()
2154
2145
def isExpr (stat : Tree ) = ! (stat.isDef || stat.isInstanceOf [Import ])
2155
- if (stats.nonEmpty && isExpr(stats.last)) Block (stats.init, stats.last)
2156
- else Block (stats, EmptyTree )
2146
+ stats match {
2147
+ case (stat : Block ) :: Nil =>
2148
+ stat // A typical case where this happens is creating a block around a region
2149
+ // hat is already indented, e.g. something following a =>.
2150
+ case _ :: stats1 if isExpr(stats.last) =>
2151
+ Block (stats.init, stats.last)
2152
+ case _ =>
2153
+ Block (stats, EmptyTree )
2154
+ }
2157
2155
}
2158
2156
2159
2157
/** Guard ::= if PostfixExpr
@@ -2192,7 +2190,7 @@ object Parsers {
2192
2190
/** Generator ::= [‘case’] Pattern `<-' Expr
2193
2191
*/
2194
2192
def generator (): Tree = {
2195
- val casePat = if (in.token == CASE ) { in.skipCASE (); true } else false
2193
+ val casePat = if (in.token == CASE ) { in.nextToken (); true } else false
2196
2194
generatorRest(pattern1(), casePat)
2197
2195
}
2198
2196
@@ -2302,15 +2300,21 @@ object Parsers {
2302
2300
* ImplicitCaseClause ::= ‘case’ PatVar [Ascription] [Guard] `=>' Block
2303
2301
*/
2304
2302
val caseClause : () => CaseDef = () => atSpan(in.offset) {
2305
- accept(CASE )
2306
- CaseDef (pattern(), guard(), atSpan(accept(ARROW )) { block() })
2303
+ val (pat, grd) = inSepRegion(LPAREN , RPAREN ) {
2304
+ accept(CASE )
2305
+ (pattern(), guard())
2306
+ }
2307
+ CaseDef (pat, grd, atSpan(accept(ARROW )) { block() })
2307
2308
}
2308
2309
2309
2310
/** TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]
2310
2311
*/
2311
2312
val typeCaseClause : () => CaseDef = () => atSpan(in.offset) {
2312
- accept(CASE )
2313
- CaseDef (infixType(), EmptyTree , atSpan(accept(ARROW )) {
2313
+ val pat = inSepRegion(LPAREN , RPAREN ) {
2314
+ accept(CASE )
2315
+ infixType()
2316
+ }
2317
+ CaseDef (pat, EmptyTree , atSpan(accept(ARROW )) {
2314
2318
val t = typ()
2315
2319
if (isStatSep) in.nextToken()
2316
2320
t
@@ -3246,7 +3250,7 @@ object Parsers {
3246
3250
*/
3247
3251
def enumCase (start : Offset , mods : Modifiers ): DefTree = {
3248
3252
val mods1 = mods | EnumCase
3249
- in.skipCASE( )
3253
+ accept( CASE )
3250
3254
3251
3255
atSpan(start, nameStart) {
3252
3256
val id = termIdent()
@@ -3349,9 +3353,8 @@ object Parsers {
3349
3353
case _ => false
3350
3354
}
3351
3355
3352
- def givenArgs (t : Tree ): Tree = {
3356
+ def givenArgs (t : Tree ): Tree =
3353
3357
if (in.token == GIVEN ) givenArgs(applyGiven(t, prefixExpr)) else t
3354
- }
3355
3358
3356
3359
if (in.token == LPAREN )
3357
3360
inParens {
0 commit comments