Skip to content

Commit d5d991d

Browse files
committed
Print double breaks after statements to avoid ambiguity
For example, the first snippet is `foo.apply(...)` and the second is eval `foo` and then eval the block. ```scala foo { ... } ``` ```scala foo { ... } ```
1 parent 1f523ea commit d5d991d

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
@@ -191,8 +191,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
191191
case stats =>
192192
this += "{"
193193
indented {
194-
this += lineBreak()
195-
printTrees(stats, lineBreak())
194+
printStats(stats.init, stats.last)
196195
}
197196
this += lineBreak() += "}"
198197
}
@@ -205,8 +204,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
205204
case stats =>
206205
this += "{"
207206
indented {
208-
this += lineBreak()
209-
printTrees(stats, lineBreak())
207+
printStats(stats.init, stats.last)
210208
}
211209
this += lineBreak() += "}"
212210
}
@@ -327,28 +325,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
327325
this += ")"
328326
case _ =>
329327
this += "{"
328+
val (stats1, expr1) =
329+
if (isLoopEntryPoint(expr)) (stats.init, stats.last)
330+
else (stats, expr)
330331
indented {
331-
if (!stats.isEmpty) {
332-
this += lineBreak()
333-
printTrees(stats, lineBreak())
334-
}
335-
if (!isLoopEntryPoint(expr)) {
336-
this += lineBreak()
337-
printTree(expr)
338-
}
332+
printStats(stats1, expr1)
339333
}
340334
this += lineBreak() += "}"
341335
}
342336

343337
case Term.Inlined(call, bindings, expansion) =>
344-
sb.append("{ // inlined")
338+
this += "{ // inlined"
345339
indented {
346-
if (!bindings.isEmpty) {
347-
this += lineBreak()
348-
printTrees(bindings, lineBreak())
349-
}
350-
this += lineBreak()
351-
printTree(expansion)
340+
printStats(bindings, expansion)
352341
}
353342
this += lineBreak() += "}"
354343

@@ -400,6 +389,31 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
400389

401390
}
402391

392+
def printStats(stats: List[Tree], expr: Tree): Unit = {
393+
def printSeparator(nextStats: List[Tree]) = {
394+
this += lineBreak()
395+
// Avoid accidental application of opening `{` on next line with a double break
396+
val next = if (nextStats.isEmpty) expr else nextStats.head
397+
next match {
398+
case Term.Block(DefDef("while$" | "doWhile$", _, _, _, _) :: Nil, _) =>
399+
case Term.Block(_, _) => this += lineBreak()
400+
case Term.Inlined(_, _, _) => this += lineBreak()
401+
case _ =>
402+
}
403+
}
404+
def printSeparated(list: List[Tree]): Unit = list match {
405+
case Nil =>
406+
printTree(expr)
407+
case x :: xs =>
408+
printTree(x)
409+
printSeparator(xs)
410+
printSeparated(xs)
411+
}
412+
413+
this += lineBreak()
414+
printSeparated(stats)
415+
}
416+
403417
def printTrees(trees: List[Tree], sep: String): Buffer = {
404418
def printSeparated(list: List[Tree]): Unit = list match {
405419
case Nil =>
@@ -603,14 +617,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
603617
}
604618
this += " =>"
605619
indented {
606-
this += lineBreak()
607620
body match {
608621
case Term.Block(stats, expr) =>
609-
printTrees(stats, lineBreak())
610-
if (stats.nonEmpty)
611-
this += lineBreak()
612-
printTree(expr)
622+
printStats(stats, expr)
613623
case body =>
624+
this += lineBreak()
614625
printTree(body)
615626
}
616627
}

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)