Skip to content

Commit

Permalink
Merge pull request #236 from marcospereira/issues/229/scalafmt
Browse files Browse the repository at this point in the history
Configure scalafmt
  • Loading branch information
mergify[bot] authored Jul 16, 2019
2 parents efa81e9 + 9e4886a commit 21105a2
Show file tree
Hide file tree
Showing 38 changed files with 1,135 additions and 653 deletions.
12 changes: 12 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Keep in sync with docs/.scalafmt.conf
align = true
assumeStandardLibraryStripMargin = true
danglingParentheses = true
docstrings = JavaDoc
maxColumn = 120
project.git = true
rewrite.rules = [ AvoidInfix, ExpandImportSelectors, RedundantParens, SortModifiers, PreferCurlyFors ]
rewrite.sortModifiers.order = [ "private", "protected", "final", "sealed", "abstract", "implicit", "override", "lazy" ]
spaces.inImportCurlyBraces = true # more idiomatic to include whitepsace in import x.{ yyy }
trailingCommas = preserve
version = 2.0.0
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ before_install: curl -Ls https://git.io/jabba | bash && . ~/.jabba/jabba.sh
install: jabba install "adopt@~1.$TRAVIS_JDK.0-0" && jabba use "$_" && java -Xmx32m -version

script:
- ./scripts/test-code.sh
- ./scripts/validate-code.sh
- ./scripts/validate-docs.sh

Expand Down
29 changes: 15 additions & 14 deletions api/shared/src/main/scala/play/twirl/api/BaseScalaTemplate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,33 @@ 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: 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_(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 () => 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 None => format.empty
case Some(v) => _display_(v)
case _ => format.empty
case _ => format.empty
}
case xml: scala.xml.NodeSeq => format.raw(xml.toString())
case escapeds: immutable.Seq[_] => format.fill(escapeds.map(_display_))
case xml: scala.xml.NodeSeq => format.raw(xml.toString())
case escapeds: immutable.Seq[_] => format.fill(escapeds.map(_display_))
case escapeds: TraversableOnce[_] => format.fill(escapeds.map(_display_).toList)
case escapeds: Array[_] => format.fill(escapeds.view.map(_display_).toList)
case escapeds: java.util.List[_] => format.fill(JavaConverters.collectionAsScalaIterableConverter(escapeds).asScala.map(_display_).toList)
case escapeds: Array[_] => format.fill(escapeds.view.map(_display_).toList)
case escapeds: java.util.List[_] =>
format.fill(JavaConverters.collectionAsScalaIterableConverter(escapeds).asScala.map(_display_).toList)
case string: String => format.escape(string)
case v if v != null => format.escape(v.toString)
case _ => format.empty
case _ => format.empty
}
}
}
8 changes: 6 additions & 2 deletions api/shared/src/main/scala/play/twirl/api/Content.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ trait Content {
* @param text Formatted content
* @tparam A self-type
*/
abstract class BufferedContent[A <: BufferedContent[A]](protected val elements: immutable.Seq[A], protected val text: String) extends Appendable[A] with Content { this: A =>
abstract class BufferedContent[A <: BufferedContent[A]](
protected val elements: immutable.Seq[A],
protected val text: String
) extends Appendable[A]
with Content { this: A =>
protected def buildString(builder: StringBuilder): Unit = {
if (!elements.isEmpty) {
elements.foreach { e =>
Expand All @@ -59,7 +63,7 @@ abstract class BufferedContent[A <: BufferedContent[A]](protected val elements:

override def equals(obj: Any): Boolean = obj match {
case other: BufferedContent[_] if this.getClass == other.getClass => body == other.body
case _ => false
case _ => false
}

override def hashCode(): Int = this.getClass.hashCode() + body.hashCode()
Expand Down
24 changes: 14 additions & 10 deletions api/shared/src/main/scala/play/twirl/api/Formats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ object Formats {
* This has 3 states, either it's a tree of elements, or a leaf, if it's a leaf, it's either safe text, or unsafe text
* that needs to be escaped when written out.
*/
class Html private[api] (elements: immutable.Seq[Html], text: String, escape: Boolean) extends BufferedContent[Html](elements, text) {
class Html private[api] (elements: immutable.Seq[Html], text: String, escape: Boolean)
extends BufferedContent[Html](elements, text) {
def this(text: String) = this(Nil, Formats.safe(text), false)
def this(elements: immutable.Seq[Html]) = this(elements, "", false)

Expand All @@ -36,7 +37,7 @@ class Html private[api] (elements: immutable.Seq[Html], text: String, escape: Bo
* of Strings, if it doesn't, performance actually goes down (measured 10%), due to the fact that the JVM can't
* optimise the invocation of buildString as well because there are two different possible implementations.
*/
override protected def buildString(builder: StringBuilder): Unit = {
protected override def buildString(builder: StringBuilder): Unit = {
if (elements.nonEmpty) {
elements.foreach { e =>
e.buildString(builder)
Expand All @@ -47,12 +48,12 @@ class Html private[api] (elements: immutable.Seq[Html], text: String, escape: Bo
var i = 0
while (i < text.length) {
text.charAt(i) match {
case '<' => builder.append("&lt;")
case '>' => builder.append("&gt;")
case '"' => builder.append("&quot;")
case '<' => builder.append("&lt;")
case '>' => builder.append("&gt;")
case '"' => builder.append("&quot;")
case '\'' => builder.append("&#x27;")
case '&' => builder.append("&amp;")
case c => builder += c
case '&' => builder.append("&amp;")
case c => builder += c
}
i += 1
}
Expand Down Expand Up @@ -80,8 +81,8 @@ object Html {
}

/**
* Creates an HTML fragment with initial content specified. Uses an empty String if None is passed.
*/
* Creates an HTML fragment with initial content specified. Uses an empty String if None is passed.
*/
def apply(text: Option[String]): Html = {
apply(text.getOrElse(""))
}
Expand Down Expand Up @@ -227,7 +228,8 @@ object XmlFormat extends Format[Xml] {
/**
* Type used in default JavaScript templates.
*/
class JavaScript private (elements: immutable.Seq[JavaScript], text: String) extends BufferedContent[JavaScript](elements, text) {
class JavaScript private (elements: immutable.Seq[JavaScript], text: String)
extends BufferedContent[JavaScript](elements, text) {
def this(text: String) = this(Nil, Formats.safe(text))
def this(elements: immutable.Seq[JavaScript]) = this(elements, "")

Expand All @@ -241,6 +243,7 @@ class JavaScript private (elements: immutable.Seq[JavaScript], text: String) ext
* Helper for JavaScript utility methods.
*/
object JavaScript {

/**
* Creates a JavaScript fragment with initial content specified
*/
Expand All @@ -253,6 +256,7 @@ object JavaScript {
* Formatter for JavaScript content.
*/
object JavaScriptFormat extends Format[JavaScript] {

/**
* Integrate `text` without performing any escaping process.
* @param text Text to integrate
Expand Down
100 changes: 94 additions & 6 deletions api/shared/src/main/scala/play/twirl/api/Template.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,113 @@ trait Template16[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Result] {
}

trait Template17[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, Result] {
def render(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M, n: N, o: O, p: P, q: Q): Result
def render(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M, n: N, o: O, p: P, q: Q)
: Result
}

trait Template18[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, Result] {
def render(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M, n: N, o: O, p: P, q: Q, r: R): Result
def render(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M, n: N, o: O, p: P, q: Q, r: R)
: Result
}

trait Template19[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, Result] {
def render(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M, n: N, o: O, p: P, q: Q, r: R, s: S): Result
def render(
a: A,
b: B,
c: C,
d: D,
e: E,
f: F,
g: G,
h: H,
i: I,
j: J,
k: K,
l: L,
m: M,
n: N,
o: O,
p: P,
q: Q,
r: R,
s: S
): Result
}

trait Template20[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, Result] {
def render(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M, n: N, o: O, p: P, q: Q, r: R, s: S, t: T): Result
def render(
a: A,
b: B,
c: C,
d: D,
e: E,
f: F,
g: G,
h: H,
i: I,
j: J,
k: K,
l: L,
m: M,
n: N,
o: O,
p: P,
q: Q,
r: R,
s: S,
t: T
): Result
}

trait Template21[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, Result] {
def render(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M, n: N, o: O, p: P, q: Q, r: R, s: S, t: T, u: U): Result
def render(
a: A,
b: B,
c: C,
d: D,
e: E,
f: F,
g: G,
h: H,
i: I,
j: J,
k: K,
l: L,
m: M,
n: N,
o: O,
p: P,
q: Q,
r: R,
s: S,
t: T,
u: U
): Result
}

trait Template22[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, Result] {
def render(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M, n: N, o: O, p: P, q: Q, r: R, s: S, t: T, u: U, v: V): Result
def render(
a: A,
b: B,
c: C,
d: D,
e: E,
f: F,
g: G,
h: H,
i: I,
j: J,
k: K,
l: L,
m: M,
n: N,
o: O,
p: P,
q: Q,
r: R,
s: S,
t: T,
u: U,
v: V
): Result
}
16 changes: 8 additions & 8 deletions api/shared/src/main/scala/play/twirl/api/TemplateMagic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ object TemplateMagic {
// --- IF

implicit def iterableToBoolean(x: Iterable[_]) = x != null && !x.isEmpty
implicit def optionToBoolean(x: Option[_]) = x != null && x.isDefined
implicit def stringToBoolean(x: String) = x != null && !x.isEmpty
implicit def optionToBoolean(x: Option[_]) = x != null && x.isDefined
implicit def stringToBoolean(x: String) = x != null && !x.isEmpty

// --- JAVA

Expand All @@ -33,12 +33,12 @@ object TemplateMagic {

case class Default(default: Any) {
def ?:(x: Any) = x match {
case "" => default
case Nil => default
case "" => default
case Nil => default
case false => default
case 0 => default
case None => default
case _ => x
case 0 => default
case None => default
case _ => x
}
}

Expand All @@ -62,7 +62,7 @@ object TemplateMagic {

def when(predicate: => Boolean) = {
predicate match {
case true => string
case true => string
case false => ""
}
}
Expand Down
48 changes: 25 additions & 23 deletions api/shared/src/main/scala/play/twirl/api/TwirlFeatureImports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ package play.twirl.api
import scala.language.implicitConversions

/**
* Imports that provide Twirl language features.
*
* This includes:
*
* - @defining
* - @using
* - iterable/option/string as boolean for if statements
* - default values (maybeFoo ? defaultFoo)
*/
* Imports that provide Twirl language features.
*
* This includes:
*
* - @defining
* - @using
* - iterable/option/string as boolean for if statements
* - default values (maybeFoo ? defaultFoo)
*/
object TwirlFeatureImports {

/**
* Provides the `@defining` language feature, that lets you set a local val that can be reused.
*
* @param t The defined val.
* @param handler The block to handle it.
*/
* Provides the `@defining` language feature, that lets you set a local val that can be reused.
*
* @param t The defined val.
* @param handler The block to handle it.
*/
def defining[T](t: T)(handler: T => Any): Any = {
handler(t)
}
Expand All @@ -32,23 +32,25 @@ object TwirlFeatureImports {

/** Adds "truthiness" to iterables, making them false if they are empty. */
implicit def twirlIterableToBoolean(x: Iterable[_]): Boolean = x != null && !x.isEmpty

/** Adds "truthiness" to options, making them false if they are empty. */
implicit def twirlOptionToBoolean(x: Option[_]): Boolean = x != null && x.isDefined

/** Adds "truthiness" to strings, making them false if they are empty. */
implicit def twirlStringToBoolean(x: String): Boolean = x != null && !x.isEmpty

/**
* Provides default values, such that an empty sequence, string, option, false boolean, or null will render
* the default value.
*/
* Provides default values, such that an empty sequence, string, option, false boolean, or null will render
* the default value.
*/
implicit class TwirlDefaultValue(default: Any) {
def ?:(x: Any): Any = x match {
case "" => default
case Nil => default
case "" => default
case Nil => default
case false => default
case 0 => default
case None => default
case _ => x
case 0 => default
case None => default
case _ => x
}
}
}
}
Loading

0 comments on commit 21105a2

Please sign in to comment.