Skip to content

Commit daea935

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 daea935

File tree

4 files changed

+42
-24
lines changed

4 files changed

+42
-24
lines changed

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

Lines changed: 36 additions & 23 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,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
205204
case stats =>
206205
this += "{"
207206
indented {
207+
printStats(stats.init, stats.last)
208208
this += lineBreak()
209-
printTrees(stats, lineBreak())
209+
printTree(stats.last)
210210
}
211211
this += lineBreak() += "}"
212212
}
@@ -327,28 +327,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
327327
this += ")"
328328
case _ =>
329329
this += "{"
330+
val (stats1, expr1) =
331+
if (isLoopEntryPoint(expr)) (stats.init, stats.last)
332+
else (stats, expr)
330333
indented {
331-
if (!stats.isEmpty) {
332-
this += lineBreak()
333-
printTrees(stats, lineBreak())
334-
}
335-
if (!isLoopEntryPoint(expr)) {
336-
this += lineBreak()
337-
printTree(expr)
338-
}
334+
printStats(stats1, expr1)
339335
}
340336
this += lineBreak() += "}"
341337
}
342338

343339
case Term.Inlined(call, bindings, expansion) =>
344-
sb.append("{ // inlined")
340+
this += "{ // inlined"
345341
indented {
346-
if (!bindings.isEmpty) {
347-
this += lineBreak()
348-
printTrees(bindings, lineBreak())
349-
}
350-
this += lineBreak()
351-
printTree(expansion)
342+
printStats(bindings, expansion)
352343
}
353344
this += lineBreak() += "}"
354345

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

401392
}
402393

394+
def printStats(stats: List[Tree], expr: Tree): Unit = {
395+
def printSeparator(nextStats: List[Tree]) = {
396+
this += lineBreak()
397+
// Avoid accidental application of opening `{` on next line with a double break
398+
val next = if (nextStats.isEmpty) expr else nextStats.head
399+
next match {
400+
case Term.Block(DefDef("while$" | "doWhile$", _, _, _, _) :: Nil, _) =>
401+
case Term.Block(_, _) => this += lineBreak()
402+
case Term.Inlined(_, _, _) => this += lineBreak()
403+
case _ =>
404+
}
405+
}
406+
def printSeparated(list: List[Tree]): Unit = list match {
407+
case Nil =>
408+
printTree(expr)
409+
case x :: xs =>
410+
printTree(x)
411+
printSeparator(xs)
412+
printSeparated(xs)
413+
}
414+
415+
this += lineBreak()
416+
printSeparated(stats)
417+
}
418+
403419
def printTrees(trees: List[Tree], sep: String): Buffer = {
404420
def printSeparated(list: List[Tree]): Unit = list match {
405421
case Nil =>
@@ -603,14 +619,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
603619
}
604620
this += " =>"
605621
indented {
606-
this += lineBreak()
607622
body match {
608623
case Term.Block(stats, expr) =>
609-
printTrees(stats, lineBreak())
610-
if (stats.nonEmpty)
611-
this += lineBreak()
612-
printTree(expr)
624+
printStats(stats, expr)
613625
case body =>
626+
this += lineBreak()
614627
printTree(body)
615628
}
616629
}

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)