@@ -234,7 +234,7 @@ object Scanners {
234234 val newSyntax = ctx.settings.newSyntax.value
235235
236236 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
238238 val rewriteToIndent = ctx.settings.indent.value && rewrite
239239 val rewriteNoIndent = noindentSyntax && rewrite
240240
@@ -352,15 +352,16 @@ object Scanners {
352352 case LPAREN | LBRACKET =>
353353 currentRegion = InParens (lastToken, currentRegion)
354354 case LBRACE =>
355- currentRegion = InBraces (currentRegion)
355+ currentRegion = InBraces (null , currentRegion)
356356 case RBRACE =>
357357 def dropBraces (): Unit = currentRegion match {
358- case Outermost =>
359- case InBraces (outer) =>
360- currentRegion = outer
358+ case r : InBraces =>
359+ currentRegion = r.enclosing
361360 case _ =>
362- currentRegion = currentRegion.enclosing
363- dropBraces()
361+ if (! currentRegion.isOutermost) {
362+ currentRegion = currentRegion.enclosing
363+ dropBraces()
364+ }
364365 }
365366 dropBraces()
366367 case RPAREN | RBRACKET =>
@@ -566,42 +567,44 @@ object Scanners {
566567 var newlineIsSeparating = false
567568 var lastWidth = IndentWidth .Zero
568569 var indentPrefix = EMPTY
570+ val nextWidth = indentWidth(offset)
569571 currentRegion match {
570- case Outermost =>
571- if (indentSyntax) indentIsSignificant = true
572- newlineIsSeparating = true
573572 case r : Indented =>
574- indentIsSignificant = true
575- newlineIsSeparating = true
573+ indentIsSignificant = indentSyntax
576574 lastWidth = r.width
575+ newlineIsSeparating = lastWidth <= nextWidth
577576 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
579581 newlineIsSeparating = true
582+ indentPrefix = LBRACE
580583 case _ =>
581584 }
582- val nextWidth = indentWidth(offset)
583585 if (newlineIsSeparating &&
584- canEndStatTokens.contains(lastToken)&&
586+ canEndStatTokens.contains(lastToken) &&
585587 canStartStatTokens.contains(token) &&
586- (! indentIsSignificant || lastWidth <= nextWidth) &&
587588 ! isLeadingInfixOperator())
588589 insert(if (pastBlankLine) NEWLINES else NEWLINE , lineOffset)
589590 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 ) {
592603 if (canStartIndentTokens.contains(lastToken)) {
593604 currentRegion = Indented (nextWidth, Set (), lastToken, currentRegion)
594605 insert(INDENT , offset)
595606 }
596607 }
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- }
605608 else if (lastWidth != nextWidth)
606609 errorButContinue(
607610 i """ Incompatible combinations of tabs and spaces in indentation prefixes.
@@ -661,9 +664,9 @@ object Scanners {
661664 val atEOL = isAfterLineEnd
662665 reset()
663666 if (atEOL) token = COLONEOL
664- case EOF =>
667+ case EOF | RBRACE =>
665668 currentRegion match {
666- case r : Indented =>
669+ case r : Indented if ! r.isOutermost =>
667670 insert(OUTDENT , offset)
668671 currentRegion = r.outer
669672 case _ =>
@@ -1368,16 +1371,16 @@ object Scanners {
13681371 def enclosing : Region = outer.asInstanceOf [Region ]
13691372 }
13701373 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
13721375 case class InString (multiLine : Boolean , outer : Region ) extends Region
13731376
13741377 /** A class describing an indentation region.
13751378 * @param width The principal indendation width
13761379 * @param others Other indendation widths > width of lines in the same region
13771380 */
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
13791382
1380- case object Outermost extends Region { val outer = null }
1383+ val Outermost = Indented ( IndentWidth . Zero , Set (), EMPTY , null )
13811384
13821385 enum IndentWidth {
13831386 case Run (ch : Char , n : Int )
0 commit comments