Skip to content

Commit 9e9c27e

Browse files
committed
Fix indentWidth calculation when rewriting to braces
1 parent 1d09f6c commit 9e9c27e

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

+2-5
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,7 @@ object Parsers {
607607
/** Parse indentation region `body` and rewrite it to be in braces instead */
608608
def indentedToBraces[T](body: => T): T = {
609609
val enclRegion = in.currentRegion.enclosing
610-
def indentWidth = enclRegion match {
611-
case Indented(w, _, _, _) => w
612-
case _ => IndentWidth.Zero
613-
}
610+
def indentWidth = enclRegion.indentWidth
614611
val followsColon = testChar(in.lastOffset - 1, ':')
615612
val startOpening =
616613
if (followsColon)
@@ -1175,7 +1172,7 @@ object Parsers {
11751172
}
11761173

11771174
def indentRegion[T](tag: EndMarkerTag)(op: => T): T = {
1178-
val iw = in.currentIndentWidth
1175+
val iw = in.currentRegion.indentWidth
11791176
val t = op
11801177
in.consumeEndMarker(tag, iw)
11811178
t

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

+10-11
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,6 @@ object Scanners {
307307
*/
308308
var currentRegion: Region = Outermost
309309

310-
def currentIndentWidth: IndentWidth = currentRegion match {
311-
case r: Indented => r.width
312-
case _ => IndentWidth.Zero
313-
}
314-
315310
/** The end marker that was skipped last */
316311
val endMarkers = new mutable.ListBuffer[EndMarker]
317312

@@ -416,7 +411,7 @@ object Scanners {
416411
* value at the end of the endMarkers queue.
417412
*/
418413
private def handleEndMarkers(width: IndentWidth): Unit = {
419-
if (next.token == IDENTIFIER && next.name == nme.end && width == currentIndentWidth) {
414+
if (next.token == IDENTIFIER && next.name == nme.end && width == currentRegion.indentWidth) {
420415
val lookahead = lookaheadScanner
421416
lookahead.nextToken() // skip the `end`
422417

@@ -589,15 +584,14 @@ object Scanners {
589584
insert(if (pastBlankLine) NEWLINES else NEWLINE, lineOffset)
590585
else if (indentIsSignificant) {
591586
if (nextWidth < lastWidth
592-
|| nextWidth == lastWidth && (indentPrefix == MATCH || indentPrefix == CATCH) && token != CASE) {
587+
|| nextWidth == lastWidth && (indentPrefix == MATCH || indentPrefix == CATCH) && token != CASE)
593588
currentRegion match {
594589
case r: Indented if !r.isOutermost && !isLeadingInfixOperator() =>
595590
currentRegion = r.enclosing
596591
insert(OUTDENT, offset)
597592
handleEndMarkers(nextWidth)
598593
case _ =>
599594
}
600-
}
601595
else if (lastWidth < nextWidth ||
602596
lastWidth == nextWidth && (lastToken == MATCH || lastToken == CATCH) && token == CASE) {
603597
if (canStartIndentTokens.contains(lastToken)) {
@@ -1369,16 +1363,21 @@ object Scanners {
13691363
def outer: Region | Null
13701364
def isOutermost = outer == null
13711365
def enclosing: Region = outer.asInstanceOf[Region]
1366+
def indentWidth = IndentWidth.Zero
13721367
}
1373-
case class InParens(prefix: Token, outer: Region) extends Region
1374-
case class InBraces(var width: IndentWidth | Null, outer: Region) extends Region
13751368
case class InString(multiLine: Boolean, outer: Region) extends Region
1369+
case class InParens(prefix: Token, outer: Region) extends Region
1370+
case class InBraces(var width: IndentWidth | Null, outer: Region) extends Region {
1371+
override def indentWidth = width
1372+
}
13761373

13771374
/** A class describing an indentation region.
13781375
* @param width The principal indendation width
13791376
* @param others Other indendation widths > width of lines in the same region
13801377
*/
1381-
case class Indented(width: IndentWidth, others: Set[IndentWidth], prefix: Token, outer: Region | Null) extends Region
1378+
case class Indented(width: IndentWidth, others: Set[IndentWidth], prefix: Token, outer: Region | Null) extends Region {
1379+
override def indentWidth = width
1380+
}
13821381

13831382
val Outermost = Indented(IndentWidth.Zero, Set(), EMPTY, null)
13841383

0 commit comments

Comments
 (0)