Skip to content

Commit

Permalink
Added scalafmt config and reformatted code base (#63)
Browse files Browse the repository at this point in the history
Pull request: #63
  • Loading branch information
lefou authored Nov 21, 2022
2 parents c07c5cd + e4de1ce commit b431f9a
Show file tree
Hide file tree
Showing 30 changed files with 167 additions and 173 deletions.
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Reformatted code base with current scalafmt settings
d910117c4c113c5358b0bb7f79be3f2090d55f77

21 changes: 21 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version = "3.6.0"

align.preset = none
align.openParenCallSite = false
align.stripMargin = true

assumeStandardLibraryStripMargin = true

continuationIndent.callSite = 2
continuationIndent.defnSite = 4

docstrings.style = Asterisk
docstrings.oneline = keep
docstrings.wrap = no

maxColumn = 120

newlines.source = keep

runner.dialect = scala213

10 changes: 5 additions & 5 deletions acyclic/src-2.11/acyclic/plugin/Compat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ object Compat {
// from https://github.com/scala/scala-collection-compat/blob/746a7de28223812b19d0d9f68d2253e0c5f655ca/compat/src/main/scala-2.11_2.12/scala/collection/compat/CompatImpl.scala#L8-L11
private def simpleCBF[A, C](f: => Builder[A, C]): CanBuildFrom[Any, A, C] = new CanBuildFrom[Any, A, C] {
def apply(from: Any): Builder[A, C] = apply()
def apply(): Builder[A, C] = f
def apply(): Builder[A, C] = f
}

// from https://github.com/scala/scala-collection-compat/blob/746a7de28223812b19d0d9f68d2253e0c5f655ca/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala#L46-L49
implicit def sortedSetCompanionToCBF[A: Ordering,
CC[X] <: SortedSet[X] with SortedSetLike[X, CC[X]]](
fact: SortedSetFactory[CC]): CanBuildFrom[Any, A, CC[A]] =
implicit def sortedSetCompanionToCBF[A: Ordering, CC[X] <: SortedSet[X] with SortedSetLike[X, CC[X]]](
fact: SortedSetFactory[CC]
): CanBuildFrom[Any, A, CC[A]] =
simpleCBF(fact.newBuilder[A])

}
}
10 changes: 5 additions & 5 deletions acyclic/src-2.12/acyclic/plugin/Compat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ object Compat {
// from https://github.com/scala/scala-collection-compat/blob/746a7de28223812b19d0d9f68d2253e0c5f655ca/compat/src/main/scala-2.11_2.12/scala/collection/compat/CompatImpl.scala#L8-L11
private def simpleCBF[A, C](f: => Builder[A, C]): CanBuildFrom[Any, A, C] = new CanBuildFrom[Any, A, C] {
def apply(from: Any): Builder[A, C] = apply()
def apply(): Builder[A, C] = f
def apply(): Builder[A, C] = f
}

// from https://github.com/scala/scala-collection-compat/blob/746a7de28223812b19d0d9f68d2253e0c5f655ca/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala#L46-L49
implicit def sortedSetCompanionToCBF[A: Ordering,
CC[X] <: SortedSet[X] with SortedSetLike[X, CC[X]]](
fact: SortedSetFactory[CC]): CanBuildFrom[Any, A, CC[A]] =
implicit def sortedSetCompanionToCBF[A: Ordering, CC[X] <: SortedSet[X] with SortedSetLike[X, CC[X]]](
fact: SortedSetFactory[CC]
): CanBuildFrom[Any, A, CC[A]] =
simpleCBF(fact.newBuilder[A])

}
}
2 changes: 1 addition & 1 deletion acyclic/src/acyclic/package.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import scala.reflect.internal.annotations.compileTimeOnly
package object acyclic {

/**
* Import this within a file to make Acyclic verify that the file does not
* have any circular dependencies with other files.
Expand All @@ -8,7 +9,6 @@ package object acyclic {
def file = ()

/**
*
*/
@compileTimeOnly("acyclic.file is just a marker and not a real value")
def skipped = ()
Expand Down
4 changes: 1 addition & 3 deletions acyclic/src/acyclic/plugin/DependencyExtraction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package acyclic.plugin
import acyclic.file
import scala.tools.nsc.Global
object DependencyExtraction{
object DependencyExtraction {
def apply(global: Global)(unit: global.CompilationUnit): Seq[(global.Symbol, global.Tree)] = {
import global._

Expand All @@ -29,7 +29,6 @@ object DependencyExtraction{
override def traverse(tree: Tree): Unit = {
tree match {
case i @ Import(expr, selectors) =>

selectors.foreach {
case ImportSelector(nme.WILDCARD, _, null, _) =>
// in case of wildcard import we do not rely on any particular name being defined
Expand Down Expand Up @@ -74,7 +73,6 @@ object DependencyExtraction{
traverser.dependencies
}


class ExtractDependenciesByInheritanceTraverser extends ExtractDependenciesTraverser {
override def traverse(tree: Tree): Unit = tree match {
case Template(parents, self, body) =>
Expand Down
51 changes: 26 additions & 25 deletions acyclic/src/acyclic/plugin/GraphAnalysis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,36 @@ import acyclic.file
import scala.tools.nsc.Global
import collection.mutable

sealed trait Value{
sealed trait Value {
def pkg: List[String]
def prettyPrint: String
}
object Value{
case class File(path: String, pkg: List[String] = Nil) extends Value{
object Value {
case class File(path: String, pkg: List[String] = Nil) extends Value {
def prettyPrint = s"file $path"
}
case class Pkg(pkg: List[String]) extends Value{
case class Pkg(pkg: List[String]) extends Value {
def prettyPrint = s"package ${pkg.mkString(".")}"
}
object Pkg{
object Pkg {
def apply(s: String): Pkg = apply(s.split('.').toList)
}
}

trait GraphAnalysis{
trait GraphAnalysis {
val global: Global
import global._

case class Node[+T <: Value](value: T, dependencies: Map[Value, Seq[Tree]]){
case class Node[+T <: Value](value: T, dependencies: Map[Value, Seq[Tree]]) {
override def toString = s"DepNode(\n $value, \n ${dependencies.keys}\n)"
}

type DepNode = Node[Value]
type FileNode = Node[Value.File]
type PkgNode = Node[Value.Pkg]

object DepNode{
object DepNode {

/**
* Does a double Breadth-First-Search to find the shortest cycle starting
* from `from` within the DepNodes in `among`.
Expand All @@ -40,20 +41,20 @@ trait GraphAnalysis{
val nodeMap = among.map(n => n.value -> n).toMap
val distances = mutable.Map(from -> 0)
val queue = mutable.Queue(from)
while(queue.nonEmpty){
while (queue.nonEmpty) {
val next = queue.dequeue()
val children = next.dependencies
.keys
.collect(nodeMap)
.filter(!distances.contains(_))
.keys
.collect(nodeMap)
.filter(!distances.contains(_))

children.foreach(distances(_) = distances(next) + 1)
queue ++= children
}
var route = List(from)
while(route.length == 1 || route.head != from){
while (route.length == 1 || route.head != from) {
route ::= among.filter(x => x.dependencies.keySet.contains(route.head.value))
.minBy(distances)
.minBy(distances)
}
route.tail
}
Expand All @@ -64,7 +65,7 @@ trait GraphAnalysis{
* whose nodes are involved in the cycle.
*/
def stronglyConnectedComponents(nodes: Seq[DepNode]): Seq[Seq[DepNode]] = {

val nodeMap = nodes.map(n => n.value -> n).toMap

val components = mutable.Map.empty[DepNode, Int] ++ nodes.zipWithIndex.toMap
Expand All @@ -75,29 +76,29 @@ trait GraphAnalysis{
def rec(node: DepNode, path: List[DepNode]): Unit = {
if (path.exists(components(_) == components(node))) {
val cycle = path.reverse
.dropWhile(components(_) != components(node))
.dropWhile(components(_) != components(node))

val involved = cycle.map(components)
val firstIndex = involved.head
for ((n, i) <- components.toSeq){
if (involved.contains(i)){
for ((n, i) <- components.toSeq) {
if (involved.contains(i)) {
components(n) = firstIndex
}
}
} else if (!visited(node)) {
visited.add(node)
// sketchy sorting to make sure we're doing this deterministically...
for((key, lines) <- node.dependencies.toSeq.sortBy(_._1.toString)){
for ((key, lines) <- node.dependencies.toSeq.sortBy(_._1.toString)) {
rec(nodeMap(key), node :: path)
}
}
}

components.groupBy{case (node, i) => i}
.toSeq
.sortBy(_._1)
.map(_._2.keys.toSeq)
components.groupBy { case (node, i) => i }
.toSeq
.sortBy(_._1)
.map(_._2.keys.toSeq)
}
}

}
}
6 changes: 2 additions & 4 deletions acyclic/src/acyclic/plugin/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import tools.nsc.Global
import scala.collection.SortedSet

class RuntimePlugin(global: Global) extends TestPlugin(global)
class TestPlugin(val global: Global,
cycleReporter: Seq[(Value, SortedSet[Int])] => Unit = _ => ())
extends tools.nsc.plugins.Plugin {
class TestPlugin(val global: Global, cycleReporter: Seq[(Value, SortedSet[Int])] => Unit = _ => ())
extends tools.nsc.plugins.Plugin {

val name = "acyclic"

Expand All @@ -24,7 +23,6 @@ class TestPlugin(val global: Global,
}
val description = "Allows the developer to prohibit inter-file dependencies"


val components = List[tools.nsc.plugins.PluginComponent](
new PluginPhase(this.global, cycleReporter, force, fatal)
)
Expand Down
Loading

0 comments on commit b431f9a

Please sign in to comment.