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

Commit 6072977

Browse files
committed
Generate return type for defs with single expressions in braces
In case def foo = { 0 } should be transformed to def foo: Int = { 0 } by adding its return type, the result was def foo: Int = 0 } Fixes #1002268
1 parent 6370a4a commit 6072977

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,10 +921,12 @@ trait ReusingPrinter extends TreePrintingTraversals with AbstractPrinter {
921921
val body = p(rhs)
922922
val noEqualNeeded = body == EmptyFragment || rhs.tpe == null || (rhs.tpe != null && rhs.tpe.toString == "Unit")
923923

924+
def openingBrace = keepOpeningBrace(tree, tpt, rhs)
925+
924926
if (noEqualNeeded)
925927
l ++ mods_ ++ resultType ++ body ++ r
926928
else
927-
l ++ mods_ ++ resultType ++ Requisite.anywhere("=", " = ") ++ body ++ r
929+
l ++ mods_ ++ resultType ++ Requisite.anywhere("=", " = ") ++ openingBrace ++ body ++ r
928930
}
929931
}
930932

@@ -1004,12 +1006,25 @@ trait ReusingPrinter extends TreePrintingTraversals with AbstractPrinter {
10041006
body == EmptyFragment || rhs.tpe == null || (rhs.tpe != null && rhs.tpe.toString == "Unit")
10051007
}
10061008

1009+
def openingBrace = keepOpeningBrace(tree, tpt, rhs)
1010+
10071011
if (noEqualNeeded && !hasEqualInSource) {
10081012
l ++ modsAndName ++ typeParameters ++ parameters ++ resultType ++ body ++ r
10091013
} else {
1010-
l ++ modsAndName ++ typeParameters ++ parameters ++ resultType ++ Requisite.anywhere("=", " = ") ++ body ++ r
1014+
l ++ modsAndName ++ typeParameters ++ parameters ++ resultType ++ Requisite.anywhere("=", " = ") ++ openingBrace ++ body ++ r
10111015
}
10121016
}
1017+
1018+
private def keepOpeningBrace(tree: Tree, tpt: Tree, rhs: Tree): String = tpt match {
1019+
case tpt: TypeTree if tpt.original != null && tree.pos != NoPosition && rhs.pos != NoPosition =>
1020+
val OpeningBrace = "(?s).*(\\{.*)".r
1021+
Layout(tree.pos.source, tree.pos.point, rhs.pos.start).asText match {
1022+
case OpeningBrace(brace) => brace
1023+
case _ => ""
1024+
}
1025+
case _ =>
1026+
""
1027+
}
10131028
}
10141029

10151030
trait SuperPrinters {

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,94 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
9191
d.copy(tpt = newTpt) replaces d
9292
}}}
9393

94+
@Test
95+
def add_return_type_to_val_with_single_expression_in_braces() = """
96+
package add_return_type_to_val_with_single_expression_in_braces
97+
object X {
98+
val foo = {
99+
0
100+
}
101+
}
102+
""" becomes """
103+
package add_return_type_to_val_with_single_expression_in_braces
104+
object X {
105+
val foo: Int = {
106+
0
107+
}
108+
}
109+
""" after topdown { matchingChildren { transform {
110+
case d @ ValDef(_, _, tpt: TypeTree, _) =>
111+
val newTpt = tpt setOriginal mkReturn(List(tpt.tpe.typeSymbol))
112+
d.copy(tpt = newTpt) replaces d
113+
}}}
114+
115+
@Test
116+
def add_return_type_to_val_with_multiple_expressions_in_braces() = """
117+
package add_return_type_to_val_with_multiple_expressions_in_braces
118+
object X {
119+
val foo = {
120+
val a = 0
121+
a
122+
}
123+
}
124+
""" becomes """
125+
package add_return_type_to_val_with_multiple_expressions_in_braces
126+
object X {
127+
val foo: Int = {
128+
val a: Int = 0
129+
a
130+
}
131+
}
132+
""" after topdown { matchingChildren { transform {
133+
case d @ ValDef(_, _, tpt: TypeTree, _) =>
134+
val newTpt = tpt setOriginal mkReturn(List(tpt.tpe.typeSymbol))
135+
d.copy(tpt = newTpt) replaces d
136+
}}}
137+
138+
@Test
139+
def add_return_type_to_def_with_single_expression_in_braces() = """
140+
package add_return_type_to_def_with_single_expression_in_braces
141+
object X {
142+
def foo = {
143+
0
144+
}
145+
}
146+
""" becomes """
147+
package add_return_type_to_def_with_single_expression_in_braces
148+
object X {
149+
def foo: Int = {
150+
0
151+
}
152+
}
153+
""" after topdown { matchingChildren { transform {
154+
case d @ DefDef(_, _, _, _, tpt: TypeTree, _) =>
155+
val newTpt = tpt setOriginal mkReturn(List(tpt.tpe.typeSymbol))
156+
d.copy(tpt = newTpt) replaces d
157+
}}}
158+
159+
@Test
160+
def add_return_type_to_def_with_multiple_expressions_in_braces() = """
161+
package add_return_type_to_def_with_multiple_expressions_in_braces
162+
object X {
163+
def foo = {
164+
def a = 0
165+
a
166+
}
167+
}
168+
""" becomes """
169+
package add_return_type_to_def_with_multiple_expressions_in_braces
170+
object X {
171+
def foo: Int = {
172+
def a: Int = 0
173+
a
174+
}
175+
}
176+
""" after topdown { matchingChildren { transform {
177+
case d @ DefDef(_, _, _, _, tpt: TypeTree, _) =>
178+
val newTpt = tpt setOriginal mkReturn(List(tpt.tpe.typeSymbol))
179+
d.copy(tpt = newTpt) replaces d
180+
}}}
181+
94182
@Test
95183
def add_override_flag() = """
96184
package add_override_flag

0 commit comments

Comments
 (0)