-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add scalafix-interfaces with Java APIs for reflective invocation #783
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
46 changes: 46 additions & 0 deletions
46
scalafix-cli/src/main/scala/scalafix/internal/interfaces/MainCallbackImpl.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,46 @@ | ||
package scalafix.internal.interfaces | ||
|
||
import scala.meta.inputs.Position | ||
import scalafix.interfaces.ScalafixDiagnostic | ||
import scalafix.interfaces.ScalafixMainCallback | ||
import scalafix.internal.config | ||
import scalafix.internal.config.ScalafixReporter | ||
import scalafix.internal.util.LintSyntax | ||
import scalafix.lint.LintDiagnostic | ||
import scalafix.lint.LintID | ||
import scalafix.lint.LintSeverity | ||
|
||
object MainCallbackImpl { | ||
|
||
def default: ScalafixMainCallback = fromScala(config.ScalafixReporter.default) | ||
|
||
def fromScala(underlying: config.ScalafixReporter): ScalafixMainCallback = | ||
new ScalafixMainCallback { | ||
override def reportDiagnostic(d: ScalafixDiagnostic): Unit = { | ||
val diagnostic = ScalafixDiagnosticImpl.fromJava(d) | ||
if (diagnostic.id == LintID.empty) { | ||
underlying.report( | ||
diagnostic.message, | ||
diagnostic.position, | ||
diagnostic.severity) | ||
} else { | ||
underlying.lint(diagnostic) | ||
} | ||
} | ||
} | ||
|
||
def fromJava(underlying: ScalafixMainCallback): ScalafixReporter = | ||
new ScalafixReporter { | ||
override def lint(d: LintDiagnostic): Unit = { | ||
val diagnostic = ScalafixDiagnosticImpl.fromScala(d) | ||
underlying.reportDiagnostic(diagnostic) | ||
} | ||
def report(msg: String, pos: Position, sev: LintSeverity): Unit = { | ||
val diagnostic = ScalafixDiagnosticImpl.fromScala( | ||
new LintSyntax.EagerLintDiagnostic(msg, pos, sev, "", LintID.empty) | ||
) | ||
underlying.reportDiagnostic(diagnostic) | ||
} | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
scalafix-cli/src/main/scala/scalafix/internal/interfaces/PositionImpl.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,26 @@ | ||
package scalafix.internal.interfaces | ||
|
||
import java.util.Optional | ||
import scala.meta.inputs.Position | ||
import scalafix.interfaces.ScalafixInput | ||
import scalafix.interfaces.ScalafixPosition | ||
import scalafix.internal.util.PositionSyntax._ | ||
|
||
object PositionImpl { | ||
def optionalFromScala(pos: Position): Optional[ScalafixPosition] = | ||
if (pos == Position.None) Optional.empty() | ||
else Optional.of(fromScala(pos)) | ||
def fromScala(pos: Position): ScalafixPosition = | ||
new ScalafixPosition { | ||
override def formatMessage(severity: String, message: String): String = | ||
pos.formatMessage(severity, message) | ||
override def startOffset(): Int = pos.start | ||
override def startLine(): Int = pos.startLine | ||
override def startColumn(): Int = pos.startColumn | ||
override def endOffset(): Int = pos.end | ||
override def endLine(): Int = pos.endLine | ||
override def endColumn(): Int = pos.endColumn | ||
override def input(): ScalafixInput = | ||
ScalafixInputImpl.fromScala(pos.input) | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
scalafix-cli/src/main/scala/scalafix/internal/interfaces/ScalafixDiagnosticImpl.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,66 @@ | ||
package scalafix.internal.interfaces | ||
import java.util.Optional | ||
import scala.meta.inputs.Input | ||
import scala.meta.inputs.Position | ||
import scalafix.interfaces.ScalafixDiagnostic | ||
import scalafix.interfaces.ScalafixLintID | ||
import scalafix.interfaces.ScalafixPosition | ||
import scalafix.interfaces.ScalafixSeverity | ||
import scalafix.lint.LintDiagnostic | ||
import scalafix.lint.LintID | ||
import scalafix.lint.LintSeverity | ||
|
||
object ScalafixDiagnosticImpl { | ||
def fromScala(diagnostic: LintDiagnostic): ScalafixDiagnostic = | ||
new ScalafixDiagnostic { | ||
override def severity(): ScalafixSeverity = diagnostic.severity match { | ||
case LintSeverity.Info => ScalafixSeverity.INFO | ||
case LintSeverity.Warning => ScalafixSeverity.WARNING | ||
case LintSeverity.Error => ScalafixSeverity.ERROR | ||
} | ||
override def message(): String = diagnostic.message | ||
override def explanation(): String = diagnostic.explanation | ||
override def position(): Optional[ScalafixPosition] = | ||
PositionImpl.optionalFromScala(diagnostic.position) | ||
override def lintID(): Optional[ScalafixLintID] = | ||
if (diagnostic.id == LintID.empty) { | ||
Optional.empty() | ||
} else { | ||
Optional.of(new ScalafixLintID { | ||
override def ruleName(): String = diagnostic.id.rule | ||
override def categoryID(): String = diagnostic.id.categoryID | ||
}) | ||
} | ||
} | ||
|
||
def fromJava(diagnostic: ScalafixDiagnostic): LintDiagnostic = | ||
new LintDiagnostic { | ||
override def message: String = diagnostic.message() | ||
override def position: Position = { | ||
if (diagnostic.position().isPresent) { | ||
val spos = diagnostic.position().get | ||
val input = Input.VirtualFile( | ||
spos.input().filename(), | ||
spos.input().text().toString | ||
) | ||
Position.Range(input, spos.startOffset(), spos.endOffset()) | ||
} else { | ||
Position.None | ||
} | ||
} | ||
override def severity: LintSeverity = diagnostic.severity() match { | ||
case ScalafixSeverity.INFO => LintSeverity.Info | ||
case ScalafixSeverity.WARNING => LintSeverity.Warning | ||
case ScalafixSeverity.ERROR => LintSeverity.Error | ||
} | ||
override def explanation: String = diagnostic.explanation() | ||
override def id: LintID = { | ||
if (diagnostic.lintID().isPresent) { | ||
val lintID = diagnostic.lintID().get | ||
LintID(lintID.ruleName(), lintID.categoryID()) | ||
} else { | ||
LintID.empty | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
scalafix-cli/src/main/scala/scalafix/internal/interfaces/ScalafixErrorImpl.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,30 @@ | ||
package scalafix.internal.interfaces | ||
import scalafix.cli.ExitStatus | ||
import scalafix.interfaces.ScalafixError | ||
|
||
object ScalafixErrorImpl { | ||
private lazy val statusToError: Map[ExitStatus, ScalafixError] = { | ||
val ok :: from = ExitStatus.all | ||
assert(ok.isOk) | ||
val to = ScalafixError.values().toList | ||
assert(from.length == to.length, s"$from != $to") | ||
val map = from.zip(to).toMap | ||
map.foreach { | ||
case (key, value) => | ||
assert( | ||
key.name.toLowerCase() == value.toString.toLowerCase, | ||
s"$key != $value" | ||
) | ||
} | ||
map | ||
} | ||
|
||
def fromScala(exit: ExitStatus): Array[ScalafixError] = { | ||
val buf = Array.newBuilder[ScalafixError] | ||
ExitStatus.all.foreach { code => | ||
if (exit.is(code)) | ||
buf += statusToError(code) | ||
} | ||
buf.result() | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
scalafix-cli/src/main/scala/scalafix/internal/interfaces/ScalafixImpl.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,37 @@ | ||
package scalafix.internal.interfaces | ||
|
||
import scalafix.Versions | ||
import scalafix.interfaces.Scalafix | ||
import scalafix.interfaces.ScalafixError | ||
import scalafix.interfaces.ScalafixMainArgs | ||
import scalafix.internal.v1.MainOps | ||
|
||
final class ScalafixImpl extends Scalafix { | ||
|
||
override def toString: String = | ||
s"""Scalafix v${scalafixVersion()}""" | ||
|
||
override def runMain(args: ScalafixMainArgs): Array[ScalafixError] = { | ||
val exit = | ||
MainOps.run(Array(), args.asInstanceOf[ScalafixMainArgsImpl].args) | ||
ScalafixErrorImpl.fromScala(exit) | ||
} | ||
|
||
override def newMainArgs(): ScalafixMainArgs = | ||
ScalafixMainArgsImpl() | ||
|
||
override def mainHelp(screenWidth: Int): String = { | ||
MainOps.helpMessage(screenWidth) | ||
} | ||
|
||
override def scalafixVersion(): String = | ||
Versions.version | ||
override def scalametaVersion(): String = | ||
Versions.scalameta | ||
override def supportedScalaVersions(): Array[String] = | ||
Versions.supportedScalaVersions.toArray | ||
override def scala211(): String = | ||
Versions.scala211 | ||
override def scala212(): String = | ||
Versions.scala212 | ||
} |
23 changes: 23 additions & 0 deletions
23
scalafix-cli/src/main/scala/scalafix/internal/interfaces/ScalafixInputImpl.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,23 @@ | ||
package scalafix.internal.interfaces | ||
|
||
import java.nio.CharBuffer | ||
import java.nio.file.Path | ||
import java.util.Optional | ||
import scala.meta.inputs.Input | ||
import scala.{meta => m} | ||
import scalafix.interfaces.ScalafixInput | ||
|
||
object ScalafixInputImpl { | ||
def fromScala(input: m.Input): ScalafixInput = | ||
new ScalafixInput { | ||
override def text(): CharSequence = input match { | ||
case Input.VirtualFile(_, value) => value | ||
case _ => CharBuffer.wrap(input.chars) | ||
} | ||
override def filename(): String = input.syntax | ||
override def path(): Optional[Path] = input match { | ||
case Input.File(path, _) => Optional.of(path.toNIO) | ||
case _ => Optional.empty() | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newline between 1 and 2 plz.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-opened scalameta/scalafmt#1069 This started happening more frequently now because intellij-scala uses Scalafmt after refactorings.