Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
88688a8
Add Scala 3 to cross build
tototoshi Jan 22, 2022
8f82bc2
Add a testcase for call-by-name parameters
tototoshi Jan 25, 2022
6bcb772
Use scalameta in TemplateAsFunctionCompiler
tototoshi Jan 25, 2022
88eb6da
Make CompilerSpec compatible with Scala3
tototoshi Jan 29, 2022
47259b7
Make @if expression work in Scala3
tototoshi Jan 30, 2022
f10f3da
Fix error reporting
tototoshi Jan 30, 2022
a10afc4
Merge remote-tracking branch 'playframework/main' into scalameta
tototoshi Jan 30, 2022
7747deb
Add a test case for if with brackets
tototoshi Jan 30, 2022
f6e583a
Merge remote-tracking branch 'playframework/main' into scalameta
tototoshi Jan 30, 2022
c95e169
Add fix for By-name parameter
tototoshi Jan 30, 2022
bce69ea
Change scalacOptions for each version
tototoshi Jan 30, 2022
3d6185f
sbt-scalajs, scalajs-compiler, ... 1.8.0 (was 1.7.1)
scala-steward Dec 11, 2021
1efefac
feat(scala3): WIP working on scala 3 cross compilation. #390.
brbrown25 Nov 19, 2021
5071500
Add Scala 3 to cross build
tototoshi Jan 22, 2022
df091ff
Use scalameta in TemplateAsFunctionCompiler
tototoshi Jan 25, 2022
47ebbc8
WIP tweaking after merging tototoshi branch
brbrown25 Jan 26, 2022
096b082
Merge remote-tracking branch 'tototoshi/scalameta' into ISSUE-390
brbrown25 Jan 31, 2022
de50d09
feat(scala3): Formating and cleanup after integrating tototoshi branc…
brbrown25 Jan 31, 2022
e8b6ceb
Refactored `ifExpression`
tototoshi Jan 31, 2022
05b73c5
Add comment for ifExpression
tototoshi Jan 31, 2022
e141ce1
Merge remote-tracking branch 'tototoshi/scalameta' into ISSUE-390
brbrown25 Jan 31, 2022
e02f4e1
chore(TwirlParser): fixing format check failure.
brbrown25 Jan 31, 2022
cb862d5
chore(build.sbt, TwirlParser): Adding Mima filters and tweaking to re…
brbrown25 Feb 1, 2022
d432c66
Removed onChangeBuildSource setting to align with other Play reposito…
tototoshi Mar 10, 2022
909f11e
Remove unused code
tototoshi Mar 10, 2022
63be771
Replace TRAVIS_SCALA_VERSION with SCALA_VERSION
tototoshi Mar 10, 2022
4174fe4
chore(build.sbt): removing the scripted change as it is not needed. #…
brbrown25 Mar 10, 2022
e892f83
Suppress compile errors log during running CompilerSpec
tototoshi Mar 11, 2022
2a3898f
chore(travis.yml): updating the scala version from 3.1.0 to 3.1.1 in …
brbrown25 Mar 16, 2022
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
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ jobs:
- SCALA_VERSION=2.13.8
- ADOPTOPENJDK=11

- name: "Run tests with Scala 3 and AdoptOpenJDK 11"
script: scripts/test-code.sh
env:
- SCALA_VERSION=3.1.1
- ADOPTOPENJDK=11

- name: "Run tests with Scala 2.12 and AdoptOpenJDK 8"
script: scripts/test-code.sh
env:
Expand All @@ -40,6 +46,12 @@ jobs:
- SCALA_VERSION=2.13.8
- ADOPTOPENJDK=8

- name: "Run tests with Scala 3 and AdoptOpenJDK 8"
script: scripts/test-code.sh
env:
- SCALA_VERSION=3.1.1
- ADOPTOPENJDK=8

- stage: docs
script: scripts/validate-docs.sh
name: "Validate documentation"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) Lightbend Inc. <https://www.lightbend.com>
*/
package play.twirl.api

import java.util.Optional
import scala.collection.immutable
import scala.jdk.CollectionConverters._
import scala.reflect.ClassTag

case class BaseScalaTemplate[T <: Appendable[T], F <: Format[T]](format: F) {
// The overloaded methods are here for speed. The compiled templates
// can take advantage of them for a 12% performance boost
def _display_(x: AnyVal): T = format.escape(x.toString)
def _display_(x: String): T = if (x eq null) format.empty else format.escape(x)
def _display_(x: Unit): T = format.empty
def _display_(x: scala.xml.NodeSeq): T = if (x eq null) format.empty else format.raw(x.toString())
def _display_(x: T): T = if (x eq null) format.empty else x

def _display_(o: Any)(implicit m: ClassTag[T]): T = {
o match {
case escaped if escaped != null && escaped.getClass == m.runtimeClass => escaped.asInstanceOf[T]
case () => format.empty
case None => format.empty
case Some(v) => _display_(v)
case key: Optional[_] =>
(if (key.isPresent) Some(key.get) else None) match {
case None => format.empty
case Some(v) => _display_(v)
case _ => format.empty
}
case xml: scala.xml.NodeSeq => format.raw(xml.toString())
case escapeds: immutable.Seq[_] => format.fill(escapeds.map(_display_))
case escapeds: IterableOnce[_] => format.fill(escapeds.iterator.map(_display_).toList)
case escapeds: Array[_] => format.fill(escapeds.view.map(_display_).toList)
case escapeds: java.util.List[_] =>
format.fill(escapeds.asScala.map(_display_).toList)
case string: String => format.escape(string)
case v if v != null => format.escape(v.toString)
case _ => format.empty
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) Lightbend Inc. <https://www.lightbend.com>
*/
package play.twirl.api

import scala.language.implicitConversions

/**
* Imports for useful Twirl helpers.
*/
object TwirlHelperImports {

/** Allows Java collections to be used as if they were Scala collections. */
implicit def twirlJavaCollectionToScala[T](x: java.lang.Iterable[T]): Iterable[T] = {
import scala.jdk.CollectionConverters._
x.asScala
}

/** Allows inline formatting of java.util.Date */
implicit class TwirlRichDate(date: java.util.Date) {
def format(pattern: String): String = {
new java.text.SimpleDateFormat(pattern).format(date)
}
}

/** Adds a when method to Strings to control when they are rendered. */
implicit class TwirlRichString(string: String) {
def when(predicate: => Boolean): String = {
predicate match {
case true => string
case false => ""
}
}
}
}
47 changes: 47 additions & 0 deletions api/shared/src/main/scala-2.13/play/twirl/api/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) Lightbend Inc. <https://www.lightbend.com>
*/
package play.twirl

import scala.reflect.ClassTag

package object api {

/**
* Brings the template engine as a
* [[http://docs.scala-lang.org/overviews/core/string-interpolation.html string interpolator]].
*
* Basic usage:
*
* {{{
* import play.twirl.api.StringInterpolation
*
* val name = "Martin"
* val htmlFragment: Html = html"&lt;div&gt;Hello \$name&lt;/div&gt;"
* }}}
*
* Three interpolators are available: `html`, `xml` and `js`.
*/
implicit class StringInterpolation(val sc: StringContext) extends AnyVal {
def html(args: Any*): Html = interpolate(args, HtmlFormat)

def xml(args: Any*): Xml = interpolate(args, XmlFormat)

def js(args: Any*): JavaScript = interpolate(args, JavaScriptFormat)

def interpolate[A <: Appendable[A]: ClassTag](args: Seq[Any], format: Format[A]): A = {
StringContext.checkLengths(args, sc.parts)
val array = Array.ofDim[Any](args.size + sc.parts.size)
val strings = sc.parts.iterator
val expressions = args.iterator
array(0) = format.raw(strings.next())
var i = 1
while (strings.hasNext) {
array(i) = expressions.next()
array(i + 1) = format.raw(strings.next())
i += 2
}
new BaseScalaTemplate[A, Format[A]](format)._display_(array)
}
}
}
43 changes: 43 additions & 0 deletions api/shared/src/main/scala-3/play/twirl/api/BaseScalaTemplate.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) Lightbend Inc. <https://www.lightbend.com>
*/
package play.twirl.api

import java.util.Optional
import scala.collection.immutable
import scala.jdk.CollectionConverters._
import scala.reflect.ClassTag

case class BaseScalaTemplate[T <: Appendable[T], F <: Format[T]](format: F) {
// The overloaded methods are here for speed. The compiled templates
// can take advantage of them for a 12% performance boost
def _display_(x: AnyVal): T = format.escape(x.toString)
def _display_(x: String): T = if (x eq null) format.empty else format.escape(x)
def _display_(x: Unit): T = format.empty
def _display_(x: scala.xml.NodeSeq): T = if (x eq null) format.empty else format.raw(x.toString())
def _display_(x: T): T = if (x eq null) format.empty else x

def _display_(o: Any)(implicit m: ClassTag[T]): T = {
o match {
case escaped if escaped != null && escaped.getClass == m.runtimeClass => escaped.asInstanceOf[T]
case () => format.empty
case None => format.empty
case Some(v) => _display_(v)
case key: Optional[_] =>
(if (key.isPresent) Some(key.get) else None) match {
case None => format.empty
case Some(v) => _display_(v)
case null => format.empty
}
case xml: scala.xml.NodeSeq => format.raw(xml.toString())
case escapeds: immutable.Seq[_] => format.fill(escapeds.map(_display_))
case escapeds: IterableOnce[_] => format.fill(escapeds.iterator.map(_display_).toList)
case escapeds: Array[_] => format.fill(escapeds.view.map(_display_).toList)
case escapeds: java.util.List[_] =>
format.fill(escapeds.asScala.map(_display_).toList)
case string: String => format.escape(string)
case v if v != null => format.escape(v.toString)
case null => format.empty
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) Lightbend Inc. <https://www.lightbend.com>
*/
package play.twirl.api

import scala.language.implicitConversions

/**
* Imports for useful Twirl helpers.
*/
object TwirlHelperImports {

/** Allows Java collections to be used as if they were Scala collections. */
implicit def twirlJavaCollectionToScala[T](x: java.lang.Iterable[T]): Iterable[T] = {
import scala.jdk.CollectionConverters._
x.asScala
}

/** Allows inline formatting of java.util.Date */
implicit class TwirlRichDate(date: java.util.Date) {
def format(pattern: String): String = {
new java.text.SimpleDateFormat(pattern).format(date)
}
}

/** Adds a when method to Strings to control when they are rendered. */
implicit class TwirlRichString(string: String) {
def when(predicate: => Boolean): String = {
predicate match {
case true => string
case false => ""
}
}
}
}
47 changes: 47 additions & 0 deletions api/shared/src/main/scala-3/play/twirl/api/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) Lightbend Inc. <https://www.lightbend.com>
*/
package play.twirl

import scala.reflect.ClassTag

package object api {

/**
* Brings the template engine as a
* [[http://docs.scala-lang.org/overviews/core/string-interpolation.html string interpolator]].
*
* Basic usage:
*
* {{{
* import play.twirl.api.StringInterpolation
*
* val name = "Martin"
* val htmlFragment: Html = html"&lt;div&gt;Hello \$name&lt;/div&gt;"
* }}}
*
* Three interpolators are available: `html`, `xml` and `js`.
*/
implicit class StringInterpolation(val sc: StringContext) extends AnyVal {
def html(args: Any*): Html = interpolate(args, HtmlFormat)

def xml(args: Any*): Xml = interpolate(args, XmlFormat)

def js(args: Any*): JavaScript = interpolate(args, JavaScriptFormat)

def interpolate[A <: Appendable[A]: ClassTag](args: Seq[Any], format: Format[A]): A = {
StringContext.checkLengths(args, sc.parts)
val array = Array.ofDim[Any](args.size + sc.parts.size)
val strings = sc.parts.iterator
val expressions = args.iterator
array(0) = format.raw(strings.next())
var i = 1
while (strings.hasNext) {
array(i) = expressions.next()
array(i + 1) = format.raw(strings.next())
i += 2
}
new BaseScalaTemplate[A, Format[A]](format)._display_(array)
}
}
}
47 changes: 39 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Dependencies._

import sbtcrossproject.crossProject
import com.typesafe.tools.mima.core.IncompatibleMethTypeProblem
import com.typesafe.tools.mima.core.MissingClassProblem
import com.typesafe.tools.mima.core.Problem
import com.typesafe.tools.mima.core.ProblemFilters
import sbtcrossproject.CrossPlugin.autoImport.crossProject
import org.scalajs.jsenv.nodejs.NodeJSEnv

// Binary compatibility is this version
Expand All @@ -16,7 +20,18 @@ def parserCombinators(scalaVersion: String) = "org.scala-lang.modules" %% "scala
}
Comment thread
brbrown25 marked this conversation as resolved.
Resolved

val mimaSettings = Seq(
mimaPreviousArtifacts := previousVersion.map(organization.value %% name.value % _).toSet
mimaPreviousArtifacts := {
CrossVersion.partialVersion(scalaVersion.value) match {
// No release for Scala 3 yet
case Some((3, _)) => Set.empty
case _ => previousVersion.map(organization.value %% name.value % _).toSet
}
},
mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[Problem]("play.twirl.parser.*"),
ProblemFilters.exclude[MissingClassProblem]("play.twirl.compiler.*"),
ProblemFilters.exclude[IncompatibleMethTypeProblem]("play.twirl.compiler.*"),
)
)

ThisBuild / sonatypeProfileName := "com.typesafe.play"
Expand Down Expand Up @@ -56,6 +71,8 @@ lazy val api = crossProject(JVMPlatform, JSPlatform)
.enablePlugins(Common, Playdoc, Omnidoc)
.configs(Docs)
.settings(
scalaVersion := Scala212,
crossScalaVersions := ScalaVersions,
mimaSettings,
name := "twirl-api",
jsEnv := nodeJs,
Expand Down Expand Up @@ -83,6 +100,8 @@ lazy val parser = project
.in(file("parser"))
.enablePlugins(Common, Omnidoc)
.settings(
scalaVersion := Scala212,
crossScalaVersions := ScalaVersions,
mimaSettings,
name := "twirl-parser",
libraryDependencies += parserCombinators(scalaVersion.value) % Optional,
Expand All @@ -93,14 +112,25 @@ lazy val parser = project
lazy val compiler = project
.in(file("compiler"))
.enablePlugins(Common, Omnidoc)
.dependsOn(apiJvm, parser % "compile;test->test")
.settings(
scalaVersion := Scala212,
crossScalaVersions := ScalaVersions,
mimaSettings,
name := "twirl-compiler",
libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value,
libraryDependencies += parserCombinators(scalaVersion.value) % "optional",
run / fork := true,
name := "twirl-compiler",
libraryDependencies ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, _)) =>
// only for scala < 3
Seq("org.scala-lang" % "scala-compiler" % scalaVersion.value)
case _ => Seq("org.scala-lang" %% "scala3-compiler" % scalaVersion.value)
}
},
libraryDependencies += parserCombinators(scalaVersion.value) % Optional,
libraryDependencies += ("org.scalameta" %% "scalameta" % "4.4.33").cross(CrossVersion.for3Use2_13),
run / fork := true
)
.aggregate(apiJvm, parser)
.dependsOn(apiJvm, parser % "compile->compile;test->test")

lazy val plugin = project
.in(file("sbt-twirl"))
Expand All @@ -127,7 +157,7 @@ lazy val plugin = project
}
()
},
mimaFailOnNoPrevious := false,
mimaFailOnNoPrevious := false
)

// Version file
Expand All @@ -141,3 +171,4 @@ def generateVersionFile =
}

addCommandAlias("validateCode", ";headerCheckAll;+scalafmtCheckAll;scalafmtSbtCheck")
addCommandAlias("format", ";+scalafmtAll;scalafmtSbt")
Loading