Skip to content

Commit 998de24

Browse files
Merge pull request #4689 from dotty-staging/add-colon-to-statement
Print double breaks after statements to avoid ambiguity
2 parents 95ec081 + 89899b7 commit 998de24

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
lines changed

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
3838
def result(): String = sb.result()
3939

4040
def lineBreak(): String = "\n" + (" " * indent)
41+
def doubleLineBreak(): String = "\n\n" + (" " * indent)
4142

4243
def printTree(tree: Tree): Buffer = tree match {
4344
case tree @ PackageClause(Term.Ident(name), stats) =>
@@ -191,8 +192,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
191192
case stats =>
192193
this += "{"
193194
indented {
194-
this += lineBreak()
195-
printTrees(stats, lineBreak())
195+
printStats(stats.init, stats.last)
196196
}
197197
this += lineBreak() += "}"
198198
}
@@ -205,8 +205,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
205205
case stats =>
206206
this += "{"
207207
indented {
208-
this += lineBreak()
209-
printTrees(stats, lineBreak())
208+
printStats(stats.init, stats.last)
210209
}
211210
this += lineBreak() += "}"
212211
}
@@ -333,28 +332,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
333332
this += ")"
334333
case _ =>
335334
this += "{"
335+
val (stats1, expr1) =
336+
if (isLoopEntryPoint(expr)) (stats.init, stats.last)
337+
else (stats, expr)
336338
indented {
337-
if (!stats.isEmpty) {
338-
this += lineBreak()
339-
printTrees(stats, lineBreak())
340-
}
341-
if (!isLoopEntryPoint(expr)) {
342-
this += lineBreak()
343-
printTree(expr)
344-
}
339+
printStats(stats1, expr1)
345340
}
346341
this += lineBreak() += "}"
347342
}
348343

349344
case Term.Inlined(call, bindings, expansion) =>
350-
sb.append("{ // inlined")
345+
this += "{ // inlined"
351346
indented {
352-
if (!bindings.isEmpty) {
353-
this += lineBreak()
354-
printTrees(bindings, lineBreak())
355-
}
356-
this += lineBreak()
357-
printTree(expansion)
347+
printStats(bindings, expansion)
358348
}
359349
this += lineBreak() += "}"
360350

@@ -406,6 +396,30 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
406396

407397
}
408398

399+
def printStats(stats: List[Tree], expr: Tree): Unit = {
400+
def printSeparator(nextStats: List[Tree]) = {
401+
// Avoid accidental application of opening `{` on next line with a double break
402+
val next = if (nextStats.isEmpty) expr else nextStats.head
403+
next match {
404+
case Term.Block(DefDef("while$" | "doWhile$", _, _, _, _) :: Nil, _) => this += lineBreak()
405+
case Term.Block(_, _) => this += doubleLineBreak()
406+
case Term.Inlined(_, _, _) => this += doubleLineBreak()
407+
case _ => this += lineBreak()
408+
}
409+
}
410+
def printSeparated(list: List[Tree]): Unit = list match {
411+
case Nil =>
412+
printTree(expr)
413+
case x :: xs =>
414+
printTree(x)
415+
printSeparator(xs)
416+
printSeparated(xs)
417+
}
418+
419+
this += lineBreak()
420+
printSeparated(stats)
421+
}
422+
409423
def printTrees(trees: List[Tree], sep: String): Buffer = {
410424
def printSeparated(list: List[Tree]): Unit = list match {
411425
case Nil =>
@@ -609,14 +623,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
609623
}
610624
this += " =>"
611625
indented {
612-
this += lineBreak()
613626
body match {
614627
case Term.Block(stats, expr) =>
615-
printTrees(stats, lineBreak())
616-
if (stats.nonEmpty)
617-
this += lineBreak()
618-
printTree(expr)
628+
printStats(stats, expr)
619629
case body =>
630+
this += lineBreak()
620631
printTree(body)
621632
}
622633
}

tests/pos/simpleDoWhile.decompiled

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ class Foo() {
66
i = 0
77
} while (i.!=(0))
88
}
9-
}
9+
}

tests/run-with-compiler/i3876-d.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
6
22
{
33
val x$1: scala.Int = 3
4+
45
{ // inlined
56
x$1.+(x$1)
67
}

tests/run-with-compiler/quote-show-blocks-raw.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{
22
scala.Predef.println(1)
3+
34
{
45
scala.Predef.println(2)
6+
57
{
68
scala.Predef.println(3)
9+
710
{
811
scala.Predef.println(4)
12+
913
{
1014
scala.Predef.println(5)
1115
()

0 commit comments

Comments
 (0)