From fadf9a22302725a8cbf2b9c97952ab8c1b8c4499 Mon Sep 17 00:00:00 2001 From: Renato Cavalcanti Date: Mon, 25 May 2020 17:15:24 +0200 Subject: [PATCH] formatting --- .../main/scala/play/twirl/api/Content.scala | 9 +- .../scala/play/twirl/api/TemplateMagic.scala | 17 +- .../play/twirl/api/TwirlFeatureImports.scala | 17 +- build.sbt | 21 +- .../play/twirl/compiler/TwirlCompiler.scala | 188 +++++++++--------- .../twirl/compiler/test/CompilerSpec.scala | 15 +- .../scala/play/twirl/parser/TwirlParser.scala | 9 +- .../play/twirl/parser/test/ParserSpec.scala | 42 ++-- .../main/scala/play/twirl/sbt/SbtTwirl.scala | 136 +++++++------ .../play/twirl/sbt/TemplateProblem.scala | 18 +- 10 files changed, 257 insertions(+), 215 deletions(-) diff --git a/api/shared/src/main/scala/play/twirl/api/Content.scala b/api/shared/src/main/scala/play/twirl/api/Content.scala index 2ffd4e52..66700f2d 100644 --- a/api/shared/src/main/scala/play/twirl/api/Content.scala +++ b/api/shared/src/main/scala/play/twirl/api/Content.scala @@ -58,10 +58,11 @@ abstract class BufferedContent[A <: BufferedContent[A]]( def body = builtBody - override def equals(obj: Any): Boolean = obj match { - case other: BufferedContent[_] if this.getClass == other.getClass => body == other.body - case _ => false - } + override def equals(obj: Any): Boolean = + obj match { + case other: BufferedContent[_] if this.getClass == other.getClass => body == other.body + case _ => false + } override def hashCode(): Int = this.getClass.hashCode() + body.hashCode() } diff --git a/api/shared/src/main/scala/play/twirl/api/TemplateMagic.scala b/api/shared/src/main/scala/play/twirl/api/TemplateMagic.scala index 45a3497b..283f415c 100644 --- a/api/shared/src/main/scala/play/twirl/api/TemplateMagic.scala +++ b/api/shared/src/main/scala/play/twirl/api/TemplateMagic.scala @@ -31,14 +31,15 @@ object TemplateMagic { // --- DEFAULT case class Default(default: Any) { - def ?:(x: Any) = x match { - case "" => default - case Nil => default - case false => default - case 0 => default - case None => default - case _ => x - } + def ?:(x: Any) = + x match { + case "" => default + case Nil => default + case false => default + case 0 => default + case None => default + case _ => x + } } implicit def anyToDefault(x: Any) = Default(x) diff --git a/api/shared/src/main/scala/play/twirl/api/TwirlFeatureImports.scala b/api/shared/src/main/scala/play/twirl/api/TwirlFeatureImports.scala index 84fe1df5..b2def6ed 100644 --- a/api/shared/src/main/scala/play/twirl/api/TwirlFeatureImports.scala +++ b/api/shared/src/main/scala/play/twirl/api/TwirlFeatureImports.scala @@ -44,13 +44,14 @@ object TwirlFeatureImports { * the default value. */ implicit class TwirlDefaultValue(default: Any) { - def ?:(x: Any): Any = x match { - case "" => default - case Nil => default - case false => default - case 0 => default - case None => default - case _ => x - } + def ?:(x: Any): Any = + x match { + case "" => default + case Nil => default + case false => default + case 0 => default + case None => default + case _ => x + } } } diff --git a/build.sbt b/build.sbt index 95f5af6b..de4ea293 100644 --- a/build.sbt +++ b/build.sbt @@ -137,8 +137,8 @@ lazy val parser = project mimaSettings, name := "twirl-parser", libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % ScalaParserCombinatorsVersion % "optional", - libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test", - libraryDependencies += "org.scalatest" %%% "scalatest" % ScalaTestVersion % "test", + libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test", + libraryDependencies += "org.scalatest" %%% "scalatest" % ScalaTestVersion % "test", ) lazy val compiler = project @@ -149,7 +149,7 @@ lazy val compiler = project commonSettings, mimaSettings, name := "twirl-compiler", - libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value, + libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value, libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % ScalaParserCombinatorsVersion % "optional", fork in run := true, ) @@ -190,12 +190,13 @@ playBuildExtraPublish := { // Version file -def generateVersionFile = Def.task { - val version = (Keys.version in apiJvm).value - val file = (resourceManaged in Compile).value / "twirl.version.properties" - val content = s"twirl.api.version=$version" - IO.write(file, content) - Seq(file) -} +def generateVersionFile = + Def.task { + val version = (Keys.version in apiJvm).value + val file = (resourceManaged in Compile).value / "twirl.version.properties" + val content = s"twirl.api.version=$version" + IO.write(file, content) + Seq(file) + } addCommandAlias("validateCode", ";headerCheckAll;scalafmtCheckAll;scalafmtSbtCheck") diff --git a/compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala b/compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala index cff45ef8..df7602c4 100644 --- a/compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala +++ b/compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala @@ -19,7 +19,7 @@ object Hash { digest.reset() digest.update(bytes) imports.foreach(i => digest.update(i.getBytes("utf-8"))) - digest.digest().map(0xFF & _).map { "%02x".format(_) }.foldLeft("") { _ + _ } + digest.digest().map(0xff & _).map { "%02x".format(_) }.foldLeft("") { _ + _ } } } @@ -373,7 +373,7 @@ object TwirlCompiler { case ScalaExp(parts) => previous :+ parts.map { case s @ Simple(code) => Source(code, s.pos) - case b @ Block(whitespace, args, content) if (content.forall(_.isInstanceOf[ScalaExp])) => + case b @ Block(whitespace, args, content) if content.forall(_.isInstanceOf[ScalaExp]) => Nil :+ Source(whitespace + "{" + args.getOrElse(""), b.pos) :+ visit(content, Nil) :+ "}" case b @ Block(whitespace, args, content) => Nil :+ Source(whitespace + "{" + args.getOrElse(""), b.pos) :+ displayVisitedChildren( @@ -390,13 +390,19 @@ object TwirlCompiler { val defs = (template.sub ++ template.defs).map { case t: Template if t.name.toString == "" => templateCode(t, resultType) case t: Template => { - Nil :+ (if (t.name.str.startsWith("implicit")) "implicit def " else "def ") :+ Source(t.name.str, t.name.pos) :+ Source( + Nil :+ (if (t.name.str.startsWith("implicit")) "implicit def " else "def ") :+ Source( + t.name.str, + t.name.pos + ) :+ Source( t.params.str, t.params.pos ) :+ ":" :+ resultType :+ " = {_display_(" :+ templateCode(t, resultType) :+ ")};" } case Def(name, params, block) => { - Nil :+ (if (name.str.startsWith("implicit")) "implicit def " else "def ") :+ Source(name.str, name.pos) :+ Source( + Nil :+ (if (name.str.startsWith("implicit")) "implicit def " else "def ") :+ Source( + name.str, + name.pos + ) :+ Source( params.str, params.pos ) :+ " = {" :+ block.code :+ "};" @@ -510,102 +516,103 @@ package """ :+ packageName :+ """ /** The maximum time in milliseconds to wait for a compiler response to finish. */ private val Timeout = 10000 - def getFunctionMapping(signature: String, returnType: String): (String, String, String) = synchronized { - def filterType(t: String) = - t.replace("_root_.scala.", "Array") - .replace("", "") + def getFunctionMapping(signature: String, returnType: String): (String, String, String) = + synchronized { + def filterType(t: String) = + t.replace("_root_.scala.", "Array") + .replace("", "") - def findSignature(tree: Tree): Option[DefDef] = { - tree match { - case t: DefDef if t.name.toString == "signature" => Some(t) - case t: Tree => t.children.flatMap(findSignature).headOption + def findSignature(tree: Tree): Option[DefDef] = { + tree match { + case t: DefDef if t.name.toString == "signature" => Some(t) + case t: Tree => t.children.flatMap(findSignature).headOption + } } - } - val params = - findSignature(PresentationCompiler.treeFrom("object FT { def signature" + signature + " }")).get.vparamss - - val resp = PresentationCompiler.global - .askForResponse { () => - val functionType = "(" + params - .map(group => - "(" + group - .map { - case ByNameParam(_, paramType) => " => " + paramType - case a => filterType(a.tpt.toString) - } - .mkString(",") + ")" - ) - .mkString(" => ") + " => " + returnType + ")" + val params = + findSignature(PresentationCompiler.treeFrom("object FT { def signature" + signature + " }")).get.vparamss - val renderCall = "def render%s: %s = apply%s".format( - "(" + params.flatten - .map { - case ByNameParam(name, paramType) => name + ":" + paramType - case a => a.name.toString + ":" + filterType(a.tpt.toString) - } - .mkString(",") + ")", - returnType, - params + val resp = PresentationCompiler.global + .askForResponse { () => + val functionType = "(" + params .map(group => "(" + group - .map { p => - p.name.toString + Option(p.tpt.toString) - .filter(_.startsWith("_root_.scala.")) - .map(_ => ".toIndexedSeq:_*") - .getOrElse("") + .map { + case ByNameParam(_, paramType) => " => " + paramType + case a => filterType(a.tpt.toString) } .mkString(",") + ")" ) - .mkString - ) + .mkString(" => ") + " => " + returnType + ")" - val templateType = "_root_.play.twirl.api.Template%s[%s%s]".format( - params.flatten.size, - params.flatten - .map { - case ByNameParam(_, paramType) => paramType - case a => filterType(a.tpt.toString) - } - .mkString(","), - (if (params.flatten.isEmpty) "" else ",") + returnType - ) + val renderCall = "def render%s: %s = apply%s".format( + "(" + params.flatten + .map { + case ByNameParam(name, paramType) => name + ":" + paramType + case a => a.name.toString + ":" + filterType(a.tpt.toString) + } + .mkString(",") + ")", + returnType, + params + .map(group => + "(" + group + .map { p => + p.name.toString + Option(p.tpt.toString) + .filter(_.startsWith("_root_.scala.")) + .map(_ => ".toIndexedSeq:_*") + .getOrElse("") + } + .mkString(",") + ")" + ) + .mkString + ) - val f = "def f:%s = %s => apply%s".format( - functionType, - params.map(group => "(" + group.map(_.name.toString).mkString(",") + ")").mkString(" => "), - params - .map(group => - "(" + group - .map { p => - p.name.toString + Option(p.tpt.toString) - .filter(_.startsWith("_root_.scala.")) - .map(_ => ".toIndexedSeq:_*") - .getOrElse("") - } - .mkString(",") + ")" - ) - .mkString - ) + val templateType = "_root_.play.twirl.api.Template%s[%s%s]".format( + params.flatten.size, + params.flatten + .map { + case ByNameParam(_, paramType) => paramType + case a => filterType(a.tpt.toString) + } + .mkString(","), + (if (params.flatten.isEmpty) "" else ",") + returnType + ) - (renderCall, f, templateType) - } - .get(Timeout) - - resp match { - case None => - PresentationCompiler.global.reportThrowable(new Throwable("Timeout in getFunctionMapping")) - ("", "", "") - case Some(res) => - res match { - case Right(t) => - PresentationCompiler.global.reportThrowable(new Throwable("Throwable in getFunctionMapping", t)) - ("", "", "") - case Left(res) => - res + val f = "def f:%s = %s => apply%s".format( + functionType, + params.map(group => "(" + group.map(_.name.toString).mkString(",") + ")").mkString(" => "), + params + .map(group => + "(" + group + .map { p => + p.name.toString + Option(p.tpt.toString) + .filter(_.startsWith("_root_.scala.")) + .map(_ => ".toIndexedSeq:_*") + .getOrElse("") + } + .mkString(",") + ")" + ) + .mkString + ) + + (renderCall, f, templateType) } + .get(Timeout) + + resp match { + case None => + PresentationCompiler.global.reportThrowable(new Throwable("Timeout in getFunctionMapping")) + ("", "", "") + case Some(res) => + res match { + case Right(t) => + PresentationCompiler.global.reportThrowable(new Throwable("Throwable in getFunctionMapping", t)) + ("", "", "") + case Left(res) => + res + } + } } - } class CompilerInstance { def additionalClassPathEntry: Option[String] = None @@ -639,9 +646,12 @@ package """ :+ packageName :+ """ ((origBootclasspath :: pathList) ::: additionalClassPathEntry.toList).mkString(File.pathSeparator) } - val compiler = new Global(settings, new ConsoleReporter(settings) { - override def display(pos: Position, msg: String, severity: Severity): Unit = () - }) + val compiler = new Global( + settings, + new ConsoleReporter(settings) { + override def display(pos: Position, msg: String, severity: Severity): Unit = () + } + ) // Everything must be done on the compiler thread, because the presentation compiler is a fussy piece of work. compiler.ask(() => new compiler.Run) diff --git a/compiler/src/test/scala/play/twirl/compiler/test/CompilerSpec.scala b/compiler/src/test/scala/play/twirl/compiler/test/CompilerSpec.scala index 2011ea2b..1ece8e3e 100644 --- a/compiler/src/test/scala/play/twirl/compiler/test/CompilerSpec.scala +++ b/compiler/src/test/scala/play/twirl/compiler/test/CompilerSpec.scala @@ -340,14 +340,17 @@ object Helper { settings.outdir.value = generatedClasses.getAbsolutePath } - val compiler = new Global(settings, new ConsoleReporter(settings) { - override def display(pos: Position, msg: String, severity: Severity): Unit = { - pos match { - case scala.reflect.internal.util.NoPosition => // do nothing - case _ => compileErrors.append(CompilationError(msg, pos.line, pos.point)) + val compiler = new Global( + settings, + new ConsoleReporter(settings) { + override def display(pos: Position, msg: String, severity: Severity): Unit = { + pos match { + case scala.reflect.internal.util.NoPosition => // do nothing + case _ => compileErrors.append(CompilationError(msg, pos.line, pos.point)) + } } } - }) + ) compiler } diff --git a/parser/src/main/scala/play/twirl/parser/TwirlParser.scala b/parser/src/main/scala/play/twirl/parser/TwirlParser.scala index 6334bd61..80f75e77 100644 --- a/parser/src/main/scala/play/twirl/parser/TwirlParser.scala +++ b/parser/src/main/scala/play/twirl/parser/TwirlParser.scala @@ -316,8 +316,8 @@ class TwirlParser(val shouldParseInclusiveDot: Boolean) { } /** Match zero or more `parser` */ - def several[T, BufferType <: mutable.Buffer[T]](parser: () => T, provided: BufferType = null)( - implicit manifest: Manifest[BufferType] + def several[T, BufferType <: mutable.Buffer[T]](parser: () => T, provided: BufferType = null)(implicit + manifest: Manifest[BufferType] ): BufferType = { val ab = if (provided != null) provided else manifest.runtimeClass.newInstance().asInstanceOf[BufferType] var parsed = parser() @@ -582,7 +582,10 @@ class TwirlParser(val shouldParseInclusiveDot: Boolean) { if (parens != null) { val blk = block(blockArgsAllowed = true) if (blk != null) { - result = Display(ScalaExp(ListBuffer(position(Simple("for" + parens + " yield "), p + 1), blk))) // don't include pos of @ + result = + Display( + ScalaExp(ListBuffer(position(Simple("for" + parens + " yield "), p + 1), blk)) + ) // don't include pos of @ } } } diff --git a/parser/src/test/scala/play/twirl/parser/test/ParserSpec.scala b/parser/src/test/scala/play/twirl/parser/test/ParserSpec.scala index 11566fcf..296f91ce 100644 --- a/parser/src/test/scala/play/twirl/parser/test/ParserSpec.scala +++ b/parser/src/test/scala/play/twirl/parser/test/ParserSpec.scala @@ -29,17 +29,19 @@ class ParserSpec extends AnyWordSpec with Matchers with Inside { parseStringSuccess(get(templateName)) } - def parseStringSuccess(template: String) = parseString(template) must matchPattern { - case parser.Success(_, rest) if rest.atEnd() => - } + def parseStringSuccess(template: String) = + parseString(template) must matchPattern { + case parser.Success(_, rest) if rest.atEnd() => + } - def parseFailure(templateName: String, message: String, line: Int, column: Int) = inside(parse(templateName)) { - case parser.Error(_, rest, errors) => - val e = errors.head - e.str mustBe message - e.pos.line mustBe line - e.pos.column mustBe column - } + def parseFailure(templateName: String, message: String, line: Int, column: Int) = + inside(parse(templateName)) { + case parser.Error(_, rest, errors) => + val e = errors.head + e.str mustBe message + e.pos.line mustBe line + e.pos.column mustBe column + } def parseTemplate(templateName: String): Template = { parseTemplateString(get(templateName)) @@ -101,7 +103,10 @@ class ParserSpec extends AnyWordSpec with Matchers with Inside { } "code block containing => of another statement with curly braces in first line" in { - val tmpl = parseTemplateString("""@if(attrs!=null){@attrs.map{ v => @v._1 }}""") // "@attrs.map{ v =>" should not be handled as block args + val tmpl = + parseTemplateString( + """@if(attrs!=null){@attrs.map{ v => @v._1 }}""" + ) // "@attrs.map{ v =>" should not be handled as block args val ifExpressions = tmpl.content(0).asInstanceOf[Display].exp.parts ifExpressions.head must be(Simple("if(attrs!=null)")) val ifBlockBody = ifExpressions(1).asInstanceOf[Block].content(0).asInstanceOf[Display].exp.parts @@ -115,7 +120,10 @@ class ParserSpec extends AnyWordSpec with Matchers with Inside { } "code block containing => of another statement with parentheses in first line" in { - val tmpl = parseTemplateString("""@if(attrs!=null){@attrs.map( v => @v._1 )}""") // "@attrs.map( v =>" should not be handled as block args + val tmpl = + parseTemplateString( + """@if(attrs!=null){@attrs.map( v => @v._1 )}""" + ) // "@attrs.map( v =>" should not be handled as block args val ifExpressions = tmpl.content(0).asInstanceOf[Display].exp.parts ifExpressions.head must be(Simple("if(attrs!=null)")) val ifBlockBody = ifExpressions(1).asInstanceOf[Block].content(0).asInstanceOf[Display].exp.parts @@ -124,7 +132,10 @@ class ParserSpec extends AnyWordSpec with Matchers with Inside { } "code block containing (...) => in first line" in { - val tmpl = parseTemplateString("""@if(attrs!=null){( arg1, arg2 ) => @arg1.toString }""") // "( arg1, arg2 ) =>" should be handled as block args + val tmpl = + parseTemplateString( + """@if(attrs!=null){( arg1, arg2 ) => @arg1.toString }""" + ) // "( arg1, arg2 ) =>" should be handled as block args val ifExpressions = tmpl.content(0).asInstanceOf[Display].exp.parts ifExpressions.head must be(Simple("if(attrs!=null)")) val ifBlock = ifExpressions(1).asInstanceOf[Block] @@ -135,7 +146,10 @@ class ParserSpec extends AnyWordSpec with Matchers with Inside { } "text outside of code block on same line containing =>" in { - val tmpl = parseTemplateString("""@if(attrs!=null){blockbody}Some plain text with => inside""") // "blockbody}Some plain text with =>" should not be handled as block args + val tmpl = + parseTemplateString( + """@if(attrs!=null){blockbody}Some plain text with => inside""" + ) // "blockbody}Some plain text with =>" should not be handled as block args val ifExpressions = tmpl.content(0).asInstanceOf[Display].exp.parts ifExpressions.head must be(Simple("if(attrs!=null)")) val ifBlockBody = ifExpressions(1).asInstanceOf[Block].content(0).asInstanceOf[Plain] diff --git a/sbt-twirl/src/main/scala/play/twirl/sbt/SbtTwirl.scala b/sbt-twirl/src/main/scala/play/twirl/sbt/SbtTwirl.scala index b09e341f..0f57a574 100644 --- a/sbt-twirl/src/main/scala/play/twirl/sbt/SbtTwirl.scala +++ b/sbt-twirl/src/main/scala/play/twirl/sbt/SbtTwirl.scala @@ -43,79 +43,85 @@ object SbtTwirl extends AutoPlugin { positionSettings ++ dependencySettings - def twirlSettings: Seq[Setting[_]] = Seq( - includeFilter in compileTemplates := "*.scala.*", - excludeFilter in compileTemplates := HiddenFileFilter, - sourceDirectories in compileTemplates := Seq(sourceDirectory.value / "twirl"), - watchSources in Defaults.ConfigGlobal += - WatchSource( - (sourceDirectory in compileTemplates).value, - (includeFilter in compileTemplates).value, - (excludeFilter in compileTemplates).value - ), - sources in compileTemplates := Defaults - .collectFiles( - sourceDirectories in compileTemplates, - includeFilter in compileTemplates, - excludeFilter in compileTemplates - ) - .value, - target in compileTemplates := crossTarget.value / "twirl" / Defaults.nameForSrc(configuration.value.name), - compileTemplates := compileTemplatesTask.value, - sourceGenerators += compileTemplates.taskValue, - managedSourceDirectories += (target in compileTemplates).value - ) - - def defaultSettings: Seq[Setting[_]] = Seq( - templateFormats := defaultFormats, - templateImports := TwirlCompiler.DefaultImports, - constructorAnnotations := Nil, - sourceEncoding := scalacEncoding(scalacOptions.value) - ) - - def positionSettings: Seq[Setting[_]] = Seq( - sourcePositionMappers += TemplateProblem.positionMapper(Codec(sourceEncoding.value)) - ) - - def dependencySettings = Def.settings( - twirlVersion := readResourceProperty("twirl.version.properties", "twirl.api.version"), - libraryDependencies += { - val crossVer = crossVersion.value - val isScalaJS = CrossVersion(crossVer, scalaVersion.value, scalaBinaryVersion.value) match { - case Some(f) => f("").contains("_sjs0.6") // detect ScalaJS CrossVersion - case None => false + def twirlSettings: Seq[Setting[_]] = + Seq( + includeFilter in compileTemplates := "*.scala.*", + excludeFilter in compileTemplates := HiddenFileFilter, + sourceDirectories in compileTemplates := Seq(sourceDirectory.value / "twirl"), + watchSources in Defaults.ConfigGlobal += + WatchSource( + (sourceDirectory in compileTemplates).value, + (includeFilter in compileTemplates).value, + (excludeFilter in compileTemplates).value + ), + sources in compileTemplates := Defaults + .collectFiles( + sourceDirectories in compileTemplates, + includeFilter in compileTemplates, + excludeFilter in compileTemplates + ) + .value, + target in compileTemplates := crossTarget.value / "twirl" / Defaults.nameForSrc(configuration.value.name), + compileTemplates := compileTemplatesTask.value, + sourceGenerators += compileTemplates.taskValue, + managedSourceDirectories += (target in compileTemplates).value + ) + + def defaultSettings: Seq[Setting[_]] = + Seq( + templateFormats := defaultFormats, + templateImports := TwirlCompiler.DefaultImports, + constructorAnnotations := Nil, + sourceEncoding := scalacEncoding(scalacOptions.value) + ) + + def positionSettings: Seq[Setting[_]] = + Seq( + sourcePositionMappers += TemplateProblem.positionMapper(Codec(sourceEncoding.value)) + ) + + def dependencySettings = + Def.settings( + twirlVersion := readResourceProperty("twirl.version.properties", "twirl.api.version"), + libraryDependencies += { + val crossVer = crossVersion.value + val isScalaJS = CrossVersion(crossVer, scalaVersion.value, scalaBinaryVersion.value) match { + case Some(f) => f("").contains("_sjs0.6") // detect ScalaJS CrossVersion + case None => false + } + // TODO: use %%% from sbt-crossproject when we add support for scalajs 1.0 + val baseModuleID = "com.typesafe.play" %% "twirl-api" % twirlVersion.value + if (isScalaJS) baseModuleID.cross(crossVer) else baseModuleID } - // TODO: use %%% from sbt-crossproject when we add support for scalajs 1.0 - val baseModuleID = "com.typesafe.play" %% "twirl-api" % twirlVersion.value - if (isScalaJS) baseModuleID.cross(crossVer) else baseModuleID - } - ) + ) def scalacEncoding(options: Seq[String]): String = { val i = options.indexOf("-encoding") + 1 if (i > 0 && i < options.length) options(i) else "UTF-8" } - def defaultFormats = Map( - "html" -> "play.twirl.api.HtmlFormat", - "txt" -> "play.twirl.api.TxtFormat", - "xml" -> "play.twirl.api.XmlFormat", - "js" -> "play.twirl.api.JavaScriptFormat" - ) - - def compileTemplatesTask = Def.task { - TemplateCompiler.compile( - (sourceDirectories in compileTemplates).value, - (target in compileTemplates).value, - templateFormats.value, - templateImports.value, - constructorAnnotations.value, - (includeFilter in compileTemplates).value, - (excludeFilter in compileTemplates).value, - Codec(sourceEncoding.value), - streams.value.log + def defaultFormats = + Map( + "html" -> "play.twirl.api.HtmlFormat", + "txt" -> "play.twirl.api.TxtFormat", + "xml" -> "play.twirl.api.XmlFormat", + "js" -> "play.twirl.api.JavaScriptFormat" ) - } + + def compileTemplatesTask = + Def.task { + TemplateCompiler.compile( + (sourceDirectories in compileTemplates).value, + (target in compileTemplates).value, + templateFormats.value, + templateImports.value, + constructorAnnotations.value, + (includeFilter in compileTemplates).value, + (excludeFilter in compileTemplates).value, + Codec(sourceEncoding.value), + streams.value.log + ) + } def readResourceProperty(resource: String, property: String): String = { val props = new java.util.Properties diff --git a/sbt-twirl/src/main/scala/play/twirl/sbt/TemplateProblem.scala b/sbt-twirl/src/main/scala/play/twirl/sbt/TemplateProblem.scala index 45ac60a8..b358d400 100644 --- a/sbt-twirl/src/main/scala/play/twirl/sbt/TemplateProblem.scala +++ b/sbt-twirl/src/main/scala/play/twirl/sbt/TemplateProblem.scala @@ -15,11 +15,12 @@ import xsbti.Severity import scala.io.Codec object TemplateProblem { - def positionMapper(codec: Codec): Position => Option[Position] = position => { - toScala(position.sourceFile).flatMap(f => MaybeGeneratedSource(f, codec)).map { generated => - TemplatePosition(generated, position) + def positionMapper(codec: Codec): Position => Option[Position] = + position => { + toScala(position.sourceFile).flatMap(f => MaybeGeneratedSource(f, codec)).map { generated => + TemplatePosition(generated, position) + } } - } def exception(source: File, codec: Codec, message: String, line: Int, column: Int) = { val column0 = 0.max(column - 1) // convert to 0-based column @@ -138,10 +139,11 @@ object TemplateProblem { } } - def toJava[A](o: Option[A]): java.util.Optional[A] = o match { - case Some(v) => java.util.Optional.ofNullable(v) - case None => java.util.Optional.empty() - } + def toJava[A](o: Option[A]): java.util.Optional[A] = + o match { + case Some(v) => java.util.Optional.ofNullable(v) + case None => java.util.Optional.empty() + } def toScala[A](o: java.util.Optional[A]): Option[A] = { if (o.isPresent) Option(o.get())