Skip to content

Commit c0078af

Browse files
committed
Allow rewritings under constructs that could not be rewritten
1 parent 891d9d1 commit c0078af

File tree

2 files changed

+15
-34
lines changed

2 files changed

+15
-34
lines changed

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,6 @@ object Parsers {
581581
*/
582582
var possibleColonOffset: Int = -1
583583

584-
/** A list of pending patches, to be issued if we can rewrite all enclosing braces to
585-
* indentation regions.
586-
*/
587-
var pendingPatches: List[() => Unit] = Nil
588-
589584
def testChar(idx: Int, p: Char => Boolean): Boolean = {
590585
val txt = source.content
591586
idx < txt.length && p(txt(idx))
@@ -607,10 +602,7 @@ object Parsers {
607602
/** Parse indentation region `body` and rewrite it to be in braces instead */
608603
def indentedToBraces[T](body: => T): T = {
609604
val enclRegion = in.currentRegion.enclosing
610-
def indentWidth = enclRegion match {
611-
case Indented(w, _, _, _) => w
612-
case _ => IndentWidth.Zero
613-
}
605+
def indentWidth = enclRegion.indentWidth
614606
val followsColon = testChar(in.lastOffset - 1, ':')
615607
val startOpening =
616608
if (followsColon)
@@ -706,7 +698,6 @@ object Parsers {
706698
val colonRequired = possibleColonOffset == in.lastOffset
707699
val (startOpening, endOpening) = startingElimRegion(colonRequired)
708700
val isOutermost = in.currentRegion.isOutermost
709-
val savedPending = pendingPatches
710701
def allBraces(r: Region): Boolean = r match {
711702
case r: InBraces => allBraces(r.enclosing)
712703
case _ => r.isOutermost
@@ -728,17 +719,9 @@ object Parsers {
728719
else if (testChar(startOpening - 1, Chars.isOperatorPart(_))) " :"
729720
else ":"
730721
val (startClosing, endClosing) = closingElimRegion()
731-
val applyPatch = () => {
732-
patch(source, Span(startOpening, endOpening), openingPatchStr)
733-
patch(source, Span(startClosing, endClosing), "")
734-
}
735-
pendingPatches = applyPatch :: pendingPatches
736-
if (isOutermost) {
737-
pendingPatches.reverse.foreach(_())
738-
pendingPatches = Nil
739-
}
722+
patch(source, Span(startOpening, endOpening), openingPatchStr)
723+
patch(source, Span(startClosing, endClosing), "")
740724
}
741-
else pendingPatches = savedPending // can't rewrite, cancel all nested patches.
742725
t
743726
}
744727

@@ -1175,7 +1158,7 @@ object Parsers {
11751158
}
11761159

11771160
def indentRegion[T](tag: EndMarkerTag)(op: => T): T = {
1178-
val iw = in.currentIndentWidth
1161+
val iw = in.currentRegion.indentWidth
11791162
val t = op
11801163
in.consumeEndMarker(tag, iw)
11811164
t
@@ -3370,9 +3353,8 @@ object Parsers {
33703353
case _ => false
33713354
}
33723355

3373-
def givenArgs(t: Tree): Tree = {
3356+
def givenArgs(t: Tree): Tree =
33743357
if (in.token == GIVEN) givenArgs(applyGiven(t, prefixExpr)) else t
3375-
}
33763358

33773359
if (in.token == LPAREN)
33783360
inParens {

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

Lines changed: 10 additions & 11 deletions
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)