Skip to content
This repository was archived by the owner on Sep 3, 2020. It is now read-only.

Commit 9564379

Browse files
committed
Fix return type printing for methods that return Unit
1 parent 6072977 commit 9564379

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

org.scala-refactoring.library/src/main/scala/scala/tools/refactoring/sourcegen/ReusingPrinter.scala

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,9 @@ trait ReusingPrinter extends TreePrintingTraversals with AbstractPrinter {
987987
case _ => false
988988
}
989989

990+
val isAbstract = body == EmptyFragment
990991
val resultType =
991-
if (body == EmptyFragment && !existsTptInFile)
992+
if (isAbstract && !existsTptInFile)
992993
EmptyFragment
993994
else
994995
p(tpt, before = Requisite.allowSurroundingWhitespace(":", ": "))
@@ -1002,19 +1003,30 @@ trait ReusingPrinter extends TreePrintingTraversals with AbstractPrinter {
10021003
}
10031004
}
10041005

1005-
val noEqualNeeded = {
1006-
body == EmptyFragment || rhs.tpe == null || (rhs.tpe != null && rhs.tpe.toString == "Unit")
1007-
}
1008-
1009-
def openingBrace = keepOpeningBrace(tree, tpt, rhs)
1006+
val noEqualNeeded = resultType == EmptyFragment || isAbstract
10101007

10111008
if (noEqualNeeded && !hasEqualInSource) {
10121009
l ++ modsAndName ++ typeParameters ++ parameters ++ resultType ++ body ++ r
10131010
} else {
1014-
l ++ modsAndName ++ typeParameters ++ parameters ++ resultType ++ Requisite.anywhere("=", " = ") ++ openingBrace ++ body ++ r
1011+
val openingBrace = keepOpeningBrace(tree, tpt, rhs)
1012+
// In case a Unit return type is added to a method like `def f {}`, we
1013+
// need to remove the whitespace between name and rhs, otherwise the
1014+
// result would be `def f : Unit = {}`.
1015+
val modsAndName2 =
1016+
if (modsAndName.trailing.asText.trim.isEmpty)
1017+
Fragment(modsAndName.leading, modsAndName.center, NoLayout)
1018+
else
1019+
modsAndName
1020+
1021+
l ++ modsAndName2 ++ typeParameters ++ parameters ++ resultType ++ Requisite.anywhere("=", " = ") ++ openingBrace ++ body ++ r
10151022
}
10161023
}
10171024

1025+
/**
1026+
* In case a definition like `def f = {0}` contains a single expression in
1027+
* braces, we need to find the braces manually because they are no part of
1028+
* the tree.
1029+
*/
10181030
private def keepOpeningBrace(tree: Tree, tpt: Tree, rhs: Tree): String = tpt match {
10191031
case tpt: TypeTree if tpt.original != null && tree.pos != NoPosition && rhs.pos != NoPosition =>
10201032
val OpeningBrace = "(?s).*(\\{.*)".r

org.scala-refactoring.library/src/test/scala/scala/tools/refactoring/tests/sourcegen/ReusingPrinterTest.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,31 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
179179
d.copy(tpt = newTpt) replaces d
180180
}}}
181181

182+
@Test
183+
def add_Unit_return_type_to_def_with_single_expression_in_braces() = """
184+
package add_Unit_return_type_to_def_with_single_expression_in_braces
185+
object X {
186+
def foo {
187+
println
188+
}
189+
def bar {}
190+
def baz = ()
191+
}
192+
""" becomes """
193+
package add_Unit_return_type_to_def_with_single_expression_in_braces
194+
object X {
195+
def foo: Unit = {
196+
println
197+
}
198+
def bar: Unit = {}
199+
def baz: Unit = ()
200+
}
201+
""" after topdown { matchingChildren { transform {
202+
case d @ DefDef(_, _, _, _, tpt: TypeTree, _) =>
203+
val newTpt = tpt setOriginal mkReturn(List(tpt.tpe.typeSymbol))
204+
d.copy(tpt = newTpt) replaces d
205+
}}}
206+
182207
@Test
183208
def add_override_flag() = """
184209
package add_override_flag

0 commit comments

Comments
 (0)