Skip to content

Commit

Permalink
Update to Scala 3 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
noelwelsh committed May 14, 2024
1 parent feae7a5 commit cda2e96
Show file tree
Hide file tree
Showing 180 changed files with 670 additions and 694 deletions.
1 change: 1 addition & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
version = "3.8.1"
runner.dialect = scala3
rewrite.scala3.convertToNewSyntax = true
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import cats.effect.unsafe.IORuntime
import doodle.algebra.Algebra
import doodle.algebra.Picture
import doodle.core.format.Format
import doodle.core.{Base64 => B64}
import doodle.core.{Base64 as B64}
import doodle.effect.Base64Writer

trait Base64WriterSyntax {
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/doodle/algebra/FromBase64.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package doodle
package algebra

import doodle.core.Base64
import doodle.core.format._
import doodle.core.format.*

/** Algebra indicating a Picture can be created from a base 64 encoded bitmap in
* Gif format.
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/doodle/algebra/Picture.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package doodle
package algebra

import cats._
import cats.*

/** Represents a picture, which is a function from a tagless final algebra to
* some type F that represents drawing a picture with result A. Has a monad
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/doodle/algebra/Transform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package algebra

import doodle.core.Angle
import doodle.core.Vec
import doodle.core.{Transform => Tx}
import doodle.core.{Transform as Tx}

trait Transform extends Algebra {
def transform[A](img: Drawing[A], tx: Tx): Drawing[A]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final case class DrawingContext(
this.copy(strokeColor = color)

def strokeWidth(width: Double): DrawingContext =
this.copy(strokeWidth = if (width <= 0) None else Some(width))
this.copy(strokeWidth = if width <= 0 then None else Some(width))

def strokeCap(cap: Cap): DrawingContext =
this.copy(strokeCap = cap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ package generic

import cats.Eval
import cats.Later
import cats.data._
import cats.data.*
import doodle.core.BoundingBox
import doodle.core.{Transform => Tx}
import doodle.core.{Transform as Tx}

/** A [[Finalized]] represents an effect that, when run, produces all the
* information needed to layout an image (it "finalizes" all the information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ package doodle
package algebra
package generic

import cats._
import cats.implicits._
import cats.*
import cats.implicits.*
import doodle.core.BoundingBox
import doodle.core.Landmark
import doodle.core.Transform

trait GenericLayout[G[_]] extends Layout {
self: GivenApply[G] with Algebra { type Drawing[A] = Finalized[G, A] } =>
import Renderable._
import Renderable.*

def on[A](top: Finalized[G, A], bottom: Finalized[G, A])(implicit
s: Semigroup[A]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package algebra
package generic

import cats.data.State
import doodle.core._
import doodle.core.{Transform => Tx}
import doodle.core.*
import doodle.core.{Transform as Tx}

import scala.annotation.tailrec

Expand Down Expand Up @@ -71,7 +71,7 @@ trait GenericPath[G[_]] extends Path {
}

def boundingBox(elements: List[PathElement]): BoundingBox = {
import PathElement._
import PathElement.*

// This implementation should avoid allocation
var minX: Double = 0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package generic

import cats.data.State
import doodle.core.BoundingBox
import doodle.core.{Transform => Tx}
import doodle.core.{Transform as Tx}

trait GenericShape[G[_]] extends Shape {
self: Algebra { type Drawing[A] = Finalized[G, A] } =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package doodle
package algebra
package generic

import cats.syntax.functor._
import cats.syntax.functor.*
import doodle.core.BoundingBox

/** Get information about the size of the bounding box enclosing an picture.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package generic
import cats.data.State
import doodle.core.BoundingBox
import doodle.core.font.Font
import doodle.core.{Transform => Tx}
import doodle.core.{Transform as Tx}

trait GenericText[G[_]] extends Text {
self: Algebra { type Drawing[A] = Finalized[G, A] } =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package doodle
package algebra
package generic

import doodle.core.{Transform => Tx}
import doodle.core.{Transform as Tx}

trait GenericTransform[G[_]] extends Transform {
self: Algebra { type Drawing[A] = Finalized[G, A] } =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package doodle
package algebra

import cats._
import cats.data._
import cats.syntax.all._
import doodle.core.{Transform => Tx}
import cats.*
import cats.data.*
import cats.syntax.all.*
import doodle.core.{Transform as Tx}

package object generic {
type ContextTransform = DrawingContext => DrawingContext
Expand Down
6 changes: 2 additions & 4 deletions core/shared/src/main/scala/doodle/core/Angle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ object Angle {
Angle(t * TwoPi)

def apply(radians: Double): Angle =
if (radians.isNaN)
new Angle(0.0)
else
new Angle(radians)
if radians.isNaN then new Angle(0.0)
else new Angle(radians)
}
27 changes: 11 additions & 16 deletions core/shared/src/main/scala/doodle/core/Color.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package doodle
package core

import doodle.syntax.angle._
import doodle.syntax.normalized._
import doodle.syntax.unsignedByte._
import doodle.syntax.angle.*
import doodle.syntax.normalized.*
import doodle.syntax.unsignedByte.*

sealed abstract class Color extends Product with Serializable {
import Color._
import Color.*

// Accessors ----------------------------------------------

Expand Down Expand Up @@ -193,21 +193,18 @@ sealed abstract class Color extends Product with Serializable {
val delta = cMax - cMin

val unnormalizedHue =
if (cMax == rNormalized)
if cMax == rNormalized then
60 * (((gNormalized - bNormalized) / delta))
else if (cMax == gNormalized)
else if cMax == gNormalized then
60 * (((bNormalized - rNormalized) / delta) + 2)
else
60 * (((rNormalized - gNormalized) / delta) + 4)
else 60 * (((rNormalized - gNormalized) / delta) + 4)
val hue = unnormalizedHue.degrees

val lightness = Normalized.clip((cMax + cMin) / 2)

val saturation =
if (delta == 0.0)
Normalized.MinValue
else
Normalized.clip(delta / (1 - Math.abs(2 * lightness.get - 1)))
if delta == 0.0 then Normalized.MinValue
else Normalized.clip(delta / (1 - Math.abs(2 * lightness.get - 1)))

HSLA(hue, saturation, lightness, a)

Expand All @@ -234,10 +231,8 @@ sealed abstract class Color extends Product with Serializable {

val lightness = l.get
val q =
if (lightness < 0.5)
lightness * (1 + s)
else
lightness + s - (lightness * s)
if lightness < 0.5 then lightness * (1 + s)
else lightness + s - (lightness * s)
val p = 2 * lightness - q
val r = hueToRgb(p, q, Normalized.wrap((h + 120.degrees).toTurns))
val g = hueToRgb(p, q, Normalized.wrap(h.toTurns))
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/doodle/core/Coordinate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ package core
* right) edge of the bounding box.
*/
sealed trait Coordinate {
import Coordinate._
import Coordinate.*

def add(that: Coordinate): Coordinate =
Add(this, that)
Expand All @@ -44,7 +44,7 @@ sealed trait Coordinate {
def eval(negative: Double, positive: Double): Double =
this match {
case Percent(value) =>
if (value < 0) Math.abs(value) * negative
if value < 0 then Math.abs(value) * negative
else value * positive

case Point(value) => value
Expand Down
12 changes: 4 additions & 8 deletions core/shared/src/main/scala/doodle/core/Normalized.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ final case class Normalized(get: Double) extends AnyVal {
this.get - that.get

def max(that: Normalized): Normalized =
if (this.get > that.get)
this
else
that
if this.get > that.get then this
else that

def min(that: Normalized): Normalized =
if (this.get < that.get)
this
else
that
if this.get < that.get then this
else that

def toTurns: Angle =
Angle.turns(get)
Expand Down
8 changes: 3 additions & 5 deletions core/shared/src/main/scala/doodle/core/Parametric.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package doodle
package core

import doodle.syntax.all._
import doodle.syntax.all.*

import scala.annotation.tailrec

Expand Down Expand Up @@ -244,10 +244,8 @@ object Parametric {

/* Get the index into the curves array from t, where each curve has a 1/size share of the space */
def index(t: Normalized): Int = {
if (t.get == 1.0)
size - 1
else
Math.floor(t.get * size).toInt
if t.get == 1.0 then size - 1
else Math.floor(t.get * size).toInt
}

NormalizedCurve { (t: Normalized) =>
Expand Down
21 changes: 9 additions & 12 deletions core/shared/src/main/scala/doodle/core/PathElement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ object PathElement {
BezierCurveTo(origin + b, origin + c, origin + d)
}

if (angle > Angle.one) circle(Point.zero, diameter)
else if (angle > Angle.threeQuarters) {
if angle > Angle.one then circle(Point.zero, diameter)
else if angle > Angle.threeQuarters then {
List(
moveTo(x + r, y),
BezierCurveTo(
Expand All @@ -180,7 +180,7 @@ object PathElement {
),
smallArc(Angle.threeQuarters, angle)
)
} else if (angle > Angle.oneHalf) {
} else if angle > Angle.oneHalf then {
List(
moveTo(x + r, y),
BezierCurveTo(
Expand All @@ -195,7 +195,7 @@ object PathElement {
),
smallArc(Angle.oneHalf, angle)
)
} else if (angle > Angle.oneQuarter) {
} else if angle > Angle.oneQuarter then {
List(
moveTo(x + r, y),
BezierCurveTo(
Expand All @@ -205,7 +205,7 @@ object PathElement {
),
smallArc(Angle.oneQuarter, angle)
)
} else if (angle > Angle.zero)
} else if angle > Angle.zero then
List(moveTo(x + r, y), smallArc(Angle.zero, angle))
else List()
}
Expand Down Expand Up @@ -236,10 +236,8 @@ object PathElement {
val rotation = Angle.one / (points * 2.0)
val path =
(1 to (points * 2)).map { n =>
if (n % 2 == 0)
lineTo(outerRadius, rotation * n.toDouble)
else
lineTo(innerRadius, rotation * n.toDouble)
if n % 2 == 0 then lineTo(outerRadius, rotation * n.toDouble)
else lineTo(innerRadius, rotation * n.toDouble)
}.toList

(moveTo(outerRadius, Angle.zero) +: path)
Expand Down Expand Up @@ -297,10 +295,9 @@ object PathElement {
): List[PathElement] = {
// Clamp radius to the smallest of width and height
val cornerRadius =
if (radius > width / 2 || radius > height / 2)
if radius > width / 2 || radius > height / 2 then
(width / 2) min (height / 2)
else
radius
else radius

// Magic number for drawing circles with bezier curves
// See http://spencermortensen.com/articles/bezier-circle/ for approximation
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/doodle/core/Point.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package doodle
package core

sealed abstract class Point extends Product with Serializable {
import Point._
import Point.*

def -(that: Point): Vec =
Vec(this.x - that.x, this.y - that.y)
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/doodle/core/Transform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ final case class Transform(elements: Array[Double]) {
otherElements: Array[Double],
index: Int
): Boolean = {
if (index >= thisElements.length) true
else if (thisElements(index) != otherElements(index)) false
if index >= thisElements.length then true
else if thisElements(index) != otherElements(index) then false
else checkEquality(thisElements, otherElements, index + 1)
}

Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/doodle/core/Vec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final case class Vec(x: Double, y: Double) {

def normalize: Vec = {
val len = length
if (len == 0) Vec(1, 0) else this / len
if len == 0 then Vec(1, 0) else this / len
}

def rotate(by: Angle): Vec =
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/doodle/core/font/Font.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ final case class Font(

}
object Font {
import FontFamily._
import FontSize._
import FontFamily.*
import FontSize.*

val defaultSerif =
Font(serif, FontStyle.normal, FontWeight.normal, points(12))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import cats.effect.IO
import doodle.algebra.Algebra
import doodle.algebra.Picture
import doodle.core.format.Format
import doodle.core.{Base64 => B64}
import doodle.core.{Base64 as B64}

/** The Base64Writer type represent the ability to encode an image as a Base64
* String in a given format.
Expand Down
Loading

0 comments on commit cda2e96

Please sign in to comment.