-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Refractoring Sortimports - ImportGroup abstract List[Import] and operations on it - Use List instead of ListBuffer when possible - Rename variables for better comprehension * Allow sorting with priority on wildcard and groups - Add asciiSort boolean on configuration (default true) - Add WildcardAndGroupFirstSort SortWith implementation
- Loading branch information
Showing
9 changed files
with
231 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
rule = SortImports | ||
SortImports.blocks = [ | ||
"java", | ||
"scala", | ||
"*" | ||
] | ||
*/ | ||
package fix | ||
|
||
import scala._ | ||
import scala.Console._ | ||
|
||
import java.util.{ Base64, HashMap } | ||
import java.util.regex.Matcher | ||
|
||
object AsciiSort { | ||
// Add code that needs fixing here. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
rule = SortImports | ||
SortImports.blocks = [ | ||
"java", | ||
"scala", | ||
"*" | ||
] | ||
SortImports.asciiSort = false | ||
*/ | ||
package fix | ||
|
||
import scala.Console._ | ||
import scala._ | ||
|
||
import java.util.regex.Matcher | ||
import java.util.{ Base64, HashMap } | ||
|
||
object NonAsciiSort { | ||
// Add code that needs fixing here. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package fix | ||
|
||
import java.util.regex.Matcher | ||
import java.util.{ Base64, HashMap } | ||
|
||
import scala.Console._ | ||
import scala._ | ||
|
||
object AsciiSort { | ||
// Add code that needs fixing here. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package fix | ||
|
||
import java.util.{ Base64, HashMap } | ||
import java.util.regex.Matcher | ||
|
||
import scala._ | ||
import scala.Console._ | ||
|
||
object NonAsciiSort { | ||
// Add code that needs fixing here. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package fix | ||
|
||
import scala.collection.mutable.ListBuffer | ||
import scala.meta.contrib.AssociatedComments | ||
import scala.meta.inputs.Position | ||
import scala.meta.tokens.Token | ||
import scala.meta.{ Import, Traverser, Tree } | ||
|
||
object ImportGroupTraverser { | ||
def retrieveImportGroups(tree: Tree): List[ImportGroup] = { | ||
val importGroupsBuffer = ListBuffer[ListBuffer[Import]](ListBuffer.empty) | ||
val importTraverser = new ImportGroupTraverser(importGroupsBuffer) | ||
importTraverser(tree) | ||
importGroupsBuffer.map(importGroupBuffer => ImportGroup(importGroupBuffer.toList)).toList | ||
} | ||
} | ||
|
||
private class ImportGroupTraverser(listBuffer: ListBuffer[ListBuffer[Import]]) extends Traverser { | ||
override def apply(tree: Tree): Unit = tree match { | ||
case x: Import => listBuffer.last.append(x) | ||
case node => | ||
listBuffer.append(ListBuffer.empty) | ||
super.apply(node) | ||
} | ||
} | ||
|
||
object ImportGroup { | ||
|
||
val empty: ImportGroup = ImportGroup(Nil) | ||
} | ||
|
||
case class ImportGroup(value: List[Import]) extends Traversable[Import] { | ||
|
||
def sortWith(ordering: Ordering[Import]): ImportGroup = ImportGroup(value.sortWith(ordering.lt)) | ||
|
||
def groupByBlock(blocks: List[String], defaultBlock: String): Map[String, ImportGroup] = | ||
value.groupBy { imp => | ||
blocks | ||
.find(block => imp.children.head.syntax.startsWith(block)) | ||
.getOrElse(defaultBlock) | ||
}.mapValues(ImportGroup(_)) | ||
|
||
def containPosition(pos: Position): Boolean = | ||
pos.start > value.head.pos.start && pos.end < value.last.pos.end | ||
|
||
def trailingComment(comments: AssociatedComments): Map[Import, Token.Comment] = | ||
value | ||
.map(currentImport => currentImport -> comments.trailing(currentImport).headOption) | ||
.collect { | ||
case (imp, comment) if comment.nonEmpty => (imp, comment.get) | ||
} | ||
.toMap | ||
|
||
override def nonEmpty: Boolean = value.nonEmpty | ||
|
||
override def foreach[U](f: Import => U): Unit = value.foreach(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package fix | ||
|
||
import scala.meta.Import | ||
import WildcardAndGroupFirstSort._ | ||
|
||
sealed trait ImportOrdering extends Ordering[Import] { | ||
|
||
protected def strFirstImport(imp: Import): String = | ||
imp.children.head.syntax | ||
} | ||
|
||
class DefaultSort extends ImportOrdering { | ||
|
||
override def compare(x: Import, y: Import): Int = | ||
strFirstImport(x).compareTo(strFirstImport(y)) | ||
} | ||
|
||
object WildcardAndGroupFirstSort { | ||
|
||
private val wildcardRegex = "_".r | ||
private val groupRegex = "\\{.+\\}".r | ||
} | ||
|
||
class WildcardAndGroupFirstSort extends ImportOrdering { | ||
|
||
private def transformForSorting(imp: Import): (String, String) = { | ||
val strImp = strFirstImport(imp) | ||
(strImp, groupRegex.replaceAllIn(wildcardRegex.replaceAllIn(strImp, "\0"), "\1")) | ||
} | ||
|
||
override def compare(x: Import, y: Import): Int = | ||
(transformForSorting(x), transformForSorting(y)) match { | ||
case ((strImp1, tranformedStrImp1), (strImp2, tranformedStrImp2)) => | ||
val transformComparison = tranformedStrImp1.compareTo(tranformedStrImp2) | ||
if (transformComparison != 0) transformComparison else strImp1.compareTo(strImp2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters