Skip to content

Commit 58f9a23

Browse files
committed
Rewrite to no-indent keeps end comment
1 parent c0479d9 commit 58f9a23

File tree

8 files changed

+59
-19
lines changed

8 files changed

+59
-19
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ object Parsers {
330330
else if in.token == END then
331331
if endSeen then syntaxError("duplicate end marker")
332332
checkEndMarker(stats)
333-
recur(sepSeen, true)
333+
recur(sepSeen, endSeen = true)
334334
else if isStatSeqEnd || in.token == altEnd then
335335
false
336336
else if sepSeen || endSeen then
@@ -663,7 +663,6 @@ object Parsers {
663663
def closingOffset(lineStart: Offset): Offset =
664664
if in.lineOffset >= 0 && lineStart >= in.lineOffset then in.lineOffset
665665
else
666-
val candidate = source.nextLine(lineStart) // unused
667666
val commentStart = skipBlanks(lineStart)
668667
if testChar(commentStart, '/') && indentWidth < in.indentWidth(commentStart)
669668
then closingOffset(source.nextLine(lineStart))
@@ -1297,6 +1296,8 @@ object Parsers {
12971296
case _: (ForYield | ForDo) => in.token == FOR
12981297
case _ => false
12991298

1299+
def endName = if in.token == IDENTIFIER then in.name.toString else tokenString(in.token)
1300+
13001301
def matchesAndSetEnd(last: T): Boolean =
13011302
val didMatch = matches(last)
13021303
if didMatch then
@@ -1307,6 +1308,9 @@ object Parsers {
13071308
val start = in.skipToken()
13081309
if stats.isEmpty || !matchesAndSetEnd(stats.last) then
13091310
syntaxError("misaligned end marker", Span(start, in.lastCharOffset))
1311+
else if overlapsPatch(source, Span(start, start)) then
1312+
patch(source, Span(start, start), "")
1313+
patch(source, Span(start, in.lastCharOffset), s"} // end $endName")
13101314
in.token = IDENTIFIER // Leaving it as the original token can confuse newline insertion
13111315
in.nextToken()
13121316
end checkEndMarker
@@ -3846,9 +3850,9 @@ object Parsers {
38463850
def templateStatSeq(): (ValDef, List[Tree]) = checkNoEscapingPlaceholders {
38473851
var self: ValDef = EmptyValDef
38483852
val stats = new ListBuffer[Tree]
3849-
if (isExprIntro && !isDefIntro(modifierTokens)) {
3853+
if isExprIntro && !isDefIntro(modifierTokens) then
38503854
val first = expr1()
3851-
if (in.token == ARROW) {
3855+
if in.token == ARROW then
38523856
first match {
38533857
case Typed(tree @ This(EmptyTypeIdent), tpt) =>
38543858
self = makeSelfDef(nme.WILDCARD, tpt).withSpan(first.span)
@@ -3859,11 +3863,10 @@ object Parsers {
38593863
}
38603864
in.token = SELFARROW // suppresses INDENT insertion after `=>`
38613865
in.nextToken()
3862-
}
38633866
else
38643867
stats += first
38653868
statSepOrEnd(stats)
3866-
}
3869+
end if
38673870
while
38683871
var empty = false
38693872
if (in.token == IMPORT)
@@ -3880,7 +3883,7 @@ object Parsers {
38803883
empty = true
38813884
statSepOrEnd(stats, empty)
38823885
do ()
3883-
(self, if (stats.isEmpty) List(EmptyTree) else stats.toList)
3886+
(self, if stats.isEmpty then List(EmptyTree) else stats.toList)
38843887
}
38853888

38863889
/** RefineStatSeq ::= RefineStat {semi RefineStat}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ object Rewrites {
2020
private[Rewrites] val pbuf = new mutable.ListBuffer[Patch]()
2121

2222
def addPatch(span: Span, replacement: String): Unit =
23-
pbuf += Patch(span, replacement)
23+
pbuf.indexWhere(p => p.span.start == span.start && p.span.end == span.end) match {
24+
case i if i >= 0 => pbuf.update(i, Patch(span, replacement))
25+
case _ => pbuf += Patch(span, replacement)
26+
}
2427

2528
def apply(cs: Array[Char]): Array[Char] = {
2629
val delta = pbuf.map(_.delta).sum
2730
val patches = pbuf.toList.sortBy(_.span.start)
2831
if (patches.nonEmpty)
29-
patches reduceLeft {(p1, p2) =>
32+
patches.reduceLeft {(p1, p2) =>
3033
assert(p1.span.end <= p2.span.start, s"overlapping patches in $source: $p1 and $p2")
3134
p2
3235
}
@@ -64,11 +67,11 @@ object Rewrites {
6467
* given by `span` in `source` by `replacement`
6568
*/
6669
def patch(source: SourceFile, span: Span, replacement: String)(using Context): Unit =
67-
if (ctx.reporter != Reporter.NoReporter) // NoReporter is used for syntax highlighting
68-
for (rewrites <- ctx.settings.rewrite.value)
69-
rewrites.patched
70-
.getOrElseUpdate(source, new Patches(source))
71-
.addPatch(span, replacement)
70+
if ctx.reporter != Reporter.NoReporter // NoReporter is used for syntax highlighting
71+
then ctx.settings.rewrite.value.foreach(_.patched
72+
.getOrElseUpdate(source, new Patches(source))
73+
.addPatch(span, replacement)
74+
)
7275

7376
/** Patch position in `ctx.compilationUnit.source`. */
7477
def patch(span: Span, replacement: String)(using Context): Unit =
@@ -96,6 +99,3 @@ class Rewrites {
9699
import Rewrites._
97100
private val patched = new PatchedFiles
98101
}
99-
100-
101-

compiler/src/dotty/tools/dotc/util/Spans.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ object Spans {
8585
|| containsInner(this, that.end)
8686
|| containsInner(that, this.start)
8787
|| containsInner(that, this.end)
88+
|| this.start == that.start && this.end == that.end // exact match in one point
8889
)
8990
}
9091

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class CompilationTests {
7272
compileFile("tests/rewrites/rewrites3x.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
7373
compileFile("tests/rewrites/i8982.scala", defaultOptions.and("-indent", "-rewrite")),
7474
compileFile("tests/rewrites/i9632.scala", defaultOptions.and("-indent", "-rewrite")),
75-
compileFile("tests/rewrites/i11895.scala", defaultOptions.and("-indent", "-rewrite"))
75+
compileFile("tests/rewrites/i11895.scala", defaultOptions.and("-indent", "-rewrite")),
76+
compileFile("tests/rewrites/i12340.scala", unindentOptions.and("-rewrite")),
7677
).checkRewrites()
7778
}
7879

compiler/test/dotty/tools/vulpix/ParallelTesting.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
978978
target.copy(dir = copyToDir(outDir, dir))
979979
}
980980

981-
val test = new RewriteTest(copiedTargets, checkFileMap, times, threadLimit, shouldFail || shouldSuppressOutput).executeTestSuite()
981+
new RewriteTest(copiedTargets, checkFileMap, times, threadLimit, shouldFail || shouldSuppressOutput).executeTestSuite()
982982
this
983983
}
984984

compiler/test/dotty/tools/vulpix/TestConfiguration.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ object TestConfiguration {
6060

6161
val commonOptions = Array("-indent", "-language:postfixOps") ++ checkOptions ++ noCheckOptions ++ yCheckOptions
6262
val defaultOptions = TestFlags(basicClasspath, commonOptions)
63+
val unindentOptions = TestFlags(basicClasspath, Array("-no-indent") ++ checkOptions ++ noCheckOptions ++ yCheckOptions)
6364
val withCompilerOptions =
6465
defaultOptions.withClasspath(withCompilerClasspath).withRunClasspath(withCompilerClasspath)
6566
lazy val withStagingOptions =

tests/rewrites/i12340.check

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
class C {
3+
def f = 42
4+
} // end C
5+
6+
def f(i: Int) = {
7+
if i < 42 then
8+
println(i)
9+
end if
10+
i
11+
} // end f
12+
13+
def g(i: Int) =
14+
if i < 42 then
15+
println(i)
16+
end if
17+
end g

tests/rewrites/i12340.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
class C:
3+
def f = 42
4+
end C
5+
6+
def f(i: Int) =
7+
if i < 42 then
8+
println(i)
9+
end if
10+
i
11+
end f
12+
13+
def g(i: Int) =
14+
if i < 42 then
15+
println(i)
16+
end if
17+
end g

0 commit comments

Comments
 (0)