Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mlachkar committed Aug 26, 2020
1 parent f0e3ce9 commit fa7e05f
Show file tree
Hide file tree
Showing 17 changed files with 179 additions and 242 deletions.
2 changes: 1 addition & 1 deletion project/Mima.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Mima {
ProblemFilters.exclude[MissingTypesProblem]("scalafix.testkit.DiffAssertions"),
ProblemFilters.exclude[MissingTypesProblem]("scalafix.testkit.SemanticRuleSuite"),
ProblemFilters.exclude[Problem]("scalafix.internal.*"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scalafix.interfaces.ScalafixArguments.runAndReturnResult")
ProblemFilters.exclude[ReversedMissingMethodProblem]("scalafix.interfaces.ScalafixArguments.evaluate")
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import scalafix.interfaces.{
ScalafixException,
ScalafixMainCallback,
ScalafixMainMode,
ScalafixResult,
ScalafixEvaluation,
ScalafixRule
}
import scalafix.internal.v1.Args
Expand All @@ -40,7 +40,7 @@ final case class ScalafixArgumentsImpl(args: Args = Args.default)
ScalafixErrorImpl.fromScala(exit)
}

override def runAndReturnResult(): ScalafixResult = {
override def evaluate(): ScalafixEvaluation = {
args.validate match {
case Configured.Ok(validated) =>
MainOps.runWithResult(validated)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package scalafix.internal.interfaces

import java.lang
import java.nio.file.Path
import java.util.Optional

import scalafix.cli.ExitStatus
import scalafix.interfaces.{
ScalafixDiagnostic,
Expand All @@ -11,6 +11,7 @@ import scalafix.interfaces.{
ScalafixPatch,
ScalafixRule
}
import scalafix.internal.diff.DiffUtils
import scalafix.internal.v1.{MainOps, ValidatedArgs}
import scalafix.lint.RuleDiagnostic
import scalafix.internal.util.OptionOps._
Expand All @@ -19,10 +20,9 @@ import scalafix.v0.RuleCtx

import scala.meta.io.AbsolutePath

final case class ScalafixOutputtImpl(
final case class ScalafixOutputImpl(
originalPath: AbsolutePath,
fixed: Option[String],
unifiedDiff: Option[String],
fixedOpt: Option[String],
error: ExitStatus,
errorMessage: Option[String],
diagnostics: Seq[RuleDiagnostic],
Expand All @@ -41,14 +41,31 @@ final case class ScalafixOutputtImpl(

override def getRules: Array[ScalafixRule] = rules.toArray

override def getOutputFileFixed(): Optional[String] = fixed.asJava

override def getUnifiedDiff: Optional[String] = unifiedDiff.asJava
override def getOutputFixed(): Optional[String] = fixedOpt.asJava

override def getUnifiedDiff: Optional[String] = {
fixedOpt
.flatMap(fixed => {
val input = args.input(originalPath).text
if (input == fixed) None
else
Some(
DiffUtils.unifiedDiff(
originalPath.toString(),
"<expected fix>",
input.linesIterator.toList,
fixed.linesIterator.toList,
3
)
)
})
.asJava
}

override def getError(): Array[ScalafixError] =
override def getErrors(): Array[ScalafixError] =
ScalafixErrorImpl.fromScala(error)

override def isSuccessful: lang.Boolean = error.isOk
override def isSuccessful: Boolean = error.isOk

override def getDiagnostics: Array[ScalafixDiagnostic] =
diagnostics.map(ScalafixDiagnosticImpl.fromScala).toArray
Expand All @@ -58,19 +75,20 @@ final case class ScalafixOutputtImpl(
}

override def applyPatches(): Array[ScalafixError] = {
val exitStatus = fixed.toTry
.flatMap(MainOps.appyDiff(args, file = originalPath, _))
val exitStatus = fixedOpt.toTry
.flatMap(MainOps.applyDiff(args, file = originalPath, _))
.getOrElse(ExitStatus.UnexpectedError)
ScalafixErrorImpl.fromScala(exitStatus).toSeq.toArray
}

override def getOutputFixedWithSelectivePatches(
selectedPatches: Array[ScalafixPatch]
): Optional[String] = {
val ids = selectedPatches.toList.map(_.getId())
val filteredPatches = patches.filter {
case ScalafixPatchImpl(id, _) => ids.contains(id.value)
}
val selectedPatchesSet =
new java.util.IdentityHashMap[ScalafixPatch, Unit](selectedPatches.length)
for (patch <- selectedPatches)
selectedPatchesSet.put(patch, ())
val filteredPatches = patches.filter(selectedPatchesSet.containsKey(_))
ctxOpt
.flatMap(ctx =>
MainOps.getFixedOutput(filteredPatches.map(_.patch), ctx, index)
Expand All @@ -80,10 +98,11 @@ final case class ScalafixOutputtImpl(
override def applySelectivePatches(
selectedPatches: Array[ScalafixPatch]
): Array[ScalafixError] = {
val ids = selectedPatches.toList.map(_.getId())
val filteredPatches = patches.filter {
case ScalafixPatchImpl(id, _) => ids.contains(id.value)
}
val selectedPatchesSet =
new java.util.IdentityHashMap[ScalafixPatch, Unit](selectedPatches.length)
for (patch <- selectedPatches)
selectedPatchesSet.put(patch, ())
val filteredPatches = patches.filter(selectedPatchesSet.containsKey(_))
val exitStatus =
ctxOpt.toTry
.flatMap(ctx =>
Expand All @@ -101,25 +120,23 @@ final case class ScalafixOutputtImpl(

}

object ScalafixOutputtImpl {
object ScalafixOutputImpl {

def from(
originalPath: AbsolutePath,
fixed: Option[String],
unifiedDiff: Option[String],
exitStatus: ExitStatus,
patches: Seq[v0.Patch],
diagnostics: Seq[RuleDiagnostic]
)(
args: ValidatedArgs,
ctx: RuleCtx,
index: Option[v0.SemanticdbIndex]
): ScalafixOutputtImpl = {
val scalafixPatches = patches.map(ScalafixPatchImpl.from)
ScalafixOutputtImpl(
): ScalafixOutputImpl = {
val scalafixPatches = patches.map(ScalafixPatchImpl)
ScalafixOutputImpl(
originalPath = originalPath,
fixed = fixed,
unifiedDiff = unifiedDiff,
fixedOpt = fixed,
error = exitStatus,
errorMessage = None,
diagnostics = diagnostics,
Expand All @@ -132,11 +149,10 @@ object ScalafixOutputtImpl {
errorMessage: String
)(
args: ValidatedArgs
): ScalafixOutputtImpl =
ScalafixOutputtImpl(
): ScalafixOutputImpl =
ScalafixOutputImpl(
originalPath,
None,
None,
exitStatus,
Some(errorMessage),
Nil,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,8 @@
package scalafix.internal.interfaces

import java.util.UUID

import scalafix.interfaces.ScalafixPatch
import scalafix.Patch

import scala.util.Try

case class ScalafixPatchImpl(id: ScalafixPatchImpl.Id, patch: Patch)
extends ScalafixPatch {
case class ScalafixPatchImpl(patch: Patch) extends ScalafixPatch {
override def kind(): String = patch.productPrefix

override def getId: String = id.value

}

object ScalafixPatchImpl {

case class Id(value: String) {
assert(Id.isValid(value), s"Invalid format for ${getClass.getName}: $value")

override def toString: String = value
}

object Id {
def generate(): Id = Id(UUID.randomUUID().toString)

def isValid(in: String): Boolean = Try(UUID.fromString(in)).isSuccess

def from(in: String): Try[Id] = Try(Id(in))
}

def from(patch: Patch): ScalafixPatchImpl =
ScalafixPatchImpl(Id.generate(), patch)

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package scalafix.internal.interfaces

import java.lang
import java.nio.file.Path
import java.util.Optional

import scalafix.cli.ExitStatus
import scalafix.interfaces.{ScalafixError, ScalafixOutput, ScalafixResult}
import scalafix.interfaces.{ScalafixError, ScalafixOutput, ScalafixEvaluation}
import scalafix.internal.util.OptionOps._

final case class ScalafixResultImpl(
error: ExitStatus,
getMessageError: Optional[String],
scalafixOutputs: Seq[ScalafixOutputtImpl]
) extends ScalafixResult {
scalafixOutputs: Seq[ScalafixOutputImpl]
) extends ScalafixEvaluation {

override def isSuccessful: lang.Boolean = error.isOk
override def isSuccessful: Boolean = error.isOk

override def getError: Array[ScalafixError] = {
ScalafixErrorImpl.fromScala(error)
Expand All @@ -38,7 +36,7 @@ object ScalafixResultImpl {
}

def from(
scalafixOutputtImpl: Seq[ScalafixOutputtImpl],
scalafixOutputtImpl: Seq[ScalafixOutputImpl],
exitStatus: ExitStatus
): ScalafixResultImpl = {
ScalafixResultImpl(exitStatus, Optional.empty(), scalafixOutputtImpl)
Expand Down
Loading

0 comments on commit fa7e05f

Please sign in to comment.