Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.6.x] Support >22 args in Scala 3 (backport #747) by @mkurz #748

Merged
merged 6 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ object TwirlCompiler {
private[compiler] class ScalaCompat(emitScala3Sources: Boolean) {
val varargSplicesSyntax: String =
if (emitScala3Sources) "*" else ": _*"
def valueOrEmptyIfScala3(value: => String): String =
if (emitScala3Sources) "" else value
}

private[compiler] object ScalaCompat {
Expand Down Expand Up @@ -574,7 +576,8 @@ class """ :+ name :+ " " :+ constructorAnnotations :+ " " :+ Source(constructor.
package """ :+ packageName :+ """

""" :+ imports :+ """
""" :+ classDeclaration :+ """ extends _root_.play.twirl.api.BaseScalaTemplate[""" :+ resultType :+ """,_root_.play.twirl.api.Format[""" :+ resultType :+ """]](""" :+ formatterType :+ """) with """ :+ templateType :+ """ {
""" :+ classDeclaration :+ """ extends _root_.play.twirl.api.BaseScalaTemplate[""" :+ resultType :+ """,_root_.play.twirl.api.Format[""" :+ resultType :+ """]](""" :+ formatterType :+ """)""" :+ scalaCompat
.valueOrEmptyIfScala3(s" with $templateType") :+ """ {

/*""" :+ root.comment.map(_.msg).getOrElse("") :+ """*/
def apply""" :+ Source(root.params.str, root.params.pos) :+ """:""" :+ resultType :+ """ = {
Expand Down Expand Up @@ -732,15 +735,17 @@ package """ :+ packageName :+ """
.mkString
)

val templateType = "_root_.play.twirl.api.Template%s[%s%s]".format(
params.flatten.size,
params.flatten
.map {
case ByNameParam(_, paramType) => paramType
case p => filterType(p)
}
.mkString(","),
(if (params.flatten.isEmpty) "" else ",") + returnType
val templateType = sc.valueOrEmptyIfScala3(
"_root_.play.twirl.api.Template%s[%s%s]".format(
params.flatten.size,
params.flatten
.map {
case ByNameParam(_, paramType) => paramType
case p => filterType(p)
}
.mkString(","),
(if (params.flatten.isEmpty) "" else ",") + returnType
)
)

val f = "def f:%s = %s => apply%s".format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ object Test extends App {
test(a.b.html.c.render("world"), "Hello, world.\n")

test(html.template.render("42"), "Answer: 42\n")

test(
html.manyargstemplate.render(
"1", "2", "3", "5", "4", "7", "6", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"),
"26 args: 1 2 3 5 4 7 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26\n"
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@****************************************************************************************************************************************************
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com> *
****************************************************************************************************************************************************@

@(a: String, b: String, c: String, d: String, e: String, f: String, g: String, h: String, i: String, j: String, k: String, l: String, m: String, n: String, o: String, p: String, q: String, r: String, s: String, t: String, u: String, v: String, w: String, x: String, y: String, z: String)
26 args: @a @b @c @d @e @f @g @h @i @j @k @l @m @n @o @p @q @r @s @t @u @v @w @x @y @z
Loading