Skip to content

Commit fffccdc

Browse files
authored
Merge pull request #1088 from mkurz/var
Add support for `var`
2 parents 4772ae3 + 67fedf8 commit fffccdc

File tree

8 files changed

+2103
-71
lines changed

8 files changed

+2103
-71
lines changed

build.sbt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ val mimaSettings = Seq(
6060
ProblemFilters.exclude[IncompatibleMethTypeProblem]("play.twirl.parser.TreeNodes#Block.this"),
6161
ProblemFilters.exclude[IncompatibleResultTypeProblem]("play.twirl.parser.TreeNodes#Block.copy$default$3"),
6262
ProblemFilters.exclude[IncompatibleResultTypeProblem]("play.twirl.parser.TreeNodes#Block._3"),
63+
// Add support for var
64+
ProblemFilters.exclude[DirectMissingMethodProblem]("play.twirl.parser.TwirlParser.elseCall"),
65+
ProblemFilters.exclude[DirectMissingMethodProblem]("play.twirl.parser.TwirlParser.elseIfCall"),
66+
ProblemFilters.exclude[DirectMissingMethodProblem]("play.twirl.parser.TwirlParser.ifExpression"),
67+
ProblemFilters.exclude[DirectMissingMethodProblem]("play.twirl.parser.TwirlParser.mixed"),
68+
ProblemFilters.exclude[DirectMissingMethodProblem]("play.twirl.parser.TwirlParser.template"),
69+
ProblemFilters.exclude[DirectMissingMethodProblem]("play.twirl.parser.TwirlParser.templateContent"),
6370
)
6471
)
6572

compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ object TwirlCompiler {
474474
def visit(
475475
elem: collection.Seq[TemplateTree],
476476
previous: collection.Seq[Any],
477-
resultType: String
477+
resultType: Option[String]
478478
): collection.Seq[Any] = {
479479
elem.toList match {
480480
case head :: tail =>
@@ -513,26 +513,39 @@ object TwirlCompiler {
513513
resultType
514514
) :+ "}"
515515
}
516+
case Reassignment(Left(template)) => {
517+
(if (previous.isEmpty) Nil else previous :+ ",") :+ Source(
518+
"{" + template.name + " = _display_{",
519+
template.pos
520+
) :+ templateCode(template, None) :+ Source("}}", template.pos)
521+
}
522+
case Reassignment(Right(variable)) => {
523+
(if (previous.isEmpty) Nil else previous :+ ",") :+ Source(
524+
"{" + variable.name + " = " + variable.code.code + "}",
525+
variable.pos
526+
)
527+
}
516528
},
517529
resultType
518530
)
519531
case Nil => previous
520532
}
521533
}
522534

523-
def templateCode(template: BaseTemplate, resultType: String): collection.Seq[Any] = {
535+
def templateCode(template: BaseTemplate, resultType: Option[String]): collection.Seq[Any] = {
524536
val defs = (template.sub ++ template.members).sortWith((l, r) => l.pos.<(r.pos)).map {
525537
case t: SubTemplate if t.name.toString == "" => templateCode(t, resultType)
526538
case t: SubTemplate => {
527-
Nil :+ (if (t.name.str.startsWith("implicit")) "implicit " else "") :+ (if (t.isVal && t.isLazy) "lazy "
528-
else "") :+ (if (t.isVal) "val "
529-
else "def ") :+ Source(
539+
Nil :+ (if (t.name.str.startsWith("implicit")) "implicit " else "") :+ t.declaration.fold(
540+
isVar => if (isVar) "var " else "def ",
541+
valIsLazy => if (valIsLazy) "lazy val " else "val "
542+
) :+ Source(
530543
t.name.str,
531544
t.name.pos
532545
) :+ Source(
533546
t.params.str,
534547
t.params.pos
535-
) :+ ":" :+ resultType :+ " = {_display_{" :+ templateCode(t, resultType) :+ "}};"
548+
) :+ resultType.map(rt => ":" :+ rt).getOrElse(Nil) :+ " = {_display_{" :+ templateCode(t, resultType) :+ "}};"
536549
}
537550
case Def(name, params, resultType, block) => {
538551
Nil :+ (if (name.str.startsWith("implicit")) "implicit def " else "def ") :+ Source(
@@ -550,6 +563,13 @@ object TwirlCompiler {
550563
name.pos
551564
) :+ resultType.map(":" + _.str).getOrElse("") :+ " = {" :+ block.code :+ "};"
552565
}
566+
case Var(name, resultType, block) => {
567+
Nil :+ (if (name.str.startsWith("implicit")) "implicit var " else "var ") :+ Source(
568+
name.str,
569+
name.pos
570+
) :+ resultType.map(":" + _.str).getOrElse("") :+ " = {" :+ block.code :+ "};"
571+
}
572+
553573
}
554574

555575
val imports = formatImports(template.imports)
@@ -611,7 +631,7 @@ package """ :+ packageName :+ """
611631
def apply""" :+ Source(root.params.str, root.params.pos) :+ """:""" :+ resultType :+ """ = {
612632
_display_ {
613633
{
614-
""" :+ templateCode(root, resultType) :+ """
634+
""" :+ templateCode(root, Some(resultType)) :+ """
615635
}
616636
}
617637
}
@@ -835,6 +855,7 @@ object Source {
835855
lines: ListBuffer[(Int, Int)]
836856
): Unit = {
837857
parts.foreach {
858+
case c: Character => source.append(c)
838859
case s: String => source.append(s)
839860
case Source(code, pos @ OffsetPosition(_, offset)) => {
840861
source.append("/*" + pos + "*/")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@****************************************************************************************************************************************************
2+
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com> *
3+
****************************************************************************************************************************************************@
4+
5+
@(condition: Integer, secondCondition: Integer)
6+
@var field = @{ "bar1 " }
7+
@if(condition == 1) {
8+
@field
9+
@field = @{ "bar2 " }
10+
@field
11+
@if(secondCondition == 1) {
12+
@field
13+
@field = @{ "bar5 " }
14+
@field
15+
}
16+
@field
17+
} else if(condition == 2) {
18+
@field
19+
@field = @{ "bar3 " }
20+
@field
21+
@if(secondCondition == 1) {
22+
@field
23+
@field = @{ "bar6 " }
24+
@field
25+
}
26+
@field
27+
} else {
28+
@field
29+
@field = @{ "bar4 " }
30+
@field
31+
@if(secondCondition == 1) {
32+
@field
33+
@field = @{ "bar7 " }
34+
@field
35+
}
36+
@field
37+
}
38+
@field
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@****************************************************************************************************************************************************
2+
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com> *
3+
****************************************************************************************************************************************************@
4+
5+
@(condition: Integer, secondCondition: Integer)
6+
@var field = { bar1 }
7+
@if(condition == 1) {
8+
@field
9+
@field = { bar2 }
10+
@field
11+
@if(secondCondition == 1) {
12+
@field
13+
@field = { bar5 }
14+
@field
15+
}
16+
@field
17+
} else if(condition == 2) {
18+
@field
19+
@field = { bar3 }
20+
@field
21+
@if(secondCondition == 1) {
22+
@field
23+
@field = { bar6 }
24+
@field
25+
}
26+
@field
27+
} else {
28+
@field
29+
@field = { bar4 }
30+
@field
31+
@if(secondCondition == 1) {
32+
@field
33+
@field = { bar7 }
34+
@field
35+
}
36+
@field
37+
}
38+
@field

0 commit comments

Comments
 (0)