Skip to content

Commit

Permalink
Merge pull request #873 from olafurpg/patch
Browse files Browse the repository at this point in the history
 Clean up Patch
  • Loading branch information
olafurpg authored Sep 20, 2018
2 parents a49d0e3 + 5d0cfdb commit 182806a
Show file tree
Hide file tree
Showing 51 changed files with 410 additions and 407 deletions.
4 changes: 3 additions & 1 deletion docs/developers/patch.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ doc.tree.collect {
It's best to target as precise tree nodes as possible when using `replaceTree()`
to avoid conflicts with other patches.

A common mistake is to use `replaceTree()` in combination with `
A common mistake is to use `replaceTree()` in combination with quasiquotes. The
tree node reference that is passed to `replaceTree()` must appear in the source
file in order to produce a diff.

### `removeImportee()`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package scala.meta.internal.scalafix

import scala.meta.Dialect
import scala.meta.Tree
import scala.meta.internal.trees.Origin
import scala.meta.dialects
import scala.meta.internal.trees.Origin

object ScalafixScalametaHacks {
def dialect(language: String): Dialect =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package scalafix.config

import metaconfig.{Conf, ConfDecoder}
import scalafix.v0.Symbol
import metaconfig.Conf
import metaconfig.ConfDecoder
import scalafix.internal.config.ScalafixMetaconfigReaders._
import scalafix.v0.Symbol

class CustomMessage[T](
val value: T,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package scalafix.internal.config

import scalafix.patch.Patch
import scalafix.patch.TreePatch.AddGlobalImport
import scalafix.patch.TreePatch.ReplaceSymbol
import scalafix.patch.TreePatch.RemoveGlobalImport
import metaconfig.ConfDecoder
import metaconfig.generic
import metaconfig.generic.Surface
import scalafix.patch.Patch
import scalafix.patch.Patch.internal._

case class ConfigRulePatches(
replaceSymbols: List[ReplaceSymbol] = Nil,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scalafix.internal.config

import metaconfig.generic
import metaconfig.ConfDecoder
import metaconfig.generic
import metaconfig.generic.Surface

case class DebugConfig(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package scalafix.internal.config

import metaconfig._
import scala.meta.io.AbsolutePath
import scala.util.matching.Regex
import metaconfig._

case class FilterMatcher(
includeFilters: Regex,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package scalafix.internal.config

import scalafix.lint.LintSeverity
import metaconfig.ConfDecoder
import metaconfig.generic
import metaconfig.generic.Surface
import scalafix.lint.LintSeverity

case class LintConfig(
explain: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package scalafix.internal.config

import scala.{meta => m}
import metaconfig.Input
import metaconfig.Position
import metaconfig.Conf
import metaconfig.ConfDecoder
import metaconfig.ConfError
import metaconfig.Configured
import metaconfig.Input
import metaconfig.Position
import metaconfig.internal.ConfGet
import scala.{meta => m}

object MetaconfigOps {
def traverse[T](lst: Seq[Configured[T]]): Configured[List[T]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package scalafix.internal.config
import java.io.PrintStream
import scala.meta.Position
import scalafix.internal.util.PositionSyntax._
import scalafix.lint.RuleDiagnostic
import scalafix.lint.LintSeverity
import scalafix.lint.RuleDiagnostic

/** A ScalafixReporter that emits messages to a PrintStream. */
case class PrintStreamReporter(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package scalafix.internal.config

import scala.reflect.ClassTag

import metaconfig.ConfDecoder
import metaconfig.Conf
import metaconfig.ConfDecoder
import metaconfig.ConfError
import metaconfig.Configured
import scala.reflect.ClassTag

object ReaderUtil {
def oneOf[T: ClassTag](options: sourcecode.Text[T]*): ConfDecoder[T] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package scalafix.internal.config

import scala.meta._
import scala.meta.dialects.Scala212
import metaconfig._
import metaconfig.generic.Surface
import scala.meta._
import scala.meta.dialects.Scala212

case class ScalafixConfig(
parser: ParserConfig = ParserConfig(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package scalafix.internal.config

import scala.meta.Ref
import scala.meta._
import scala.meta.parsers.Parse
import scalafix.v0.Symbol
import scala.reflect.ClassTag
import scala.util.Try
import scala.util.matching.Regex
import scalafix.patch.TreePatch._
import java.io.OutputStream
import java.io.PrintStream
import java.net.URI
import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException
import scala.util.control.NonFatal
import metaconfig.Conf
import metaconfig.ConfDecoder
import metaconfig.ConfError
import metaconfig.Configured
import metaconfig.Configured.Ok
import scala.meta.Ref
import scala.meta._
import scala.meta.parsers.Parse
import scala.reflect.ClassTag
import scala.util.Try
import scala.util.control.NonFatal
import scala.util.matching.Regex
import scalafix.internal.util.SymbolOps
import scalafix.patch.Patch.internal._
import scalafix.v0.Symbol
import scalafix.v0._

object ScalafixMetaconfigReaders extends ScalafixMetaconfigReaders
Expand All @@ -31,8 +31,8 @@ trait ScalafixMetaconfigReaders {
ReaderUtil.oneOf[MetaParser](parseSource, parseStat, parseCase)
}
implicit lazy val dialectReader: ConfDecoder[Dialect] = {
import scala.meta.dialects._
import ScalafixConfig.{DefaultDialect => Default}
import scala.meta.dialects._
ReaderUtil.oneOf[Dialect](
Default,
Scala211,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package scalafix.internal.config
import metaconfig.ConfDecoder
import metaconfig.ConfEncoder
import scala.meta.Position
import scalafix.lint.RuleDiagnostic
import scalafix.lint.LintSeverity
import scalafix.lint.RuleDiagnostic

trait ScalafixReporter {
private[scalafix] def report(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package scalafix.internal.diff

import scala.meta.inputs.Input
import scala.meta.Position

import scala.collection.mutable.StringBuilder

import scala.meta.Position
import scala.meta.inputs.Input
import scalafix.internal.util.IntervalSet

object DiffDisable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package scalafix.internal.patch

import scala.meta.Classpath
import scalafix.v0.SemanticdbIndex
import scalafix.v0.Document
import scalafix.v0.SemanticdbIndex

trait CrashingSemanticdbIndex extends SemanticdbIndex {
final override def classpath: Classpath =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import scala.meta._
import scala.meta.contrib._
import scala.meta.tokens.Token
import scala.meta.tokens.Token.Comment
import scalafix.v0._
import scalafix.internal.config.FilterMatcher
import scalafix.internal.patch.EscapeHatch._
import scalafix.internal.util.LintSyntax._
import scalafix.lint.RuleDiagnostic
import scalafix.patch.AtomicPatch
import scalafix.patch.Concat
import scalafix.patch.EmptyPatch
import scalafix.patch.LintPatch
import scalafix.patch.Patch.internal._
import scalafix.rule.RuleName
import scalafix.util.TreeExtractors.Mods
import scalafix.internal.util.LintSyntax._
import scalafix.v0._

/** EscapeHatch is an algorithm to selectively disable rules. There
* are two mechanisms to do so: anchored comments and the
Expand All @@ -37,7 +34,7 @@ class EscapeHatch private (
val diagnostics = List.newBuilder[RuleDiagnostic]
patchesByName.foreach {
case (rule, rulePatch) =>
Patch.foreach(rulePatch) {
PatchInternals.foreach(rulePatch) {
case LintPatch(message) =>
diagnostics += message.toDiagnostic(rule, ctx.config)
case rewritePatch =>
Expand Down Expand Up @@ -75,7 +72,7 @@ class EscapeHatch private (
def loop(name: RuleName, patch: Patch): Patch = patch match {
case AtomicPatch(underlying) =>
val hasDisabledPatch = {
val patches = Patch.treePatchApply(underlying)(ctx, index)
val patches = PatchInternals.treePatchApply(underlying)(ctx, index)
patches.exists { tp =>
val byGit = diff.isDisabled(tp.tok.pos)
val byEscape = isDisabledByEscape(name, tp.tok.pos.start)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package scalafix.internal.patch

import scala.annotation.tailrec
import scala.collection.immutable.Seq
import scala.collection.mutable
import scala.meta._
import scalafix.v0.Symbol
import scalafix.v0.Signature
import scalafix.internal.util.SymbolOps
import scalafix.patch.Patch
import scalafix.patch.TreePatch
import scalafix.patch.TreePatch.ImportPatch
import scalafix.patch.Patch.internal._
import scalafix.rule.RuleCtx
import scalafix.syntax._
import scalafix.util.Newline
import scalafix.util.SemanticdbIndex
import scalafix.v0.Signature
import scalafix.v0.Symbol

object ImportPatchOps {
object symbols {
Expand Down Expand Up @@ -89,11 +87,11 @@ object ImportPatchOps {
}
val isRemovedImportee = mutable.LinkedHashSet.empty[Importee]
importPatches.foreach {
case TreePatch.RemoveGlobalImport(sym) =>
case RemoveGlobalImport(sym) =>
allImporteeSymbols
.withFilter(_._1 == sym.normalized)
.foreach { case (_, x) => isRemovedImportee += x }
case x: TreePatch.RemoveImportee => isRemovedImportee += x.importee
case x: RemoveImportee => isRemovedImportee += x.importee
case _ =>
}
val importersToAdd = {
Expand All @@ -108,13 +106,13 @@ object ImportPatchOps {
isAlreadyImported += underlying
}
importPatches.flatMap {
case TreePatch.AddGlobalSymbol(symbol)
case AddGlobalSymbol(symbol)
if !allNamedImports.contains(symbol) &&
!isAlreadyImported(symbol) &&
!isPredef(symbol) =>
isAlreadyImported += symbol
SymbolOps.toImporter(symbol).toList
case TreePatch.AddGlobalImport(importer)
case AddGlobalImport(importer)
// best effort deduplication for syntactic addGlobalImport(Importer)
if !allImportersSyntax.contains(importer.syntax) =>
importer :: Nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package scalafix.internal.patch

import scala.meta.Importee
import scala.meta.Importer
import scalafix.v0.Symbol
import scala.meta.Token
import scala.meta.Tree
import scala.meta.tokens.Tokens
import scalafix.v0._
import scalafix.internal.patch.LegacyPatchOps.DeprecationMessage
import scalafix.internal.util.SymbolOps.Root
import scalafix.patch.Patch.internal._
import scalafix.patch.PatchOps
import DeprecatedPatchOps.DeprecationMessage
import scalafix.v0.Symbol
import scalafix.v0._

trait DeprecatedPatchOps extends PatchOps {
trait LegacyPatchOps extends PatchOps {
@deprecated(DeprecationMessage, "0.6.0")
final def removeImportee(importee: Importee): Patch =
Patch.removeImportee(importee)
Expand Down Expand Up @@ -49,15 +51,15 @@ trait DeprecatedPatchOps extends PatchOps {
@deprecated(DeprecationMessage, "0.6.0")
final def removeGlobalImport(symbol: Symbol)(
implicit index: SemanticdbIndex): Patch =
Patch.removeGlobalImport(symbol)
RemoveGlobalImport(symbol)
@deprecated(DeprecationMessage, "0.6.0")
final def addGlobalImport(symbol: Symbol)(
implicit index: SemanticdbIndex): Patch =
Patch.addGlobalImport(symbol)
AddGlobalSymbol(symbol)
@deprecated(DeprecationMessage, "0.6.0")
final def replaceSymbol(fromSymbol: Symbol.Global, toSymbol: Symbol.Global)(
implicit index: SemanticdbIndex): Patch =
Patch.replaceSymbol(fromSymbol, toSymbol)
ReplaceSymbol(fromSymbol, toSymbol)
@deprecated(DeprecationMessage, "0.6.0")
final def replaceSymbols(toReplace: (String, String)*)(
implicit index: SemanticdbIndex): Patch =
Expand All @@ -70,12 +72,12 @@ trait DeprecatedPatchOps extends PatchOps {
@deprecated(DeprecationMessage, "0.6.0")
final def renameSymbol(fromSymbol: Symbol.Global, toName: String)(
implicit index: SemanticdbIndex): Patch =
Patch.renameSymbol(fromSymbol, toName)
ReplaceSymbol(fromSymbol, Root(Signature.Term(toName)))
@deprecated(DeprecationMessage, "0.6.0")
final def lint(msg: Diagnostic): Patch =
Patch.lint(msg)
}

object DeprecatedPatchOps {
object LegacyPatchOps {
private[scalafix] final val DeprecationMessage = "Use scalafix.Patch instead"
}
Loading

0 comments on commit 182806a

Please sign in to comment.