-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit is too big, I'm sorry. Main changes: - Patch(from, to, replace) replaced with TokenPatch(token, newToken) with helpful combinators such as Add{Left,Right} - New high-level TreePatch datatype with can be converted to TokenPatch. - Implemented two tree patches, AddGlobalImport and RemoveGlobalImport. - Implemented OrganizeImport, which is a prerequisite to use import tree patches. - OrganizeImports - orders imports by configurable groups - removes unused imports using scalac -Ywarn-unused-import infrastructure. The implementation is requires hijacking a few private fields in g.analyzer using evil reflection hackery. - option to expand relative imports - handles renames - configurable "always used" to force keeping an import such as acyclic.file Known bugs: - introduces false import on non-existing cats.data.UUID in circe - makes an import unused by expanding relative imports to fully-qualified imports, requiring you to run scalafix twice to fully remove unused imports (e.g. in cats with -Ywarn-unused-import) - crashes on scala.meta parser bug (in akka), not scalafix problem really. Expand relative Test that patch is unchanged
- Loading branch information
Showing
48 changed files
with
5,295 additions
and
320 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 @@ | ||
imports.optimize = true |
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 @@ | ||
scalac -Ywarn-unused-import -Yrangepos -Xplugin:/Users/ollie/.ivy2/local/ch.epfl.scala/scalafix-nsc_2.11/0.2.0-SNAPSHOT/jars/scalafix-nsc_2.11.jar $1 |
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,27 @@ | ||
package scalafix | ||
|
||
import scala.util.matching.Regex | ||
import scalafix.util.AbsoluteFile | ||
|
||
case class FilterMatcher(include: Regex, exclude: Regex) { | ||
def matches(file: AbsoluteFile): Boolean = matches(file.path) | ||
def matches(input: String): Boolean = | ||
include.findFirstIn(input).isDefined && | ||
exclude.findFirstIn(input).isEmpty | ||
} | ||
|
||
object FilterMatcher { | ||
val matchEverything = new FilterMatcher(".*".r, mkRegexp(Nil)) | ||
|
||
def mkRegexp(filters: Seq[String]): Regex = | ||
filters match { | ||
case Nil => "$a".r // will never match anything | ||
case head :: Nil => head.r | ||
case _ => filters.mkString("(", "|", ")").r | ||
} | ||
|
||
def apply(includes: Seq[String], excludes: Seq[String]): FilterMatcher = | ||
new FilterMatcher(mkRegexp(includes), mkRegexp(excludes)) | ||
def apply(include: String): FilterMatcher = | ||
new FilterMatcher(mkRegexp(Seq(include)), mkRegexp(Nil)) | ||
} |
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
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
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package scalafix.rewrite | ||
import scalafix.ScalafixConfig | ||
import scalafix.util.AssociatedComments | ||
import scalafix.util.TokenList | ||
|
||
case class RewriteCtx( | ||
config: ScalafixConfig, | ||
tokenList: TokenList, | ||
comments: AssociatedComments, | ||
semantic: Option[SemanticApi] | ||
) |
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
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,31 @@ | ||
package scalafix | ||
|
||
import scala.meta.Importee | ||
import scala.meta.tokens.Token | ||
import scalafix.util.CanonicalImport | ||
import scalafix.util.ImportPatch | ||
import scalafix.util.logger | ||
|
||
import com.typesafe.config.Config | ||
|
||
package object syntax { | ||
implicit class XtensionImporter(i: CanonicalImport) { | ||
def supersedes(patch: ImportPatch): Boolean = | ||
i.ref.structure == patch.importer.ref.structure && | ||
(i.importee.is[Importee.Wildcard] || | ||
i.importee.structure == patch.importer.importee.structure) | ||
} | ||
|
||
implicit class XtensionToken(token: Token) { | ||
def posTuple: (Int, Int) = token.start -> token.end | ||
} | ||
|
||
implicit class XtensionConfig(config: Config) { | ||
def getBoolOrElse(key: String, els: Boolean): Boolean = | ||
if (config.hasPath(key)) config.getBoolean(key) | ||
else els | ||
} | ||
implicit class XtensionString(str: String) { | ||
def reveal: String = logger.reveal(str) | ||
} | ||
} |
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,31 @@ | ||
package scalafix.util | ||
|
||
import java.io.File | ||
|
||
/** Wrapper around java.io.File with an absolute path. */ | ||
sealed abstract case class AbsoluteFile(jfile: File) { | ||
def path: String = jfile.getAbsolutePath | ||
def /(other: String) = new AbsoluteFile(new File(jfile, other)) {} | ||
} | ||
|
||
object AbsoluteFile { | ||
def fromFiles(files: Seq[File], | ||
workingDir: AbsoluteFile): Seq[AbsoluteFile] = { | ||
files.map(x => fromFile(x, workingDir)) | ||
} | ||
private def makeAbsolute(workingDir: File)(file: File): File = | ||
if (file.isAbsolute) file | ||
else new File(workingDir, file.getPath) | ||
|
||
// If file is already absolute, then workingDir is not used. | ||
def fromFile(file: File, workingDir: AbsoluteFile): AbsoluteFile = { | ||
new AbsoluteFile(makeAbsolute(workingDir.jfile)(file)) {} | ||
} | ||
def fromPath(path: String): Option[AbsoluteFile] = { | ||
val file = new File(path) | ||
if (file.isAbsolute) Some(new AbsoluteFile(file) {}) | ||
else None | ||
} | ||
def userDir = new AbsoluteFile(new File(System.getProperty("user.dir"))) {} | ||
def homeDir = new AbsoluteFile(new File(System.getProperty("user.home"))) {} | ||
} |
Oops, something went wrong.