@@ -559,7 +559,7 @@ object Parsers {
559
559
def inBraces [T ](body : => T ): T = enclosed(LBRACE , body)
560
560
def inBrackets [T ](body : => T ): T = enclosed(LBRACKET , body)
561
561
562
- def inBracesOrIndented [T ](body : => T , inStatSeq : Boolean = false , rewriteWithColon : Boolean = false ): T =
562
+ def inBracesOrIndented [T ](body : => T , rewriteWithColon : Boolean = false ): T =
563
563
if in.token == INDENT then
564
564
// braces are always optional after `=>` so none should be inserted
565
565
val afterArrow = testChars(in.lastOffset - 3 , " =>" )
@@ -569,11 +569,11 @@ object Parsers {
569
569
else if rewriteToIndent then enclosed(INDENT , toIndentedRegion(body))
570
570
else enclosed(INDENT , body)
571
571
else
572
- if in.rewriteToIndent then bracesToIndented(body, inStatSeq, rewriteWithColon)
572
+ if in.rewriteToIndent then bracesToIndented(body, rewriteWithColon)
573
573
else inBraces(body)
574
574
575
- def inDefScopeBraces [T ](body : => T , inStatSeq : Boolean = false , rewriteWithColon : Boolean = false ): T =
576
- inBracesOrIndented(body, inStatSeq, rewriteWithColon)
575
+ def inDefScopeBraces [T ](body : => T , rewriteWithColon : Boolean = false ): T =
576
+ inBracesOrIndented(body, rewriteWithColon)
577
577
578
578
/** <part> {`,` <part>} */
579
579
def commaSeparated [T ](part : () => T ): List [T ] =
@@ -748,6 +748,9 @@ object Parsers {
748
748
def blankLinesAround (start : Offset , end : Offset ): (Offset , Offset ) =
749
749
(skipBlanks(start - 1 , - 1 ) + 1 , skipBlanks(end, 1 ))
750
750
751
+ private val bracesToIndentPredecessors =
752
+ colonEOLPredecessors | canStartIndentTokens | BitSet (IDENTIFIER )
753
+
751
754
/** Parse brace-enclosed `body` and rewrite it to be an indentation region instead, if possible.
752
755
* If possible means:
753
756
* 1. not inside (...), [...], case ... =>
@@ -756,31 +759,23 @@ object Parsers {
756
759
* 4. there is at least one token between the braces
757
760
* 5. the closing brace is also at the end of the line, or it is followed by one of
758
761
* `then`, `else`, `do`, `catch`, `finally`, `yield`, or `match`.
759
- * 6. the opening brace does not follow a closing brace
762
+ * 6. the opening brace follows an colonEOLPredecessors, a canStartIndentTokens or an identifier
760
763
* 7. last token is not a leading operator
761
- * 8. not a block in a sequence of statements
762
- * 9. cannot rewrite if colon required after a NEWLINE, e.g.
763
- * true ||
764
- * {
765
- * false
766
- * }
767
- */
768
- def bracesToIndented [T ](body : => T , inStatSeq : Boolean , rewriteWithColon : Boolean ): T =
764
+ */
765
+ def bracesToIndented [T ](body : => T , rewriteWithColon : Boolean ): T =
769
766
import IndentRewriteState .*
770
767
val prevSaved = prev.saveCopy
771
768
val lastOffsetSaved = in.lastOffset
772
769
val underColonSyntax = possibleColonOffset == in.lastOffset
773
770
val colonRequired = rewriteWithColon || underColonSyntax
774
771
val (startOpening, endOpening) = elimRegion(in.offset)
775
772
def isBracesOrIndented (r : Region ): Boolean = r match
776
- case r : Indented => true
777
- case r : InBraces => true
773
+ case r : (Indented | InBraces ) => true
778
774
case _ => false
779
- var canRewrite = isBracesOrIndented(in.currentRegion) && // test (1)
780
- prevSaved.token != RBRACE && // test (6)
781
- ! (prevSaved.isOperator && prevSaved.isAfterLineEnd) && // test (7)
782
- ! inStatSeq && // test (8)
783
- (! colonRequired || ! in.isAfterLineEnd) // test (9)
775
+ var canRewrite =
776
+ isBracesOrIndented(in.currentRegion) // test (1)
777
+ && bracesToIndentPredecessors.contains(prevSaved.token) // test (6)
778
+ && ! (prevSaved.isOperator && prevSaved.isAfterLineEnd) // test (7)
784
779
val t = enclosed(LBRACE , {
785
780
if in.isAfterLineEnd && in.token != RBRACE then // test (2)(4)
786
781
toIndentedRegion :
@@ -2180,7 +2175,7 @@ object Parsers {
2180
2175
2181
2176
def subExpr () = subPart(expr)
2182
2177
2183
- def expr (location : Location , inStatSeq : Boolean = false ): Tree = {
2178
+ def expr (location : Location ): Tree = {
2184
2179
val start = in.offset
2185
2180
in.token match
2186
2181
case IMPLICIT =>
@@ -2208,7 +2203,7 @@ object Parsers {
2208
2203
else new WildcardFunction (placeholderParams.reverse, t)
2209
2204
finally placeholderParams = saved
2210
2205
2211
- val t = expr1(location, inStatSeq )
2206
+ val t = expr1(location)
2212
2207
if in.isArrow then
2213
2208
placeholderParams = Nil // don't interpret `_' to the left of `=>` as placeholder
2214
2209
wrapPlaceholders(closureRest(start, location, convertToParams(t)))
@@ -2220,7 +2215,7 @@ object Parsers {
2220
2215
wrapPlaceholders(t)
2221
2216
}
2222
2217
2223
- def expr1 (location : Location = Location .ElseWhere , inStatSeq : Boolean = false ): Tree = in.token match
2218
+ def expr1 (location : Location = Location .ElseWhere ): Tree = in.token match
2224
2219
case IF =>
2225
2220
ifExpr(in.offset, If )
2226
2221
case WHILE =>
@@ -2318,7 +2313,7 @@ object Parsers {
2318
2313
case t =>
2319
2314
syntaxError(em " `inline` must be followed by an `if` or a `match` " , start)
2320
2315
t
2321
- else expr1Rest(postfixExpr(location, inStatSeq ), location)
2316
+ else expr1Rest(postfixExpr(location), location)
2322
2317
end expr1
2323
2318
2324
2319
def expr1Rest (t : Tree , location : Location ): Tree =
@@ -2476,8 +2471,8 @@ object Parsers {
2476
2471
* | InfixExpr id ColonArgument
2477
2472
* | InfixExpr MatchClause
2478
2473
*/
2479
- def postfixExpr (location : Location = Location .ElseWhere , inStatSeq : Boolean = false ): Tree =
2480
- val t = postfixExprRest(prefixExpr(location, inStatSeq ), location)
2474
+ def postfixExpr (location : Location = Location .ElseWhere ): Tree =
2475
+ val t = postfixExprRest(prefixExpr(location), location)
2481
2476
if location.inArgs && followingIsVararg() then
2482
2477
Typed (t, atSpan(skipToken()) { Ident (tpnme.WILDCARD_STAR ) })
2483
2478
else
@@ -2490,7 +2485,7 @@ object Parsers {
2490
2485
/** PrefixExpr ::= [PrefixOperator'] SimpleExpr
2491
2486
* PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ (if not backquoted)
2492
2487
*/
2493
- def prefixExpr ( location : Location , inStatSeq : Boolean = false ) : Tree =
2488
+ val prefixExpr : Location => Tree = location =>
2494
2489
if in.token == IDENTIFIER && nme.raw.isUnary(in.name)
2495
2490
&& in.canStartExprTokens.contains(in.lookahead.token)
2496
2491
then
@@ -2500,7 +2495,7 @@ object Parsers {
2500
2495
simpleExprRest(literal(start), location, canApply = true )
2501
2496
else
2502
2497
atSpan(start) { PrefixOp (op, simpleExpr(location)) }
2503
- else simpleExpr(location, inStatSeq )
2498
+ else simpleExpr(location)
2504
2499
2505
2500
/** SimpleExpr ::= ‘new’ ConstrApp {`with` ConstrApp} [TemplateBody]
2506
2501
* | ‘new’ TemplateBody
@@ -2526,7 +2521,7 @@ object Parsers {
2526
2521
* Quoted ::= ‘'’ ‘{’ Block ‘}’
2527
2522
* | ‘'’ ‘[’ Type ‘]’
2528
2523
*/
2529
- def simpleExpr (location : Location , inStatSeq : Boolean = false ): Tree = {
2524
+ def simpleExpr (location : Location ): Tree = {
2530
2525
var canApply = true
2531
2526
val t = in.token match {
2532
2527
case XMLSTART =>
@@ -2547,7 +2542,7 @@ object Parsers {
2547
2542
atSpan(in.offset) { makeTupleOrParens(inParens(exprsInParensOrBindings())) }
2548
2543
case LBRACE | INDENT =>
2549
2544
canApply = false
2550
- blockExpr(inStatSeq )
2545
+ blockExpr()
2551
2546
case QUOTE =>
2552
2547
atSpan(skipToken()) {
2553
2548
withinStaged(StageKind .Quoted | (if (location.inPattern) StageKind .QuotedPattern else 0 )) {
@@ -2711,12 +2706,12 @@ object Parsers {
2711
2706
2712
2707
/** BlockExpr ::= <<< (CaseClauses | Block) >>>
2713
2708
*/
2714
- def blockExpr (inStatSeq : Boolean = false ): Tree = atSpan(in.offset) {
2709
+ def blockExpr (): Tree = atSpan(in.offset) {
2715
2710
val simplify = in.token == INDENT
2716
2711
inDefScopeBraces({
2717
2712
if (in.token == CASE ) Match (EmptyTree , caseClauses(() => caseClause()))
2718
2713
else block(simplify)
2719
- }, inStatSeq = inStatSeq )
2714
+ })
2720
2715
}
2721
2716
2722
2717
/** Block ::= BlockStatSeq
@@ -4177,7 +4172,7 @@ object Parsers {
4177
4172
Template (constr, parents, derived, self, stats)
4178
4173
4179
4174
def templateBody (parents : List [Tree ], rewriteWithColon : Boolean = true ): (ValDef , List [Tree ]) =
4180
- val r = inDefScopeBraces(templateStatSeq(), rewriteWithColon = rewriteWithColon )
4175
+ val r = inDefScopeBraces(templateStatSeq(), rewriteWithColon)
4181
4176
if in.token == WITH && parents.isEmpty then
4182
4177
syntaxError(EarlyDefinitionsNotSupported ())
4183
4178
nextToken()
@@ -4299,7 +4294,7 @@ object Parsers {
4299
4294
else if (isDefIntro(modifierTokensOrCase))
4300
4295
stats +++= defOrDcl(in.offset, defAnnotsMods(modifierTokens))
4301
4296
else if (isExprIntro)
4302
- stats += expr1(inStatSeq = true )
4297
+ stats += expr1()
4303
4298
else
4304
4299
empty = true
4305
4300
statSepOrEnd(stats, noPrevStat = empty)
@@ -4376,7 +4371,7 @@ object Parsers {
4376
4371
if (in.token == IMPORT )
4377
4372
stats ++= importClause()
4378
4373
else if (isExprIntro)
4379
- stats += expr(Location .InBlock , inStatSeq = true )
4374
+ stats += expr(Location .InBlock )
4380
4375
else if in.token == IMPLICIT && ! in.inModifierPosition() then
4381
4376
stats += closure(in.offset, Location .InBlock , modifiers(BitSet (IMPLICIT )))
4382
4377
else if isIdent(nme.extension) && followingIsExtension() then
@@ -4445,7 +4440,7 @@ object Parsers {
4445
4440
def skipBracesHook (): Option [Tree ] =
4446
4441
if (in.token == XMLSTART ) Some (xmlLiteral()) else None
4447
4442
4448
- override def blockExpr (inStatSeq : Boolean ): Tree = {
4443
+ override def blockExpr (): Tree = {
4449
4444
skipBraces()
4450
4445
EmptyTree
4451
4446
}
0 commit comments