-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods to scalafix api to be able to retrieve patches (#1223)
* Add methods to scalafix api to be able to retrieve patches * be able to apply selectives patches * Adjust exitcode as it was in the previous MainOps.run * Add test for applySelectivePatches * Fix mima test and skip windows * Add missing documentation * Various fixes * Various fixes 2 * Add a test to verify that withMode and withCallback are not taken into account when evaluate is called
- Loading branch information
Showing
24 changed files
with
864 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
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
49 changes: 49 additions & 0 deletions
49
scalafix-cli/src/main/scala/scalafix/internal/interfaces/ScalafixEvaluationImpl.scala
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,49 @@ | ||
package scalafix.internal.interfaces | ||
|
||
import java.util.Optional | ||
|
||
import scalafix.cli.ExitStatus | ||
import scalafix.interfaces.{ | ||
ScalafixError, | ||
ScalafixFileEvaluation, | ||
ScalafixEvaluation | ||
} | ||
import scalafix.internal.util.OptionOps._ | ||
|
||
final case class ScalafixEvaluationImpl( | ||
error: ExitStatus, | ||
getMessageError: Optional[String], | ||
fileEvaluations: Seq[ScalafixFileEvaluationImpl] | ||
) extends ScalafixEvaluation { | ||
|
||
override def isSuccessful: Boolean = error.isOk | ||
|
||
override def getErrors: Array[ScalafixError] = { | ||
ScalafixErrorImpl.fromScala(error) | ||
} | ||
|
||
override def getFileEvaluations: Array[ScalafixFileEvaluation] = | ||
fileEvaluations.toArray | ||
|
||
override def apply(): Array[ScalafixError] = { | ||
fileEvaluations.flatMap(o => o.applyPatches()).toArray | ||
} | ||
} | ||
|
||
object ScalafixEvaluationImpl { | ||
|
||
def apply( | ||
error: ExitStatus, | ||
errorMessage: Option[String] = None | ||
): ScalafixEvaluationImpl = { | ||
new ScalafixEvaluationImpl(error, errorMessage.asJava, Nil) | ||
} | ||
|
||
def from( | ||
fileEvaluations: Seq[ScalafixFileEvaluationImpl], | ||
exitStatus: ExitStatus | ||
): ScalafixEvaluationImpl = { | ||
ScalafixEvaluationImpl(exitStatus, Optional.empty(), fileEvaluations) | ||
} | ||
|
||
} |
160 changes: 160 additions & 0 deletions
160
scalafix-cli/src/main/scala/scalafix/internal/interfaces/ScalafixFileEvaluationImpl.scala
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,160 @@ | ||
package scalafix.internal.interfaces | ||
|
||
import java.nio.file.Path | ||
import java.util.Optional | ||
|
||
import scalafix.cli.ExitStatus | ||
import scalafix.interfaces.{ | ||
ScalafixDiagnostic, | ||
ScalafixError, | ||
ScalafixFileEvaluation, | ||
ScalafixPatch, | ||
ScalafixRule | ||
} | ||
import scalafix.internal.diff.DiffUtils | ||
import scalafix.internal.v1.{MainOps, ValidatedArgs} | ||
import scalafix.lint.RuleDiagnostic | ||
import scalafix.internal.util.OptionOps._ | ||
import scalafix.v0 | ||
import scalafix.v0.RuleCtx | ||
|
||
import scala.meta.io.AbsolutePath | ||
|
||
final case class ScalafixFileEvaluationImpl( | ||
originalPath: AbsolutePath, | ||
fixedOpt: Option[String], | ||
error: ExitStatus, | ||
errorMessage: Option[String], | ||
diagnostics: Seq[RuleDiagnostic], | ||
patches: Seq[ScalafixPatchImpl] | ||
)( | ||
args: ValidatedArgs, | ||
ctxOpt: Option[RuleCtx], | ||
index: Option[v0.SemanticdbIndex] | ||
) extends ScalafixFileEvaluation { | ||
val rules: Seq[ScalafixRuleImpl] = | ||
args.rules.rules.map(new ScalafixRuleImpl(_)) | ||
val pathToReplace = args.pathReplace(originalPath) | ||
|
||
override def getEvaluatedFile: Path = originalPath.toNIO | ||
|
||
override def getEvaluatedRules: Array[ScalafixRule] = rules.toArray | ||
|
||
override def previewPatches(): Optional[String] = fixedOpt.asJava | ||
|
||
override def previewPatchesAsUnifiedDiff: 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 getErrors(): Array[ScalafixError] = | ||
ScalafixErrorImpl.fromScala(error) | ||
|
||
override def isSuccessful: Boolean = error.isOk | ||
|
||
override def getDiagnostics: Array[ScalafixDiagnostic] = | ||
diagnostics.map(ScalafixDiagnosticImpl.fromScala).toArray | ||
|
||
override def getPatches: Array[ScalafixPatch] = { | ||
patches.toArray | ||
} | ||
|
||
override def applyPatches(): Array[ScalafixError] = { | ||
val exitStatus = fixedOpt.toTry | ||
.flatMap(MainOps.applyDiff(args, file = originalPath, _)) | ||
.getOrElse(ExitStatus.UnexpectedError) | ||
ScalafixErrorImpl.fromScala(exitStatus).toSeq.toArray | ||
} | ||
|
||
override def previewPatches( | ||
selectedPatches: Array[ScalafixPatch] | ||
): Optional[String] = { | ||
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.previewPatches(filteredPatches.map(_.patch), ctx, index) | ||
) | ||
.asJava | ||
} | ||
override def applyPatches( | ||
selectedPatches: Array[ScalafixPatch] | ||
): Array[ScalafixError] = { | ||
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 => | ||
MainOps.applyPatches( | ||
args, | ||
filteredPatches.map(_.patch), | ||
ctx, | ||
index, | ||
pathToReplace | ||
) | ||
) | ||
.getOrElse(ExitStatus.UnexpectedError) | ||
ScalafixErrorImpl.fromScala(exitStatus) | ||
} | ||
|
||
} | ||
|
||
object ScalafixFileEvaluationImpl { | ||
|
||
def from( | ||
originalPath: AbsolutePath, | ||
fixed: Option[String], | ||
exitStatus: ExitStatus, | ||
patches: Seq[v0.Patch], | ||
diagnostics: Seq[RuleDiagnostic] | ||
)( | ||
args: ValidatedArgs, | ||
ctx: RuleCtx, | ||
index: Option[v0.SemanticdbIndex] | ||
): ScalafixFileEvaluationImpl = { | ||
val scalafixPatches = patches.map(ScalafixPatchImpl) | ||
ScalafixFileEvaluationImpl( | ||
originalPath = originalPath, | ||
fixedOpt = fixed, | ||
error = exitStatus, | ||
errorMessage = None, | ||
diagnostics = diagnostics, | ||
patches = scalafixPatches | ||
)(args, Some(ctx), index) | ||
} | ||
def from( | ||
originalPath: AbsolutePath, | ||
exitStatus: ExitStatus, | ||
errorMessage: String | ||
)( | ||
args: ValidatedArgs | ||
): ScalafixFileEvaluationImpl = | ||
ScalafixFileEvaluationImpl( | ||
originalPath, | ||
None, | ||
exitStatus, | ||
Some(errorMessage), | ||
Nil, | ||
Nil | ||
)(args, None, None) | ||
} |
6 changes: 6 additions & 0 deletions
6
scalafix-cli/src/main/scala/scalafix/internal/interfaces/ScalafixPatchImpl.scala
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,6 @@ | ||
package scalafix.internal.interfaces | ||
|
||
import scalafix.interfaces.ScalafixPatch | ||
import scalafix.Patch | ||
|
||
case class ScalafixPatchImpl(patch: Patch) extends ScalafixPatch |
Oops, something went wrong.