Permalink
Browse files

Adds alternative grouping like 'akka,java'.

  • Loading branch information...
wpopielarski committed Aug 17, 2017
1 parent bea75b3 commit 2c61073cbb09236638faa8cc33e60dc5d9f58c97
@@ -13,23 +13,40 @@ import scala.util.control.NonFatal
object OrganizeImports {
val DefaultGroup = "*"
val InGroupDelimiter = ","
/**
* Abstract algorithms used by the implementation, extracted mostly for testing purposes
*/
private[implementations] object Algos {
def groupImports[ImportT](getImportExpression: ImportT => String)(groups: Seq[String], imports: Seq[ImportT]): Seq[List[ImportT]] = {
val distinctGroups = groups.distinct
val acc = distinctGroups.map(_ -> scala.collection.mutable.ListBuffer.empty[ImportT]).toMap
val assigned = imports.foldLeft(acc) { (acc, imp) =>
val distinctGroups = groups.map(_.split(InGroupDelimiter).sorted.mkString(InGroupDelimiter)).distinct
val groupImportsMap = distinctGroups.map(_ -> scala.collection.mutable.ListBuffer.empty[ImportT]).toMap
val assigned = imports.foldLeft(groupImportsMap) { (groupImportsMap, imp) =>
val expr = getImportExpression(imp)
val inGroup = distinctGroups.filter { group =>
expr.startsWith(group + ".") || expr == group
def isInGroup(group: String) = expr.startsWith(group + ".") || expr == group
distinctGroups.foldLeft[Option[String]](None) { (longestGroup, group) =>
val potential = group.split(InGroupDelimiter).exists { isInGroup }
if (potential) {
val locallyLongest = group.split(InGroupDelimiter).foldLeft(0) { (locallyLongest, group) =>
if (isInGroup(group) && group.length > locallyLongest) {
group.length
} else locallyLongest
}
longestGroup.map { lg =>
val longest = lg.split(InGroupDelimiter).foldLeft(0) { (longest, g) =>
if (isInGroup(g) && g.length > longest)
g.length
else longest
}
if (longest < locallyLongest)
group
else lg
}.orElse(Option(group))
} else longestGroup
}.map { mostSpecificGroup =>
groupImportsMap(mostSpecificGroup) += imp
}
if (inGroup.nonEmpty) {
val mostSpecificGroup = inGroup.sortBy(-_.length).head
acc(mostSpecificGroup) += imp
}
acc
groupImportsMap
}
val unassigned = {
val a = assigned.values.toList.flatten.map(getImportExpression)
@@ -46,6 +46,42 @@ class OrganizeImportsAlgosTest {
expected = List(List("a.A"), List("a.b.AB"), List("ab.Ab1", "ab.Ab2"), List("abc.Abc1", "abc.Abc2")))
}
@Test
def testGroupImportsWithAlternatives(): Unit = {
testGroupImports(
groups = List("a.c", "a,b.c", "b"),
imports = List("a.b.X", "a.c.Y", "b.a.Y", "b.c.X"),
expected = List(List("a.c.Y"), List("a.b.X", "b.c.X"), List("b.a.Y")))
testGroupImports(
groups = List("a.c", "nonexisting", "a,b.c", "b"),
imports = List("a.b.X", "a.c.Y", "b.a.Y", "b.c.X"),
expected = List(List("a.c.Y"), List("a.b.X", "b.c.X"), List("b.a.Y")))
testGroupImports(
groups = List("a.c", "*", "a,b.c", "b"),
imports = List("a.b.X", "a.c.Y", "b.a.Y", "b.c.X", "d.a.Y"),
expected = List(List("a.c.Y"), List("d.a.Y"), List("a.b.X", "b.c.X"), List("b.a.Y")))
// default group at the end
testGroupImports(
groups = List("a.c", "a,b.c", "b"),
imports = List("a.b.X", "a.c.Y", "b.a.Y", "b.c.X", "d.a.Y"),
expected = List(List("a.c.Y"), List("a.b.X", "b.c.X"), List("b.a.Y"), List("d.a.Y")))
// disordered alternative
testGroupImports(
groups = List("a.c", "b.c,a", "b"),
imports = List("a.b.X", "a.c.Y", "b.a.Y", "b.c.X"),
expected = List(List("a.c.Y"), List("a.b.X", "b.c.X"), List("b.a.Y")))
// disordered and repeated alternative
testGroupImports(
groups = List("a.c", "b.c,a", "b", "a,b.c"),
imports = List("a.b.X", "a.c.Y", "b.a.Y", "b.c.X"),
expected = List(List("a.c.Y"), List("a.b.X", "b.c.X"), List("b.a.Y")))
}
private def testGroupImports(groups: List[String], imports: List[String], expected: List[List[String]]): Unit = {
def getImportExpr(imp: String) = {
val lastDot = imp.lastIndexOf('.')

0 comments on commit 2c61073

Please sign in to comment.