Skip to content

Commit

Permalink
add scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
dos65 committed Oct 4, 2020
1 parent 2c3e181 commit 6ec74fd
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 132 deletions.
3 changes: 3 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = 2.7.3
maxColumn = 100
continuationIndent.defnSite = 2
4 changes: 2 additions & 2 deletions modules/cats-effect/src/main/scala/make/ce/resource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ object resource {

implicit def ceResourceEff[F[_]: Applicative]: MakeEff[Resource[F, ?]] =
new MakeEff[Resource[F, ?]] {
def map[A, B](fa: Resource[F, A])(f: A => B): Resource[F, B] = fa.map(f)
def map[A, B](fa: Resource[F, A])(f: A => B): Resource[F, B] = fa.map(f)
def pure[A](a: A): Resource[F, A] = Resource.pure(a)
def flatMap[A, B](fa: Resource[F, A])(f: A => Resource[F, B]): Resource[F, B] = fa.flatMap(f)
}
}
}
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/make/Conflicts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ final case class Conflicts(values: List[Conflicts.TpeConflict]) extends Exceptio
}
object Conflicts {
final case class TpeConflict(tpe: Type, positions: List[SourcePos])
}
}
47 changes: 24 additions & 23 deletions modules/core/src/main/scala/make/Graph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class Graph[F[_], A](
val order = initOrder
val init = F.pure(Map.empty[Type, Any])

val rs = order.foldLeft(init){ case (rs, tpe) =>
val rs = order.foldLeft(init) { case (rs, tpe) =>
MakeEff[F].flatMap(rs)(depsMap => {

val entry = entries(tpe)
Expand All @@ -27,22 +27,24 @@ final class Graph[F[_], A](
F.map(rsc)(v => depsMap.updated(tpe, v))
})
}

MakeEff[F].map(rs)(values => values(targetTpe).asInstanceOf[A])
}

private def initOrder: List[Type] = {
val indexedKeys = entries.keys.zipWithIndex.toMap
val indexedMap = indexedKeys.map {case (tpe, _) =>
val indexedMap = indexedKeys.map { case (tpe, _) =>
val entry = entries(tpe)
entry.dependsOn.map(indexedKeys(_)).toList
}.toList
val sorted = Tarjans.apply(indexedMap)

sorted.flatten.map(i => {
val (tpe, idx) = indexedKeys.find(_._2 == i).get
tpe
}).toList
sorted.flatten
.map(i => {
val (tpe, idx) = indexedKeys.find(_._2 == i).get
tpe
})
.toList
}

}
Expand All @@ -53,7 +55,7 @@ object Graph {
tpe: Type,
pos: Tag.SourcePos,
dependsOn: List[Type],
f: List[Any] => F[Any]
f: List[Any] => F[Any]
)

def fromMake[F[_]: MakeEff, A](v: Make[F, A]): Either[Conflicts, Graph[F, A]] = {
Expand All @@ -64,23 +66,22 @@ object Graph {

val init = (Map.empty[Type, RawEntry[F]], List.empty[Conflicts.TpeConflict])
val (okMap, errors) =
allEntriesMap.foldLeft(init){
case ((okAcc, errAcc), (tpe, entries)) =>
val refs = entries.foldLeft(Set.empty[SourcePos]){case (acc, e) => acc + e.pos }
if (refs.size > 1) {
val error = Conflicts.TpeConflict(tpe, refs.toList)
(okAcc, error :: errAcc)
} else {
val nextOk = okAcc.updated(tpe, entries.head)
(nextOk, errAcc)
}
allEntriesMap.foldLeft(init) { case ((okAcc, errAcc), (tpe, entries)) =>
val refs = entries.foldLeft(Set.empty[SourcePos]) { case (acc, e) => acc + e.pos }
if (refs.size > 1) {
val error = Conflicts.TpeConflict(tpe, refs.toList)
(okAcc, error :: errAcc)
} else {
val nextOk = okAcc.updated(tpe, entries.head)
(nextOk, errAcc)
}
}

if (errors.size > 0) {
Left(Conflicts(errors))
} else {
Right(new Graph(okMap, v.tag.typeTag.tpe))
}
}
}

@tailrec
Expand All @@ -92,15 +93,15 @@ object Graph {
type HandleOut = (List[Any] => F[Any], List[Type], List[Make[F, Any]])

def handleNode(v: Make[F, Any]): HandleOut = v match {
case Make.Value(v, tag) =>
case Make.Value(v, tag) =>
((_: List[Any]) => v, List.empty, List.empty)
case Make.Bind(prev, f, tag) =>
val func = (in: List[Any]) => f(in(0))
val deps = List(prev.tag.typeTag.tpe)
val other = List(prev)
(func, deps, other)
case Make.Ap(prev, op, tag) =>
val func =
val func =
(in: List[Any]) => {
val a = in(0)
val aToB = in(1).asInstanceOf[Any => Any]
Expand Down Expand Up @@ -142,4 +143,4 @@ object Graph {
makeToAllEntriesMap(nextAcc, nextStack)
}
}
}
}
10 changes: 5 additions & 5 deletions modules/core/src/main/scala/make/Make.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ object Make extends MakeTupleInstances with LowPrioMake {
def eff[F[_]: MakeEff, A: Tag](v: F[A]): Make[F, A] =
Value(v, Tag.of[A])


implicit def contraMakeInstance[F[_]: MakeEff, B, A](
implicit contra: ContraMake[B, A], m: Make[F, B], tag: Tag[A]
implicit def contraMakeInstance[F[_]: MakeEff, B, A](implicit
contra: ContraMake[B, A],
m: Make[F, B],
tag: Tag[A]
): Make[F, A] = MakeOps.map(m)(contra.f)
}

Expand All @@ -46,7 +47,6 @@ object ContraMake {
def apply[B, A](f: B => A): ContraMake[B, A] = new ContraMake[B, A](f)
}


trait LowPrioMake {
implicit def debugInstance[F[_], A](implicit x: Debug[Make[F, A]]): Make[F, A] = x.v
}
}
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/make/MakeEff.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ trait MakeEff[F[_]] {

object MakeEff {
def apply[F[_]](implicit eff: MakeEff[F]): MakeEff[F] = eff
}
}
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/make/MakeSyntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ final class MakeBasicSyntax[F[_], A](private val m: Make[F, A]) extends AnyVal {
// case Right(dag) => dag.toEff
// }
// }
}
}
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/make/Tag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ object Tag {
implicit def materialize: SourcePos =
macro SourcePosMacro.materializeSourcePos
}
}
}
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/make/autoMake.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ import scala.annotation.compileTimeOnly

class autoMake extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro MakeAnnotationMacro.autoMake
}
}
6 changes: 3 additions & 3 deletions modules/core/src/main/scala/make/enableDebug.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package make
import make.internal.MakeMacro

object enableDebug {
implicit def makeToDebugSyntax(obj: Make.type): DebugSyntax = new DebugSyntax(obj)
implicit def debugHook[F[_], A]: Debug[Make[F, A]] = macro MakeMacro.debugHook[F, A]
implicit def makeToDebugSyntax(obj: Make.type): DebugSyntax = new DebugSyntax(obj)
implicit def debugHook[F[_], A]: Debug[Make[F, A]] = macro MakeMacro.debugHook[F, A]
}

final class DebugSyntax(val obj: Make.type) extends AnyVal {
def debugOf[F[_], A]: Make[F, A] = macro MakeMacro.debug[F, A]
}
}
18 changes: 9 additions & 9 deletions modules/core/src/main/scala/make/internal/MacroState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ private[make] object MacroState {
private class MacroStateAttachment(val state: collection.mutable.HashMap[Class[_], Any])

/**
* Associates some state with a global.Run. Preferable to using static state in the macro
* classloader (e.g vars in top level objects) so as to avoid race conditions under `-Ycache-macro-classloader`.
*
* @tparam T the type of the state. The erased `Class[_]` value for this type will be used a a map key, so each user of
* this facility should use a dedicated and distinct class to wrap the state.
* @param u The reflection universe, typically obtained from `context.universe` in a macro implementation.
* @param factory Factory to create the state.
*/
* Associates some state with a global.Run. Preferable to using static state in the macro
* classloader (e.g vars in top level objects) so as to avoid race conditions under `-Ycache-macro-classloader`.
*
* @tparam T the type of the state. The erased `Class[_]` value for this type will be used a a map key, so each user of
* this facility should use a dedicated and distinct class to wrap the state.
* @param u The reflection universe, typically obtained from `context.universe` in a macro implementation.
* @param factory Factory to create the state.
*/
def getOrElseUpdate[T: ClassTag](u: Universe, factory: => T): T = {
// Cast needed for access to perRunCaches and convenient access to attachments
val g = u.asInstanceOf[scala.reflect.internal.SymbolTable]
Expand Down Expand Up @@ -47,4 +47,4 @@ private[make] object MacroState {
value
}
}
}
}
86 changes: 47 additions & 39 deletions modules/core/src/main/scala/make/internal/MakeAnnotationMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ class MakeAnnotationMacro(val c: blackbox.Context) {
def autoMake(annottees: Tree*): Tree = {
annottees match {
case List(cls: ClassDef) =>
Clz.extract(cls) match {
case Some(clz) =>
Clz.extract(cls) match {
case Some(clz) =>
q"""
$cls
object ${cls.name.toTermName} {
${instanceTree(clz)}
}
"""
case None => reportUnexpectedError()
}
"""
case None => reportUnexpectedError()
}
case List(
cls: ClassDef,
q"..$mods object $objName extends { ..$objEarlyDefs } with ..$objParents { $objSelf => ..$objDefs }"
) =>
Clz.extract(cls) match {
case Some(clz) =>
q"""
cls: ClassDef,
q"..$mods object $objName extends { ..$objEarlyDefs } with ..$objParents { $objSelf => ..$objDefs }"
) =>
Clz.extract(cls) match {
case Some(clz) =>
q"""
$cls
$mods object $objName extends { ..$objEarlyDefs } with ..$objParents { $objSelf =>
..$objDefs
..${instanceTree(clz)}
}
"""
case None => reportUnexpectedError()
}
"""
case None => reportUnexpectedError()
}
case _ =>
c.abort(c.enclosingPosition, "@deriveMake can be applied only on case classes or classes")
}
Expand All @@ -53,19 +53,19 @@ class MakeAnnotationMacro(val c: blackbox.Context) {
val mapF = if (params.size > 1) q"($create).tupled" else create

val effTpe = TermName(c.freshName("E")).toTypeName
val targetTpe =

val targetTpe =
if (typeParams.isEmpty)
tq"${name.toTypeName}"
else
tq"${name.toTypeName}[..${typeParams.map(_.name)}]"

val implicits =
q"deps: _root_.make.Make[$effTpe, $tpe]" ::
q"${TermName(c.freshName())}: _root_.make.MakeEff[$effTpe]" ::
implicitParams.toList ++
(if (paramsTpe.isEmpty) List.empty else List(q"tag: _root_.make.Tag[$targetTpe]"))
q"${TermName(c.freshName())}: _root_.make.MakeEff[$effTpe]" ::
implicitParams.toList ++
(if (paramsTpe.isEmpty) List.empty else List(q"tag: _root_.make.Tag[$targetTpe]"))

q"""
implicit def make[$effTpe[_], ..$typeParams](
implicit ..${implicits}
Expand All @@ -85,27 +85,35 @@ class MakeAnnotationMacro(val c: blackbox.Context) {
object Clz {

def extract(clsDef: ClassDef): Option[Clz] = {
findInit(clsDef.impl.body).map{ init =>
val tparams = clsDef.tparams
val (params, implicitParams) =
init.vparamss.flatten.foldLeft((Vector.empty[ValDef], Vector.empty[ValDef])){
case ((pAcc, ipAcc), vdef) =>
val isImplicit = vdef.mods.hasFlag(Flag.IMPLICIT)
if (isImplicit)
(pAcc, ipAcc :+ vdef)
else
(pAcc :+ vdef, ipAcc)
}

val create = {
val funValDefs = params.zipWithIndex.map({
case (d, i) => ValDef(Modifiers(Flag.PARAM), TermName(s"x$i"), d.tpt, EmptyTree)
}).toList
Function(funValDefs, Apply(Select(New(Ident(clsDef.name.decodedName)), init.name), funValDefs.map(d => Ident(d.name))))
findInit(clsDef.impl.body).map { init =>
val tparams = clsDef.tparams
val (params, implicitParams) =
init.vparamss.flatten.foldLeft((Vector.empty[ValDef], Vector.empty[ValDef])) {
case ((pAcc, ipAcc), vdef) =>
val isImplicit = vdef.mods.hasFlag(Flag.IMPLICIT)
if (isImplicit)
(pAcc, ipAcc :+ vdef)
else
(pAcc :+ vdef, ipAcc)
}

Clz(clsDef.name, tparams, params.toList, implicitParams.toList, create)
val create = {
val funValDefs = params.zipWithIndex
.map({ case (d, i) =>
ValDef(Modifiers(Flag.PARAM), TermName(s"x$i"), d.tpt, EmptyTree)
})
.toList
Function(
funValDefs,
Apply(
Select(New(Ident(clsDef.name.decodedName)), init.name),
funValDefs.map(d => Ident(d.name))
)
)
}

Clz(clsDef.name, tparams, params.toList, implicitParams.toList, create)
}
}

def findInit(body: List[Tree]): Option[DefDef] =
Expand All @@ -114,4 +122,4 @@ class MakeAnnotationMacro(val c: blackbox.Context) {
}

}
}
}
4 changes: 2 additions & 2 deletions modules/core/src/main/scala/make/internal/MakeBasicOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait MakeBasicOps {
def mapF[F[_]: MakeEff, A, B: Tag](ma: Make[F, A])(f: A => F[B]): Make[F, B] =
Make.Bind(
ma,
(a: A) => f(a),
(a: A) => f(a),
Tag.of[B]
)

Expand All @@ -27,4 +27,4 @@ trait MakeBasicOps {
Tag.of[B]
)

}
}
Loading

0 comments on commit 6ec74fd

Please sign in to comment.