@@ -234,7 +234,7 @@ object Scanners {
234
234
val newSyntax = ctx.settings.newSyntax.value
235
235
236
236
val noindentSyntax = ctx.settings.noindent.value
237
- val indentSyntax = Config .allowIndent || ctx.settings.indent.value || noindentSyntax && rewrite
237
+ val indentSyntax = Config .alwaysIndent || ctx.settings.indent.value || noindentSyntax && rewrite
238
238
val rewriteToIndent = ctx.settings.indent.value && rewrite
239
239
val rewriteNoIndent = noindentSyntax && rewrite
240
240
@@ -352,15 +352,16 @@ object Scanners {
352
352
case LPAREN | LBRACKET =>
353
353
currentRegion = InParens (lastToken, currentRegion)
354
354
case LBRACE =>
355
- currentRegion = InBraces (currentRegion)
355
+ currentRegion = InBraces (null , currentRegion)
356
356
case RBRACE =>
357
357
def dropBraces (): Unit = currentRegion match {
358
- case Outermost =>
359
- case InBraces (outer) =>
360
- currentRegion = outer
358
+ case r : InBraces =>
359
+ currentRegion = r.enclosing
361
360
case _ =>
362
- currentRegion = currentRegion.enclosing
363
- dropBraces()
361
+ if (! currentRegion.isOutermost) {
362
+ currentRegion = currentRegion.enclosing
363
+ dropBraces()
364
+ }
364
365
}
365
366
dropBraces()
366
367
case RPAREN | RBRACKET =>
@@ -566,42 +567,44 @@ object Scanners {
566
567
var newlineIsSeparating = false
567
568
var lastWidth = IndentWidth .Zero
568
569
var indentPrefix = EMPTY
570
+ val nextWidth = indentWidth(offset)
569
571
currentRegion match {
570
- case Outermost =>
571
- if (indentSyntax) indentIsSignificant = true
572
- newlineIsSeparating = true
573
572
case r : Indented =>
574
- indentIsSignificant = true
575
- newlineIsSeparating = true
573
+ indentIsSignificant = indentSyntax
576
574
lastWidth = r.width
575
+ newlineIsSeparating = lastWidth <= nextWidth
577
576
indentPrefix = r.prefix
578
- case _ : InBraces =>
577
+ case r : InBraces =>
578
+ indentIsSignificant = indentSyntax
579
+ if (r.width == null ) r.width = nextWidth
580
+ lastWidth = r.width
579
581
newlineIsSeparating = true
582
+ indentPrefix = LBRACE
580
583
case _ =>
581
584
}
582
- val nextWidth = indentWidth(offset)
583
585
if (newlineIsSeparating &&
584
- canEndStatTokens.contains(lastToken)&&
586
+ canEndStatTokens.contains(lastToken) &&
585
587
canStartStatTokens.contains(token) &&
586
- (! indentIsSignificant || lastWidth <= nextWidth) &&
587
588
! isLeadingInfixOperator())
588
589
insert(if (pastBlankLine) NEWLINES else NEWLINE , lineOffset)
589
590
else if (indentIsSignificant) {
590
- if (lastWidth < nextWidth ||
591
- lastWidth == nextWidth && (lastToken == MATCH || lastToken == CATCH ) && token == CASE ) {
591
+ if (nextWidth < lastWidth
592
+ || nextWidth == lastWidth && (indentPrefix == MATCH || indentPrefix == CATCH ) && token != CASE ) {
593
+ currentRegion match {
594
+ case r : Indented if ! r.isOutermost && ! isLeadingInfixOperator() =>
595
+ currentRegion = r.enclosing
596
+ insert(OUTDENT , offset)
597
+ handleEndMarkers(nextWidth)
598
+ case _ =>
599
+ }
600
+ }
601
+ else if (lastWidth < nextWidth ||
602
+ lastWidth == nextWidth && (lastToken == MATCH || lastToken == CATCH ) && token == CASE ) {
592
603
if (canStartIndentTokens.contains(lastToken)) {
593
604
currentRegion = Indented (nextWidth, Set (), lastToken, currentRegion)
594
605
insert(INDENT , offset)
595
606
}
596
607
}
597
- else if (nextWidth < lastWidth ||
598
- nextWidth == lastWidth && (indentPrefix == MATCH || indentPrefix == CATCH ) && token != CASE ) {
599
- if (! isLeadingInfixOperator()) {
600
- currentRegion = currentRegion.asInstanceOf [Indented ].enclosing
601
- insert(OUTDENT , offset)
602
- handleEndMarkers(nextWidth)
603
- }
604
- }
605
608
else if (lastWidth != nextWidth)
606
609
errorButContinue(
607
610
i """ Incompatible combinations of tabs and spaces in indentation prefixes.
@@ -661,9 +664,9 @@ object Scanners {
661
664
val atEOL = isAfterLineEnd
662
665
reset()
663
666
if (atEOL) token = COLONEOL
664
- case EOF =>
667
+ case EOF | RBRACE =>
665
668
currentRegion match {
666
- case r : Indented =>
669
+ case r : Indented if ! r.isOutermost =>
667
670
insert(OUTDENT , offset)
668
671
currentRegion = r.outer
669
672
case _ =>
@@ -1368,16 +1371,16 @@ object Scanners {
1368
1371
def enclosing : Region = outer.asInstanceOf [Region ]
1369
1372
}
1370
1373
case class InParens (prefix : Token , outer : Region ) extends Region
1371
- case class InBraces (outer : Region ) extends Region
1374
+ case class InBraces (var width : IndentWidth | Null , outer : Region ) extends Region
1372
1375
case class InString (multiLine : Boolean , outer : Region ) extends Region
1373
1376
1374
1377
/** A class describing an indentation region.
1375
1378
* @param width The principal indendation width
1376
1379
* @param others Other indendation widths > width of lines in the same region
1377
1380
*/
1378
- case class Indented (width : IndentWidth , others : Set [IndentWidth ], prefix : Token , outer : Region ) extends Region
1381
+ case class Indented (width : IndentWidth , others : Set [IndentWidth ], prefix : Token , outer : Region | Null ) extends Region
1379
1382
1380
- case object Outermost extends Region { val outer = null }
1383
+ val Outermost = Indented ( IndentWidth . Zero , Set (), EMPTY , null )
1381
1384
1382
1385
enum IndentWidth {
1383
1386
case Run (ch : Char , n : Int )
0 commit comments