@@ -305,7 +305,12 @@ object Scanners {
305305 * (the STRINGLIT appears twice in succession on the stack iff the
306306 * expression is a multiline string literal).
307307 */
308- var sepRegions : Region = GlobalRegion
308+ var currentRegion : Region = Outermost
309+
310+ def currentIndentWidth = currentRegion match {
311+ case r : Indented => r.width
312+ case _ => 0
313+ }
309314
310315 /** Indentation widths, innermost to outermost */
311316 var indent : IndentRegion = IndentRegion (IndentWidth .Zero , Set (), EMPTY , null )
@@ -330,16 +335,11 @@ object Scanners {
330335
331336// Get next token ------------------------------------------------------------
332337
333- /** Are we directly in a string interpolation expression?
334- */
335- private def inStringInterpolation =
336- sepRegions.isInstanceOf [InStringRegion ]
337-
338338 /** Are we directly in a multiline string interpolation expression?
339339 * @pre inStringInterpolation
340340 */
341- private def inMultiLineInterpolation = sepRegions match {
342- case InStringRegion (_, multiLine ) => multiLine
341+ private def inMultiLineInterpolation = currentRegion match {
342+ case InString (multiLine, _ ) => multiLine
343343 case _ => false
344344 }
345345
@@ -353,27 +353,27 @@ object Scanners {
353353
354354 def adjustSepRegions (lastToken : Token ): Unit = (lastToken : @ switch) match {
355355 case LPAREN | LBRACKET =>
356- sepRegions = InParensRegion (sepRegions, lastToken + 1 )
356+ currentRegion = InParens (lastToken, currentRegion )
357357 case LBRACE =>
358- sepRegions = InBracesRegion (sepRegions )
358+ currentRegion = InBraces (currentRegion )
359359 case RBRACE =>
360- def dropBraces (): Unit = sepRegions match {
361- case GlobalRegion =>
362- case InBracesRegion (outer) =>
363- sepRegions = outer
360+ def dropBraces (): Unit = currentRegion match {
361+ case Outermost =>
362+ case InBraces (outer) =>
363+ currentRegion = outer
364364 case _ =>
365- sepRegions = sepRegions.outer
365+ currentRegion = currentRegion.enclosing
366366 dropBraces()
367367 }
368368 dropBraces()
369369 case RPAREN | RBRACKET =>
370- sepRegions match {
371- case InParensRegion (outer, closing ) if closing == lastToken => sepRegions = outer
370+ currentRegion match {
371+ case InParens (prefix, outer ) if prefix + 1 == lastToken => currentRegion = outer
372372 case _ =>
373373 }
374374 case STRINGLIT =>
375- sepRegions match {
376- case InStringRegion (outer, _ ) => sepRegions = outer
375+ currentRegion match {
376+ case InString (_, outer ) => currentRegion = outer
377377 case _ =>
378378 }
379379 case _ =>
@@ -388,8 +388,11 @@ object Scanners {
388388 // Read a token or copy it from `next` tokenData
389389 if (next.token == EMPTY ) {
390390 lastOffset = lastCharOffset
391- if (inStringInterpolation) fetchStringPart() else fetchToken()
392- if (token == ERROR ) adjustSepRegions(STRINGLIT )
391+ currentRegion match {
392+ case InString (multiLine, _) => fetchStringPart(multiLine)
393+ case _ => fetchToken()
394+ }
395+ if (token == ERROR ) adjustSepRegions(STRINGLIT ) // make sure we exit enclosing string literal
393396 }
394397 else {
395398 this .copyFrom(next)
@@ -511,14 +514,14 @@ object Scanners {
511514 *
512515 * Indentation is _significant_ if indentSyntax is set, and we are not inside a
513516 * {...}, [...], (...), case ... => pair, nor in a if/while condition
514- * (i.e. sepRegions is empty).
517+ * (i.e. currentRegion is empty).
515518 *
516519 * There are three rules:
517520 *
518521 * 1. Insert NEWLINE or NEWLINES if
519522 *
520523 * - the closest enclosing sepRegion is { ... } or for ... do/yield,
521- * or we are on the toplevel, i.e. sepRegions is empty, and
524+ * or we are on the toplevel, i.e. currentRegion is empty, and
522525 * - the previous token can end a statement, and
523526 * - the current token can start a statement, and
524527 * - the current token is not a leading infix operator, and
@@ -562,9 +565,9 @@ object Scanners {
562565 * if the current indentation width and the indentation of the current token are incomparable.
563566 */
564567 def handleNewLine (lastToken : Token ) = {
565- val indentIsSignificant = indentSyntax && sepRegions.isEmpty
566- val newlineIsSeparating = sepRegions match {
567- case GlobalRegion | InBracesRegion (_) => true
568+ val indentIsSignificant = indentSyntax && currentRegion.isOutermost
569+ val newlineIsSeparating = currentRegion match {
570+ case Outermost | InBraces (_) => true
568571 case _ => false
569572 }
570573 val curWidth = indentWidth(offset)
@@ -758,7 +761,7 @@ object Scanners {
758761 case '\" ' =>
759762 def stringPart (multiLine : Boolean ) = {
760763 getStringPart(multiLine)
761- sepRegions = InStringRegion (sepRegions, multiLine )
764+ currentRegion = InString (multiLine, currentRegion )
762765 }
763766 def fetchDoubleQuote () = {
764767 if (token == INTERPOLATIONID ) {
@@ -1129,9 +1132,9 @@ object Scanners {
11291132 }
11301133 }
11311134
1132- private def fetchStringPart () = {
1135+ private def fetchStringPart (multiLine : Boolean ) = {
11331136 offset = charOffset - 1
1134- getStringPart(multiLine = inMultiLineInterpolation )
1137+ getStringPart(multiLine)
11351138 }
11361139
11371140 private def isTripleQuote (): Boolean =
@@ -1342,16 +1345,15 @@ object Scanners {
13421345 // end Scanner
13431346
13441347 abstract class Region {
1345- def outer : Region
1346- def isEmpty = false
1347- }
1348- case class InParensRegion (val outer : Region , closing : Token ) extends Region
1349- case class InBracesRegion (val outer : Region ) extends Region
1350- case class InStringRegion (val outer : Region , multiLine : Boolean ) extends Region
1351- case object GlobalRegion extends Region {
1352- def outer = throw UnsupportedOperationException (" GlobalRegion.outer" )
1353- override def isEmpty = true
1348+ def outer : Region | Null
1349+ def isOutermost = outer == null
1350+ def enclosing : Region = outer.asInstanceOf [Region ]
13541351 }
1352+ case class InParens (prefix : Token , outer : Region ) extends Region
1353+ case class InBraces (outer : Region ) extends Region
1354+ case class InString (multiLine : Boolean , outer : Region ) extends Region
1355+ case class Indented (width : IndentWidth , others : Set [IndentWidth ], prefix : Token , outer : Region ) extends Region
1356+ case object Outermost extends Region { val outer = null }
13551357
13561358 /** A class describing an indentation region.
13571359 * @param width The principal indendation width
0 commit comments