Skip to content

Commit 6720ae6

Browse files
committed
Refacto
1 parent 3603b66 commit 6720ae6

File tree

7 files changed

+36
-41
lines changed

7 files changed

+36
-41
lines changed

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

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -562,15 +562,14 @@ object Parsers {
562562
def inBracesOrIndented[T](body: => T, inStatSeq: Boolean = false, rewriteWithColon: Boolean = false): T =
563563
val followsArrow = in.last.token == ARROW
564564
if in.token == INDENT then
565-
val rewriteToBraces = in.rewriteNoIndent
566-
&& !followsArrow // braces are always optional after `=>` so none should be inserted
565+
// braces are always optional after `=>` so none should be inserted
566+
val rewriteToBraces = in.rewriteNoIndent && !followsArrow
567567
val rewriteToIndent = in.rewriteToIndent && !followsArrow
568568
if rewriteToBraces then indentedToBraces(body)
569569
else if rewriteToIndent then enclosed(INDENT, toIndentedRegion(body))
570570
else enclosed(INDENT, body)
571571
else
572-
if in.rewriteToIndent then
573-
bracesToIndented(body, inStatSeq, rewriteWithColon)
572+
if in.rewriteToIndent then bracesToIndented(body, inStatSeq, rewriteWithColon)
574573
else inBraces(body)
575574

576575
def inDefScopeBraces[T](body: => T, inStatSeq: Boolean = false, rewriteWithColon: Boolean = false): T =
@@ -642,10 +641,17 @@ object Parsers {
642641

643642
/* -------- REWRITES ----------------------------------------------------------- */
644643

645-
/** The last offset where a colon at the end of line would be required if a subsequent { ... }
646-
* block would be converted to an indentation region.
647-
*/
648-
var possibleColonOffset: Int = -1
644+
object IndentRewriteState:
645+
/** The last offset where a colon at the end of line would be required if a subsequent { ... }
646+
* block would be converted to an indentation region. */
647+
var possibleColonOffset: Int = -1
648+
649+
/** When rewriting to indent, the minimum indent width to rewrite to */
650+
var minimumIndent: IndentWidth = IndentWidth.Zero
651+
652+
/** When rewritting to indent, the maximum indent width to rewrite to
653+
* to ensure an indent region is properly closed by outdentation */
654+
var maximumIndent: Option[IndentWidth] = None
649655

650656
def testChar(idx: Int, p: Char => Boolean): Boolean = {
651657
val txt = source.content
@@ -735,7 +741,7 @@ object Parsers {
735741
else (start, end) // skip the end of line
736742
else (offset, end) // skip from last to end of token
737743

738-
/** Expand the current span to the surrounding blank lines */
744+
/** Expand the current span to its surrounding blank space */
739745
def blankLinesAround(start: Offset, end: Offset): (Offset, Offset) =
740746
(skipBlanks(start - 1, -1) + 1, skipBlanks(end, 1))
741747

@@ -756,7 +762,8 @@ object Parsers {
756762
* false
757763
* }
758764
*/
759-
def bracesToIndented[T](body: => T, inStatSeq: Boolean, rewriteWithColon: Boolean): T = {
765+
def bracesToIndented[T](body: => T, inStatSeq: Boolean, rewriteWithColon: Boolean): T =
766+
import IndentRewriteState.*
760767
val lastSaved = in.last.saveCopy
761768
val lastOffsetSaved = in.lastOffset
762769
val underColonSyntax = possibleColonOffset == in.lastOffset
@@ -770,13 +777,10 @@ object Parsers {
770777
lastSaved.token != RBRACE && // test (6)
771778
!(lastSaved.isOperator && lastSaved.isAfterLineEnd) && // test (7)
772779
!inStatSeq // test (8)
773-
var innerIndent: Option[IndentWidth] = None
774780
val t = enclosed(LBRACE, {
775781
if in.isAfterLineEnd && in.token != RBRACE then // test (2)(4)
776782
toIndentedRegion:
777-
try
778-
innerIndent = Some(minimumIndent)
779-
body
783+
try body
780784
finally canRewrite &= in.isAfterLineEnd // test (3)
781785
else
782786
canRewrite = false
@@ -794,29 +798,22 @@ object Parsers {
794798
if lastSaved.isOperator then
795799
patch(Span(lastSaved.offset, lastSaved.offset + lastSaved.name.length), s"`${lastSaved.name}` :")
796800
else patch(Span(lastOffsetSaved), ":")
797-
// force outdentation of token after }
798-
maximumIndent = innerIndent
799801
else
800802
// no need to force outdentation after `}`
801803
maximumIndent = None
802804
t
803-
}
804-
805-
/** When rewriting to indent, the minimum indent width to rewrite to */
806-
var minimumIndent: IndentWidth = IndentWidth.Zero
807-
/** When rewritting to indent, the maximum indent width to rewrite to
808-
* to ensure an indent region is properly closed by outdentation */
809-
var maximumIndent: Option[IndentWidth] = None
805+
end bracesToIndented
810806

811807
/** When rewriting to indent, make sure there is an indent after a `=>\n` */
812-
def indentedRegionAfterArrow[T](body: => T, inCase: Boolean = false): T =
813-
if in.rewriteToIndent && (inCase || in.isAfterLineEnd) then
808+
def indentedRegionAfterArrow[T](body: => T, inCaseDef: Boolean = false): T =
809+
if in.rewriteToIndent && (inCaseDef || in.isAfterLineEnd) then
814810
assert(in.last.isArrow || in.last.token == SELFARROW)
815811
toIndentedRegion(body)
816812
else body
817813

818814
/** compute required indentation to indent region properly */
819815
def toIndentedRegion[T](body: => T): T =
816+
import IndentRewriteState.*
820817
val enclosingIndent = minimumIndent
821818
minimumIndent =
822819
if enclosingIndent < in.currentRegion.indentWidth then
@@ -834,6 +831,7 @@ object Parsers {
834831
/** when rewriting to indent, check that indentaton is correct or patch */
835832
def patchIndent(): Unit =
836833
if in.isAfterLineEnd && !in.isNewLine && in.token != OUTDENT && in.token != INDENT then
834+
import IndentRewriteState.*
837835
val currentIndent = in.indentWidth(in.offset)
838836
val indentEndOffset = in.lineOffset + currentIndent.size
839837
def isDotOrClosing = (closingParens + DOT).contains(in.token)
@@ -844,7 +842,8 @@ object Parsers {
844842
if needsOutdent || !(currentIndent >= minimumIndent) || offByOne then
845843
patch(Span(in.lineOffset, indentEndOffset), minimumIndent.toPrefix)
846844
// no need to outdent anymore
847-
maximumIndent = None
845+
if in.token != RBRACE then
846+
maximumIndent = None
848847

849848
def nextToken(): Unit =
850849
if in.rewriteToIndent then patchIndent()
@@ -1401,7 +1400,7 @@ object Parsers {
14011400
syntaxErrorOrIncomplete(em"indented definitions expected, ${in} found")
14021401

14031402
def colonAtEOLOpt(): Unit =
1404-
possibleColonOffset = in.lastOffset
1403+
IndentRewriteState.possibleColonOffset = in.lastOffset
14051404
in.observeColonEOL(inTemplate = false)
14061405
if in.token == COLONeol then
14071406
nextToken()
@@ -2883,7 +2882,7 @@ object Parsers {
28832882
|an indented case.""")
28842883
expr()
28852884
else block()
2886-
}, inCase = true)
2885+
}, inCaseDef = true)
28872886
})
28882887
}
28892888

@@ -2901,7 +2900,7 @@ object Parsers {
29012900
}
29022901
}
29032902
CaseDef(pat, EmptyTree, atSpan(accept(ARROW)) {
2904-
val t = indentedRegionAfterArrow(rejectWildcardType(typ()), inCase = true)
2903+
val t = indentedRegionAfterArrow(rejectWildcardType(typ()), inCaseDef = true)
29052904
if in.token == SEMI then nextToken()
29062905
newLinesOptWhenFollowedBy(CASE)
29072906
t

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,19 +1671,15 @@ object Scanners {
16711671

16721672
def < (that: IndentWidth): Boolean = this <= that && !(that <= this)
16731673

1674-
def >= (that: IndentWidth): Boolean = (this, that) match
1675-
case (Run(ch1, n1), Run(ch2, n2)) => n1 >= n2 && (ch1 == ch2 || n2 == 0)
1676-
case (Conc(l1, r1), Conc(l2, r2)) => (l1 == l2 && r1 >= r2) || l1 >= that
1677-
case (Conc(l1, _), _) => l1 >= that
1678-
case _ => false
1674+
def >= (that: IndentWidth): Boolean = that <= this
16791675

1680-
def >(that: IndentWidth): Boolean = this >= that && !(that >= this)
1676+
def >(that: IndentWidth): Boolean = that < this
16811677

16821678
def size: Int = this match
16831679
case Run(_, n) => n
16841680
case Conc(l, r) => l.size + r.n
16851681

1686-
/** Add one level of indentation (one tab or two spaces depending on the last char)**/
1682+
/** Add one level of indentation (one tab or two spaces depending on the last char) */
16871683
def increment: IndentWidth =
16881684
def incRun(ch: Char, n: Int): Run = ch match
16891685
case ' ' => IndentWidth.Run(' ', n + 2)

compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ object Rewrites {
7575
.addPatch(span, replacement)
7676
)
7777

78-
/** Record a patch that replaces an inner patch */
78+
/** Record a patch that replaces the first patch that it contains */
7979
def patchOver(source: SourceFile, span: Span, replacement: String)(using Context): Unit =
8080
if ctx.reporter != Reporter.NoReporter // NoReporter is used for syntax highlighting
8181
then ctx.settings.rewrite.value.foreach(_.patched

tests/rewrites/indent-comments.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Rewriting to indent should not remove the comments
1+
// Rewriting to indent should preserve comments
22
class A: /* 1 */ /* 2 */
33
def m1(b: Boolean) = /* 3 */ /* 4 */
44
val x = if (b)

tests/rewrites/indent-comments.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Rewriting to indent should not remove the comments
1+
// Rewriting to indent should preserve comments
22
class A /* 1 */ { /* 2 */
33
def m1(b: Boolean) = /* 3 */ { /* 4 */
44
val x = if (b)

tests/rewrites/indent-rewrite.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// A collection of patterns/bugs found when rewriting the community build to indent
1+
// A collection of patterns found when rewriting the community build to indent
22

33
trait C1:
44

tests/rewrites/indent-rewrite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// A collection of patterns/bugs found when rewriting the community build to indent
1+
// A collection of patterns found when rewriting the community build to indent
22

33
trait C1 {
44

0 commit comments

Comments
 (0)