Skip to content
This repository was archived by the owner on Sep 3, 2020. It is now read-only.

Commit d220e1f

Browse files
committed
Adds * as default import group.
1 parent 3114ea7 commit d220e1f

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

src/main/scala/scala/tools/refactoring/implementations/OrganizeImports.scala

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,36 @@ import scala.tools.refactoring.sourcegen.Formatting
1212
import scala.util.control.NonFatal
1313

1414
object OrganizeImports {
15+
val DefaultGroup = "*"
1516
/**
1617
* Abstract algorithms used by the implementation, extracted mostly for testing purposes
1718
*/
1819
private[implementations] object Algos {
1920
def groupImports[ImportT](getImportExpression: ImportT => String)(groups: Seq[String], imports: Seq[ImportT]): Seq[List[ImportT]] = {
2021
val distinctGroups = groups.distinct
21-
22-
case class Accumulator(remainingImports: Seq[ImportT] = imports, groups: Map[String, List[ImportT]] = Map()) {
23-
def assembleResult: Seq[List[ImportT]] = {
24-
val importGroups = distinctGroups.flatMap(groups.get(_))
25-
if (remainingImports.isEmpty) importGroups
26-
else importGroups :+ remainingImports.toList
27-
}
28-
}
29-
30-
distinctGroups.sortBy(-_.length).foldLeft(Accumulator()) { (acc, group) =>
31-
val (inGroup, remaining) = acc.remainingImports.partition { imp =>
32-
val expr = getImportExpression(imp)
22+
val acc = distinctGroups.map(_ -> scala.collection.mutable.ListBuffer.empty[ImportT]).toMap
23+
val assigned = imports.foldLeft(acc) { (acc, imp) =>
24+
val expr = getImportExpression(imp)
25+
val (inGroup, _) = distinctGroups.partition { group =>
3326
expr.startsWith(group + ".") || expr == group
3427
}
35-
36-
val newGroups = {
37-
if (inGroup.isEmpty) acc.groups
38-
else acc.groups + (group -> inGroup.toList)
28+
if (inGroup.nonEmpty) {
29+
val mostSpecificGroup = inGroup.sortBy(-_.length).head
30+
acc(mostSpecificGroup) += imp
3931
}
40-
41-
acc.copy(remainingImports = remaining, groups = newGroups)
42-
}.assembleResult
32+
acc
33+
}
34+
val unassigned = {
35+
val a = assigned.values.toList.flatten.map(getImportExpression)
36+
val (_, unassigned) = imports.partition { imp => a.contains(getImportExpression(imp)) }
37+
unassigned
38+
}
39+
if (assigned.keySet(DefaultGroup)) {
40+
assigned(DefaultGroup) ++= unassigned
41+
assigned.values.filter(_.nonEmpty).toSeq.map(_.toList)
42+
} else {
43+
assigned.values.filter(_.nonEmpty).toSeq.map(_.toList) :+ (unassigned.toList)
44+
}
4345
}
4446
}
4547

src/test/scala/scala/tools/refactoring/tests/implementations/imports/OrganizeImportsGroupsTest.scala

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,48 @@ class OrganizeImportsGroupsTest extends OrganizeImportsBaseTest {
380380
}
381381
"""
382382
} applyRefactoring organize(List("org", "scala", "java", "*"))
383+
384+
@Test
385+
def mostSpecificTakesPrecedence() = new FileSet {
386+
source becomes
387+
"""
388+
import scala.io.Source
389+
390+
import scala.collection.mutable.HashMap
391+
import scala.collection.mutable.ListBuffer
392+
393+
import java.util.AbstractList
394+
import java.util.BitSet
395+
import org.xml.sax.Attributes
396+
397+
trait Temp {
398+
// we need some code that use the imports
399+
val x: (ListBuffer[Int], HashMap[String, Int])
400+
val y: (AbstractList[Int], BitSet)
401+
val z: (Attributes, Source)
402+
}
403+
"""
404+
} applyRefactoring organize(List("scala", "scala.collection"))
405+
406+
@Test
407+
def fromSpecificToGeneral() = new FileSet {
408+
source becomes
409+
"""
410+
import scala.io.Source
411+
412+
import scala.collection.mutable.HashMap
413+
import scala.collection.mutable.ListBuffer
414+
415+
import java.util.AbstractList
416+
import java.util.BitSet
417+
import org.xml.sax.Attributes
418+
419+
trait Temp {
420+
// we need some code that use the imports
421+
val x: (ListBuffer[Int], HashMap[String, Int])
422+
val y: (AbstractList[Int], BitSet)
423+
val z: (Attributes, Source)
424+
}
425+
"""
426+
} applyRefactoring organize(List("scala.io", "scala"))
383427
}

0 commit comments

Comments
 (0)