Skip to content

Commit

Permalink
feat(scala3): Formating and cleanup after integrating tototoshi branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
brbrown25 committed Jan 31, 2022
1 parent 096b082 commit de50d09
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ case class BaseScalaTemplate[T <: Appendable[T], F <: Format[T]](format: F) {
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: TraversableOnce[_] => format.fill(escapeds.iterator.map(_display_).toList)
case escapeds: Array[_] => format.fill(escapeds.view.map(_display_).toList)
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ case class BaseScalaTemplate[T <: Appendable[T], F <: Format[T]](format: F) {
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: TraversableOnce[_] => format.fill(escapeds.iterator.map(_display_).toList)
case escapeds: Array[_] => format.fill(escapeds.view.map(_display_).toList)
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)
Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,4 @@ def generateVersionFile =
}

addCommandAlias("validateCode", ";headerCheckAll;+scalafmtCheckAll;scalafmtSbtCheck")
addCommandAlias("format", ";+scalafmtAll;scalafmtSbt")
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,8 @@ package """ :+ packageName :+ """
val functionType = "(" + params
.map(group =>
"(" + group
.map {
case ByNameParam(_, paramType) => paramType
case p => filterType(p)
.map { case p =>
filterType(p)
}
.mkString(",") + ")"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/*
* Copyright (C) Lightbend Inc. <https://www.lightbend.com>
*/
package play.twirl.compiler
package test

import java.io._


object Helper {
case class CompilationError(message: String, line: Int, column: Int) extends RuntimeException(message)

Expand Down
30 changes: 19 additions & 11 deletions compiler/src/test/scala-3/play/twirl/compiler/test/Helper.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
/*
* Copyright (C) Lightbend Inc. <https://www.lightbend.com>
*/
package play.twirl.compiler
package test

import java.io._
import java.net.URLClassLoader
import java.nio.file.{Files, Path, Paths}
import dotty.tools.dotc.core.Contexts, Contexts.{ Context, ctx }
import dotty.tools.dotc.{Compiler, Driver}
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import dotty.tools.dotc.core.Contexts
import Contexts.Context
import Contexts.ctx
import dotty.tools.dotc.Compiler
import dotty.tools.dotc.Driver
import dotty.tools.dotc.reporting.Reporter
import dotty.tools.io.{ PlainDirectory, Directory, ClassPath }
import dotty.tools.io.PlainDirectory
import dotty.tools.io.Directory
import dotty.tools.io.ClassPath
import scala.jdk.CollectionConverters._


object Helper {
case class CompilationError(message: String, line: Int, column: Int) extends RuntimeException(message)

Expand Down Expand Up @@ -47,9 +56,9 @@ object Helper {
}

def compile[T](
templateName: String,
className: String,
additionalImports: Seq[String] = Nil
templateName: String,
className: String,
additionalImports: Seq[String] = Nil
): CompiledTemplate[T] = {
val templateFile = new File(sourceDir, templateName)
val Some(generated) = twirlCompiler.compile(
Expand All @@ -75,16 +84,15 @@ object Helper {
val reporter = driver.compile()

if (reporter.hasErrors) {
val error = reporter.allErrors.head
val error = reporter.allErrors.head
val message = error.msg
val pos = error.pos
val pos = error.pos
throw CompilationError(message.toString, mapper.mapLine(pos.line + 1), mapper.mapPosition(pos.point))
}

new CompiledTemplate[T](className)
}


class TestDriver(outDir: Path, compilerArgs: Array[String], path: Path) extends Driver {
def compile(): Reporter = {
val Some((toCompile, rootCtx)) = setup(compilerArgs :+ path.toAbsolutePath.toString, initCtx.fresh)
Expand Down
8 changes: 5 additions & 3 deletions parser/src/main/scala/play/twirl/parser/TwirlParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -629,11 +629,12 @@ class TwirlParser(val shouldParseInclusiveDot: Boolean) {
position(Simple("if" + parens), p + 1),
blk,
Simple(" else { null } ")
))
)
)
)
} else {
val ws = whitespaceNoBreak()
val m = mixed()
val m = mixed()
if (m.nonEmpty) {
result = Display(
ScalaExp(
Expand All @@ -642,7 +643,8 @@ class TwirlParser(val shouldParseInclusiveDot: Boolean) {
position(Simple("if" + parens), p + 1),
Block(ws, None, m),
Simple(" else { null } ")
))
)
)
)
} else {
result = null
Expand Down
44 changes: 17 additions & 27 deletions project/Common.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,34 @@ object Common extends AutoPlugin {
"-Xlint:unchecked"
)

private val scalacOptionsForScala2 = Seq(
val scalacParameters = Seq(
"-deprecation",
"-feature",
"-unchecked",
"-encoding",
"utf8",
"-target:jvm-1.8",
"-Ywarn-unused:imports",
"-Xlint:nullary-unit",
"-Xlint",
"-Ywarn-dead-code",
"utf8"
)

private val scalacOptionsForScala3 = Seq(
"-deprecation",
"-feature",
"-unchecked",
"-encoding",
"utf8",
)

private def crossScalacOptions(version: String) =
def crossScalacOptions(version: String) = {
CrossVersion.partialVersion(version) match {
case Some((2, _)) => scalacOptionsForScala2
case _ => scalacOptionsForScala3
case Some((2, n)) if n < 12 =>
scalacParameters ++ Seq(
"-target:jvm-1.8",
"-Ywarn-unused:imports",
"-Xlint:nullary-unit",
"-Xlint",
"-Ywarn-dead-code"
)
case _ => scalacParameters
}
}

override def projectSettings =
Seq(
scalaVersion := Scala212,
crossScalaVersions := ScalaVersions,
scalacOptions ++= crossScalacOptions(scalaVersion.value),
javacOptions ++= javacParameters
scalaVersion := Scala212,
crossScalaVersions := ScalaVersions,
scalacOptions ++= crossScalacOptions(scalaVersion.value),
javacOptions ++= javacParameters
)

override def globalSettings =
Expand All @@ -66,11 +61,6 @@ object Common extends AutoPlugin {
organizationHomepage := Some(url("https://www.lightbend.com/")),
homepage := Some(url(s"https://github.com/playframework/${repoName}")),
licenses := Seq("Apache-2.0" -> url("https://www.apache.org/licenses/LICENSE-2.0.html")),
scalaVersion := Scala212,
crossScalaVersions := ScalaVersions,
semanticdbEnabled := scalaVersion.value != Scala212,
scalacOptions ++= scalacParameters ++ scalacVersionSpecificParameters(scalaVersion.value),
javacOptions ++= javacParameters,
developers += Developer(
"contributors",
"Contributors",
Expand Down

0 comments on commit de50d09

Please sign in to comment.