@@ -532,7 +532,7 @@ object Parsers:
532
532
def inBraces [T ](body : => T ): T = enclosed(LBRACE , body)
533
533
def inBrackets [T ](body : => T ): T = enclosed(LBRACKET , body)
534
534
535
- def inBracesOrIndented [T ](body : => T , inStatSeq : Boolean = false , rewriteWithColon : Boolean = false ): T =
535
+ def inBracesOrIndented [T ](body : => T , rewriteWithColon : Boolean = false ): T =
536
536
if in.token == INDENT then
537
537
// braces are always optional after `=>` so none should be inserted
538
538
val afterArrow = testChars(in.lastOffset - 3 , " =>" )
@@ -542,11 +542,11 @@ object Parsers:
542
542
else if rewriteToIndent then enclosed(INDENT , toIndentedRegion(body))
543
543
else enclosed(INDENT , body)
544
544
else
545
- if in.rewriteToIndent then bracesToIndented(body, inStatSeq, rewriteWithColon)
545
+ if in.rewriteToIndent then bracesToIndented(body, rewriteWithColon)
546
546
else inBraces(body)
547
547
548
- def inDefScopeBraces [T ](body : => T , inStatSeq : Boolean = false , rewriteWithColon : Boolean = false ): T =
549
- inBracesOrIndented(body, inStatSeq, rewriteWithColon)
548
+ def inDefScopeBraces [T ](body : => T , rewriteWithColon : Boolean = false ): T =
549
+ inBracesOrIndented(body, rewriteWithColon)
550
550
551
551
/** <part> {`,` <part>} */
552
552
def commaSeparated [T ](part : () => T ): List [T ] =
@@ -717,6 +717,9 @@ object Parsers:
717
717
def blankLinesAround (start : Offset , end : Offset ): (Offset , Offset ) =
718
718
(skipBlanks(start - 1 , - 1 ) + 1 , skipBlanks(end, 1 ))
719
719
720
+ private val bracesToIndentPredecessors =
721
+ colonEOLPredecessors | canStartIndentTokens | BitSet (IDENTIFIER )
722
+
720
723
/** Parse brace-enclosed `body` and rewrite it to be an indentation region instead, if possible.
721
724
* If possible means:
722
725
* 1. not inside (...), [...], case ... =>
@@ -725,31 +728,23 @@ object Parsers:
725
728
* 4. there is at least one token between the braces
726
729
* 5. the closing brace is also at the end of the line, or it is followed by one of
727
730
* `then`, `else`, `do`, `catch`, `finally`, `yield`, or `match`.
728
- * 6. the opening brace does not follow a closing brace
731
+ * 6. the opening brace follows an colonEOLPredecessors, a canStartIndentTokens or an identifier
729
732
* 7. last token is not a leading operator
730
- * 8. not a block in a sequence of statements
731
- * 9. cannot rewrite if colon required after a NEWLINE, e.g.
732
- * true ||
733
- * {
734
- * false
735
- * }
736
- */
737
- def bracesToIndented [T ](body : => T , inStatSeq : Boolean , rewriteWithColon : Boolean ): T =
733
+ */
734
+ def bracesToIndented [T ](body : => T , rewriteWithColon : Boolean ): T =
738
735
import IndentRewriteState .*
739
736
val prevSaved = prev.saveCopy
740
737
val lastOffsetSaved = in.lastOffset
741
738
val underColonSyntax = possibleColonOffset == in.lastOffset
742
739
val colonRequired = rewriteWithColon || underColonSyntax
743
740
val (startOpening, endOpening) = elimRegion(in.offset)
744
741
def isBracesOrIndented (r : Region ): Boolean = r match
745
- case r : Indented => true
746
- case r : InBraces => true
742
+ case r : (Indented | InBraces ) => true
747
743
case _ => false
748
- var canRewrite = isBracesOrIndented(in.currentRegion) && // test (1)
749
- prevSaved.token != RBRACE && // test (6)
750
- ! (prevSaved.isOperator && prevSaved.isAfterLineEnd) && // test (7)
751
- ! inStatSeq && // test (8)
752
- (! colonRequired || ! in.isAfterLineEnd) // test (9)
744
+ var canRewrite =
745
+ isBracesOrIndented(in.currentRegion) // test (1)
746
+ && bracesToIndentPredecessors.contains(prevSaved.token) // test (6)
747
+ && ! (prevSaved.isOperator && prevSaved.isAfterLineEnd) // test (7)
753
748
val t = enclosed(LBRACE , {
754
749
if in.isAfterLineEnd && in.token != RBRACE then // test (2)(4)
755
750
toIndentedRegion :
@@ -2073,7 +2068,7 @@ object Parsers:
2073
2068
2074
2069
def subExpr () = subPart(expr)
2075
2070
2076
- def expr (location : Location , inStatSeq : Boolean = false ): Tree = {
2071
+ def expr (location : Location ): Tree = {
2077
2072
val start = in.offset
2078
2073
in.token match
2079
2074
case IMPLICIT =>
@@ -2100,7 +2095,7 @@ object Parsers:
2100
2095
else new WildcardFunction (placeholderParams.reverse, t)
2101
2096
finally placeholderParams = saved
2102
2097
2103
- val t = expr1(location, inStatSeq )
2098
+ val t = expr1(location)
2104
2099
if in.isArrow then
2105
2100
placeholderParams = Nil // don't interpret `_' to the left of `=>` as placeholder
2106
2101
wrapPlaceholders(closureRest(start, location, convertToParams(t)))
@@ -2111,7 +2106,7 @@ object Parsers:
2111
2106
checkNonParamTuple(t)
2112
2107
wrapPlaceholders(t)
2113
2108
2114
- def expr1 (location : Location = Location .ElseWhere , inStatSeq : Boolean = false ): Tree = in.token match
2109
+ def expr1 (location : Location = Location .ElseWhere ): Tree = in.token match
2115
2110
case IF =>
2116
2111
ifExpr(in.offset, If )
2117
2112
case WHILE =>
@@ -2201,7 +2196,7 @@ object Parsers:
2201
2196
case t =>
2202
2197
syntaxError(em " `inline` must be followed by an `if` or a `match` " , start)
2203
2198
t
2204
- else expr1Rest(postfixExpr(location, inStatSeq ), location)
2199
+ else expr1Rest(postfixExpr(location), location)
2205
2200
end expr1
2206
2201
2207
2202
def expr1Rest (t : Tree , location : Location ): Tree =
@@ -2346,8 +2341,8 @@ object Parsers:
2346
2341
* | InfixExpr id ColonArgument
2347
2342
* | InfixExpr MatchClause
2348
2343
*/
2349
- def postfixExpr (location : Location = Location .ElseWhere , inStatSeq : Boolean = false ): Tree =
2350
- val t = postfixExprRest(prefixExpr(location, inStatSeq ), location)
2344
+ def postfixExpr (location : Location = Location .ElseWhere ): Tree =
2345
+ val t = postfixExprRest(prefixExpr(location), location)
2351
2346
if location.inArgs && followingIsVararg() then
2352
2347
Typed (t, atSpan(skipToken()) { Ident (tpnme.WILDCARD_STAR ) })
2353
2348
else
@@ -2360,7 +2355,7 @@ object Parsers:
2360
2355
/** PrefixExpr ::= [PrefixOperator'] SimpleExpr
2361
2356
* PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ (if not backquoted)
2362
2357
*/
2363
- def prefixExpr ( location : Location , inStatSeq : Boolean = false ) : Tree =
2358
+ val prefixExpr : Location => Tree = location =>
2364
2359
if in.token == IDENTIFIER && nme.raw.isUnary(in.name)
2365
2360
&& in.canStartExprTokens.contains(in.lookahead.token)
2366
2361
then
@@ -2370,7 +2365,7 @@ object Parsers:
2370
2365
simpleExprRest(literal(start), location, canApply = true )
2371
2366
else
2372
2367
atSpan(start) { PrefixOp (op, simpleExpr(location)) }
2373
- else simpleExpr(location, inStatSeq )
2368
+ else simpleExpr(location)
2374
2369
2375
2370
/** SimpleExpr ::= ‘new’ ConstrApp {`with` ConstrApp} [TemplateBody]
2376
2371
* | ‘new’ TemplateBody
@@ -2396,7 +2391,7 @@ object Parsers:
2396
2391
* Quoted ::= ‘'’ ‘{’ Block ‘}’
2397
2392
* | ‘'’ ‘[’ Type ‘]’
2398
2393
*/
2399
- def simpleExpr (location : Location , inStatSeq : Boolean = false ): Tree = {
2394
+ def simpleExpr (location : Location ): Tree = {
2400
2395
var canApply = true
2401
2396
val t = in.token match
2402
2397
case XMLSTART =>
@@ -2417,7 +2412,7 @@ object Parsers:
2417
2412
atSpan(in.offset) { makeTupleOrParens(inParens(exprsInParensOrBindings())) }
2418
2413
case LBRACE | INDENT =>
2419
2414
canApply = false
2420
- blockExpr(inStatSeq )
2415
+ blockExpr()
2421
2416
case QUOTE =>
2422
2417
atSpan(skipToken()) {
2423
2418
withinStaged(StageKind .Quoted | (if (location.inPattern) StageKind .QuotedPattern else 0 )) {
@@ -2568,12 +2563,12 @@ object Parsers:
2568
2563
2569
2564
/** BlockExpr ::= <<< (CaseClauses | Block) >>>
2570
2565
*/
2571
- def blockExpr (inStatSeq : Boolean = false ): Tree = atSpan(in.offset) {
2566
+ def blockExpr (): Tree = atSpan(in.offset) {
2572
2567
val simplify = in.token == INDENT
2573
2568
inDefScopeBraces({
2574
2569
if (in.token == CASE ) Match (EmptyTree , caseClauses(() => caseClause()))
2575
2570
else block(simplify)
2576
- }, inStatSeq = inStatSeq )
2571
+ })
2577
2572
}
2578
2573
2579
2574
/** Block ::= BlockStatSeq
@@ -3938,7 +3933,7 @@ object Parsers:
3938
3933
Template (constr, parents, derived, self, stats)
3939
3934
3940
3935
def templateBody (parents : List [Tree ], rewriteWithColon : Boolean = true ): (ValDef , List [Tree ]) =
3941
- val r = inDefScopeBraces(templateStatSeq(), rewriteWithColon = rewriteWithColon )
3936
+ val r = inDefScopeBraces(templateStatSeq(), rewriteWithColon)
3942
3937
if in.token == WITH && parents.isEmpty then
3943
3938
syntaxError(EarlyDefinitionsNotSupported ())
3944
3939
nextToken()
@@ -4055,7 +4050,7 @@ object Parsers:
4055
4050
else if (isDefIntro(modifierTokensOrCase))
4056
4051
stats +++= defOrDcl(in.offset, defAnnotsMods(modifierTokens))
4057
4052
else if (isExprIntro)
4058
- stats += expr1(inStatSeq = true )
4053
+ stats += expr1()
4059
4054
else
4060
4055
empty = true
4061
4056
statSepOrEnd(stats, noPrevStat = empty)
@@ -4129,7 +4124,7 @@ object Parsers:
4129
4124
if (in.token == IMPORT )
4130
4125
stats ++= importClause()
4131
4126
else if (isExprIntro)
4132
- stats += expr(Location .InBlock , inStatSeq = true )
4127
+ stats += expr(Location .InBlock )
4133
4128
else if in.token == IMPLICIT && ! in.inModifierPosition() then
4134
4129
stats += closure(in.offset, Location .InBlock , modifiers(BitSet (IMPLICIT )))
4135
4130
else if isIdent(nme.extension) && followingIsExtension() then
@@ -4190,7 +4185,7 @@ object Parsers:
4190
4185
def skipBracesHook (): Option [Tree ] =
4191
4186
if (in.token == XMLSTART ) Some (xmlLiteral()) else None
4192
4187
4193
- override def blockExpr (inStatSeq : Boolean ): Tree = {
4188
+ override def blockExpr (): Tree = {
4194
4189
skipBraces()
4195
4190
EmptyTree
4196
4191
0 commit comments