@@ -562,15 +562,14 @@ object Parsers {
562
562
def inBracesOrIndented [T ](body : => T , inStatSeq : Boolean = false , rewriteWithColon : Boolean = false ): T =
563
563
val followsArrow = in.last.token == ARROW
564
564
if in.token == INDENT then
565
- val rewriteToBraces = in.rewriteNoIndent
566
- && ! followsArrow // braces are always optional after `=>` so none should be inserted
565
+ // braces are always optional after `=>` so none should be inserted
566
+ val rewriteToBraces = in.rewriteNoIndent && ! followsArrow
567
567
val rewriteToIndent = in.rewriteToIndent && ! followsArrow
568
568
if rewriteToBraces then indentedToBraces(body)
569
569
else if rewriteToIndent then enclosed(INDENT , toIndentedRegion(body))
570
570
else enclosed(INDENT , body)
571
571
else
572
- if in.rewriteToIndent then
573
- bracesToIndented(body, inStatSeq, rewriteWithColon)
572
+ if in.rewriteToIndent then bracesToIndented(body, inStatSeq, rewriteWithColon)
574
573
else inBraces(body)
575
574
576
575
def inDefScopeBraces [T ](body : => T , inStatSeq : Boolean = false , rewriteWithColon : Boolean = false ): T =
@@ -642,10 +641,17 @@ object Parsers {
642
641
643
642
/* -------- REWRITES ----------------------------------------------------------- */
644
643
645
- /** The last offset where a colon at the end of line would be required if a subsequent { ... }
646
- * block would be converted to an indentation region.
647
- */
648
- var possibleColonOffset : Int = - 1
644
+ object IndentRewriteState :
645
+ /** The last offset where a colon at the end of line would be required if a subsequent { ... }
646
+ * block would be converted to an indentation region. */
647
+ var possibleColonOffset : Int = - 1
648
+
649
+ /** When rewriting to indent, the minimum indent width to rewrite to */
650
+ var minimumIndent : IndentWidth = IndentWidth .Zero
651
+
652
+ /** When rewritting to indent, the maximum indent width to rewrite to
653
+ * to ensure an indent region is properly closed by outdentation */
654
+ var maximumIndent : Option [IndentWidth ] = None
649
655
650
656
def testChar (idx : Int , p : Char => Boolean ): Boolean = {
651
657
val txt = source.content
@@ -735,7 +741,7 @@ object Parsers {
735
741
else (start, end) // skip the end of line
736
742
else (offset, end) // skip from last to end of token
737
743
738
- /** Expand the current span to the surrounding blank lines */
744
+ /** Expand the current span to its surrounding blank space */
739
745
def blankLinesAround (start : Offset , end : Offset ): (Offset , Offset ) =
740
746
(skipBlanks(start - 1 , - 1 ) + 1 , skipBlanks(end, 1 ))
741
747
@@ -756,7 +762,8 @@ object Parsers {
756
762
* false
757
763
* }
758
764
*/
759
- def bracesToIndented [T ](body : => T , inStatSeq : Boolean , rewriteWithColon : Boolean ): T = {
765
+ def bracesToIndented [T ](body : => T , inStatSeq : Boolean , rewriteWithColon : Boolean ): T =
766
+ import IndentRewriteState .*
760
767
val lastSaved = in.last.saveCopy
761
768
val lastOffsetSaved = in.lastOffset
762
769
val underColonSyntax = possibleColonOffset == in.lastOffset
@@ -770,13 +777,10 @@ object Parsers {
770
777
lastSaved.token != RBRACE && // test (6)
771
778
! (lastSaved.isOperator && lastSaved.isAfterLineEnd) && // test (7)
772
779
! inStatSeq // test (8)
773
- var innerIndent : Option [IndentWidth ] = None
774
780
val t = enclosed(LBRACE , {
775
781
if in.isAfterLineEnd && in.token != RBRACE then // test (2)(4)
776
782
toIndentedRegion :
777
- try
778
- innerIndent = Some (minimumIndent)
779
- body
783
+ try body
780
784
finally canRewrite &= in.isAfterLineEnd // test (3)
781
785
else
782
786
canRewrite = false
@@ -794,29 +798,22 @@ object Parsers {
794
798
if lastSaved.isOperator then
795
799
patch(Span (lastSaved.offset, lastSaved.offset + lastSaved.name.length), s " ` ${lastSaved.name}` : " )
796
800
else patch(Span (lastOffsetSaved), " :" )
797
- // force outdentation of token after }
798
- maximumIndent = innerIndent
799
801
else
800
802
// no need to force outdentation after `}`
801
803
maximumIndent = None
802
804
t
803
- }
804
-
805
- /** When rewriting to indent, the minimum indent width to rewrite to */
806
- var minimumIndent : IndentWidth = IndentWidth .Zero
807
- /** When rewritting to indent, the maximum indent width to rewrite to
808
- * to ensure an indent region is properly closed by outdentation */
809
- var maximumIndent : Option [IndentWidth ] = None
805
+ end bracesToIndented
810
806
811
807
/** When rewriting to indent, make sure there is an indent after a `=>\n` */
812
- def indentedRegionAfterArrow [T ](body : => T , inCase : Boolean = false ): T =
813
- if in.rewriteToIndent && (inCase || in.isAfterLineEnd) then
808
+ def indentedRegionAfterArrow [T ](body : => T , inCaseDef : Boolean = false ): T =
809
+ if in.rewriteToIndent && (inCaseDef || in.isAfterLineEnd) then
814
810
assert(in.last.isArrow || in.last.token == SELFARROW )
815
811
toIndentedRegion(body)
816
812
else body
817
813
818
814
/** compute required indentation to indent region properly */
819
815
def toIndentedRegion [T ](body : => T ): T =
816
+ import IndentRewriteState .*
820
817
val enclosingIndent = minimumIndent
821
818
minimumIndent =
822
819
if enclosingIndent < in.currentRegion.indentWidth then
@@ -834,6 +831,7 @@ object Parsers {
834
831
/** when rewriting to indent, check that indentaton is correct or patch */
835
832
def patchIndent (): Unit =
836
833
if in.isAfterLineEnd && ! in.isNewLine && in.token != OUTDENT && in.token != INDENT then
834
+ import IndentRewriteState .*
837
835
val currentIndent = in.indentWidth(in.offset)
838
836
val indentEndOffset = in.lineOffset + currentIndent.size
839
837
def isDotOrClosing = (closingParens + DOT ).contains(in.token)
@@ -844,7 +842,8 @@ object Parsers {
844
842
if needsOutdent || ! (currentIndent >= minimumIndent) || offByOne then
845
843
patch(Span (in.lineOffset, indentEndOffset), minimumIndent.toPrefix)
846
844
// no need to outdent anymore
847
- maximumIndent = None
845
+ if in.token != RBRACE then
846
+ maximumIndent = None
848
847
849
848
def nextToken (): Unit =
850
849
if in.rewriteToIndent then patchIndent()
@@ -1401,7 +1400,7 @@ object Parsers {
1401
1400
syntaxErrorOrIncomplete(em " indented definitions expected, ${in} found " )
1402
1401
1403
1402
def colonAtEOLOpt (): Unit =
1404
- possibleColonOffset = in.lastOffset
1403
+ IndentRewriteState . possibleColonOffset = in.lastOffset
1405
1404
in.observeColonEOL(inTemplate = false )
1406
1405
if in.token == COLONeol then
1407
1406
nextToken()
@@ -2883,7 +2882,7 @@ object Parsers {
2883
2882
|an indented case. """ )
2884
2883
expr()
2885
2884
else block()
2886
- }, inCase = true )
2885
+ }, inCaseDef = true )
2887
2886
})
2888
2887
}
2889
2888
@@ -2901,7 +2900,7 @@ object Parsers {
2901
2900
}
2902
2901
}
2903
2902
CaseDef (pat, EmptyTree , atSpan(accept(ARROW )) {
2904
- val t = indentedRegionAfterArrow(rejectWildcardType(typ()), inCase = true )
2903
+ val t = indentedRegionAfterArrow(rejectWildcardType(typ()), inCaseDef = true )
2905
2904
if in.token == SEMI then nextToken()
2906
2905
newLinesOptWhenFollowedBy(CASE )
2907
2906
t
0 commit comments