@@ -330,7 +330,7 @@ object Parsers {
330
330
else if in.token == END then
331
331
if endSeen then syntaxError(" duplicate end marker" )
332
332
checkEndMarker(stats)
333
- recur(sepSeen, true )
333
+ recur(sepSeen, endSeen = true )
334
334
else if isStatSeqEnd || in.token == altEnd then
335
335
false
336
336
else if sepSeen || endSeen then
@@ -537,15 +537,13 @@ object Parsers {
537
537
def inBrackets [T ](body : => T ): T = enclosed(LBRACKET , body)
538
538
539
539
def inBracesOrIndented [T ](body : => T , rewriteWithColon : Boolean = false ): T =
540
- if (in.token == INDENT ) {
541
- val rewriteToBraces =
542
- in.rewriteNoIndent &&
543
- ! testChars(in.lastOffset - 3 , " =>" ) // braces are always optional after `=>` so none should be inserted
544
- if (rewriteToBraces) indentedToBraces(body)
540
+ if in.token == INDENT then
541
+ val rewriteToBraces = in.rewriteNoIndent
542
+ && ! testChars(in.lastOffset - 3 , " =>" ) // braces are always optional after `=>` so none should be inserted
543
+ if rewriteToBraces then indentedToBraces(body)
545
544
else enclosed(INDENT , body)
546
- }
547
545
else
548
- if ( in.rewriteToIndent) bracesToIndented(body, rewriteWithColon)
546
+ if in.rewriteToIndent then bracesToIndented(body, rewriteWithColon)
549
547
else inBraces(body)
550
548
551
549
def inDefScopeBraces [T ](body : => T , rewriteWithColon : Boolean = false ): T =
@@ -635,68 +633,62 @@ object Parsers {
635
633
else idx
636
634
637
635
/** Parse indentation region `body` and rewrite it to be in braces instead */
638
- def indentedToBraces [T ](body : => T ): T = {
639
- val enclRegion = in.currentRegion.enclosing
640
- def indentWidth = enclRegion.indentWidth
636
+ def indentedToBraces [T ](body : => T ): T =
637
+ val enclRegion = in.currentRegion.enclosing // capture on entry
638
+ def indentWidth = enclRegion.indentWidth
641
639
val followsColon = testChar(in.lastOffset - 1 , ':' )
642
- val startOpening =
643
- if (followsColon)
644
- if (testChar(in.lastOffset - 2 , ' ' )) in.lastOffset - 2
645
- else in.lastOffset - 1
646
- else in.lastOffset
647
- val endOpening = in.lastOffset
648
-
649
- val t = enclosed(INDENT , body)
650
640
651
641
/** Is `expr` a tree that lacks a final `else`? Put such trees in `{...}` to make
652
642
* sure we don't accidentally merge them with a following `else`.
653
643
*/
654
644
def isPartialIf (expr : Tree ): Boolean = expr match {
655
645
case If (_, _, EmptyTree ) => true
656
- case If (_, _, e) => isPartialIf(e)
657
- case _ => false
646
+ case If (_, _, e) => isPartialIf(e)
647
+ case _ => false
658
648
}
659
649
660
650
/** Is `expr` a (possibly curried) function that has a multi-statement block
661
651
* as body? Put such trees in `{...}` since we don't enclose statements following
662
652
* a `=>` in braces.
663
653
*/
664
654
def isBlockFunction [T ](expr : T ): Boolean = expr match {
665
- case Function (_, body) => isBlockFunction(body)
655
+ case Function (_, body) => isBlockFunction(body)
666
656
case Block (stats, expr) => stats.nonEmpty || isBlockFunction(expr)
667
- case _ => false
657
+ case _ => false
668
658
}
669
659
670
660
/** Start of first line after in.lastOffset that does not have a comment
671
661
* at indent width greater than the indent width of the closing brace.
672
662
*/
673
663
def closingOffset (lineStart : Offset ): Offset =
674
- if (in.lineOffset >= 0 && lineStart >= in.lineOffset) in.lineOffset
675
- else {
676
- val candidate = source.nextLine(lineStart)
664
+ if in.lineOffset >= 0 && lineStart >= in.lineOffset then in.lineOffset
665
+ else
677
666
val commentStart = skipBlanks(lineStart)
678
- if (testChar(commentStart, '/' ) && indentWidth < in.indentWidth(commentStart))
679
- closingOffset(source.nextLine(lineStart))
680
- else
681
- lineStart
682
- }
667
+ if testChar(commentStart, '/' ) && indentWidth < in.indentWidth(commentStart)
668
+ then closingOffset(source.nextLine(lineStart))
669
+ else lineStart
683
670
684
671
def needsBraces (t : Any ): Boolean = t match {
685
672
case Match (EmptyTree , _) => true
686
- case Block (stats, expr) =>
687
- stats.nonEmpty || needsBraces(expr)
688
- case expr : Tree =>
689
- followsColon ||
690
- isPartialIf(expr) && in.token == ELSE ||
691
- isBlockFunction(expr)
692
- case _ => true
693
- }
694
- if (needsBraces(t)) {
673
+ case Block (stats, expr) => stats.nonEmpty || needsBraces(expr)
674
+ case expr : Tree => followsColon
675
+ || isPartialIf(expr) && in.token == ELSE
676
+ || isBlockFunction(expr)
677
+ case _ => true
678
+ }
679
+ // begin indentedToBraces
680
+ val startOpening =
681
+ if followsColon then
682
+ if testChar(in.lastOffset - 2 , ' ' ) then in.lastOffset - 2
683
+ else in.lastOffset - 1
684
+ else in.lastOffset
685
+ val endOpening = in.lastOffset
686
+ val t = enclosed(INDENT , body)
687
+ if needsBraces(t) then
695
688
patch(source, Span (startOpening, endOpening), " {" )
696
689
patch(source, Span (closingOffset(source.nextLine(in.lastOffset))), indentWidth.toPrefix ++ " }\n " )
697
- }
698
690
t
699
- }
691
+ end indentedToBraces
700
692
701
693
/** The region to eliminate when replacing an opening `(` or `{` that ends a line.
702
694
* The `(` or `{` is at in.offset.
@@ -1304,17 +1296,21 @@ object Parsers {
1304
1296
case _ : (ForYield | ForDo ) => in.token == FOR
1305
1297
case _ => false
1306
1298
1307
- def matchesAndSetEnd (last : T ): Boolean = {
1299
+ def endName = if in.token == IDENTIFIER then in.name.toString else tokenString(in.token)
1300
+
1301
+ def matchesAndSetEnd (last : T ): Boolean =
1308
1302
val didMatch = matches(last)
1309
1303
if didMatch then
1310
1304
updateSpanOfLast(last)
1311
1305
didMatch
1312
- }
1313
1306
1314
1307
if in.token == END then
1315
1308
val start = in.skipToken()
1316
1309
if stats.isEmpty || ! matchesAndSetEnd(stats.last) then
1317
1310
syntaxError(" misaligned end marker" , Span (start, in.lastCharOffset))
1311
+ else if overlapsPatch(source, Span (start, start)) then
1312
+ patch(source, Span (start, start), " " )
1313
+ patch(source, Span (start, in.lastCharOffset), s " } // end $endName" )
1318
1314
in.token = IDENTIFIER // Leaving it as the original token can confuse newline insertion
1319
1315
in.nextToken()
1320
1316
end checkEndMarker
@@ -3854,9 +3850,9 @@ object Parsers {
3854
3850
def templateStatSeq (): (ValDef , List [Tree ]) = checkNoEscapingPlaceholders {
3855
3851
var self : ValDef = EmptyValDef
3856
3852
val stats = new ListBuffer [Tree ]
3857
- if ( isExprIntro && ! isDefIntro(modifierTokens)) {
3853
+ if isExprIntro && ! isDefIntro(modifierTokens) then
3858
3854
val first = expr1()
3859
- if ( in.token == ARROW ) {
3855
+ if in.token == ARROW then
3860
3856
first match {
3861
3857
case Typed (tree @ This (EmptyTypeIdent ), tpt) =>
3862
3858
self = makeSelfDef(nme.WILDCARD , tpt).withSpan(first.span)
@@ -3867,11 +3863,10 @@ object Parsers {
3867
3863
}
3868
3864
in.token = SELFARROW // suppresses INDENT insertion after `=>`
3869
3865
in.nextToken()
3870
- }
3871
3866
else
3872
3867
stats += first
3873
3868
statSepOrEnd(stats)
3874
- }
3869
+ end if
3875
3870
while
3876
3871
var empty = false
3877
3872
if (in.token == IMPORT )
@@ -3888,7 +3883,7 @@ object Parsers {
3888
3883
empty = true
3889
3884
statSepOrEnd(stats, empty)
3890
3885
do ()
3891
- (self, if ( stats.isEmpty) List (EmptyTree ) else stats.toList)
3886
+ (self, if stats.isEmpty then List (EmptyTree ) else stats.toList)
3892
3887
}
3893
3888
3894
3889
/** RefineStatSeq ::= RefineStat {semi RefineStat}
0 commit comments