Skip to content
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

Scalafmt: expand Error.WithCode in external format #4609

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion scalafmt-core/shared/src/main/scala/org/scalafmt/Error.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ object Error {

case object MegaTestFailed extends Error("Mega test failed.")

case class WithCode(error: Throwable, code: String) extends Exception(error)
case class WithCode(error: Throwable, code: String)
extends Exception(error.getMessage, error)

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ object Scalafmt {
style: ScalafmtConfig,
range: Set[Range],
filename: String,
): Formatted = formatCode(code, style, range, filename).formatted
): Formatted = formatCode(code, style, range, filename).formatted match {
case Formatted.Failure(Error.WithCode(ex, _)) => Formatted.Failure(ex)
case x => x
}

private[scalafmt] def formatCode(
code: String,
Expand Down Expand Up @@ -137,7 +140,7 @@ object Scalafmt {
code: String,
style: ScalafmtConfig = ScalafmtConfig.default,
range: Set[Range] = Set.empty[Range],
): Formatted = formatCode(code, style, range).formatted
): Formatted = format(code, style, range, defaultFilename)

// used by ScalafmtReflect.parseConfig
def parseHoconConfigFile(configPath: Path): Configured[ScalafmtConfig] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class DynamicSuite extends FunSuite {
)
}
}
checkParseError(nightly, "scala212")
checkParseError(latest, "scala212")
checkParseError("1.0.0", "Scala211")

Expand Down Expand Up @@ -386,8 +387,10 @@ class DynamicSuite extends FunSuite {
}
}
}
checkSbt(nightly, "scala213")
checkSbt(latest, "scala213")
checkSbt("3.8.0", "scala213")
checkSbt(nightly, "scala211")
checkSbt(latest, "scala211")
checkSbt("3.8.0", "scala211")
checkSbt("1.2.0", "Scala211")
Expand Down Expand Up @@ -607,7 +610,18 @@ class DynamicSuite extends FunSuite {
private object DynamicSuite {

def nightly = BuildInfo.nightly
def latest = BuildInfo.previousStable
def latest = {
val latest = BuildInfo.previousStable
ScalafmtVersion.parse(latest).flatMap { ver =>
if (ver.rc <= 0 && ver.snapshot.isEmpty) None
else if (ver.patch > 0)
Some(ScalafmtVersion(ver.major, ver.minor, ver.patch - 1).toString)
else if (ver.minor > 0)
Some(ScalafmtVersion(ver.major, ver.minor - 1, 0).toString)
else if (ver.major > 0) Some(ScalafmtVersion(ver.major - 1, 0, 0).toString)
else None
}.getOrElse(latest)
}

def getDialectError(version: String, dialect: String, sbt: Boolean = false) =
if (version < "3.1.0") "" else s" [dialect ${if (sbt) "sbt" else dialect}]"
Expand Down