Skip to content

Commit

Permalink
ScalafmtReflect: replace format() with tryFormat()
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Dec 2, 2021
1 parent 91a577a commit 493cf44
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.scalafmt.dynamic

import java.nio.file.Path

import scala.util.Try
import scala.util.Success

import org.scalafmt.dynamic.ScalafmtDynamicError._
import org.scalafmt.dynamic.exceptions._
Expand Down Expand Up @@ -43,11 +43,9 @@ final case class ScalafmtDynamicSession(
cfg.getVersion < ScalafmtVersion(2, 6, 3) &&
extension == "sbt" || extension == "sc" // added in 2.6.3
}
Try {
val configWithDialect: ScalafmtReflectConfig =
if (needSbt) cfg.withSbtDialect else cfg
configWithDialect.format(code, Some(file))
}.toEither.left.map { x =>
val cfgWithDialect = if (needSbt) cfg.withSbtDialect else Success(cfg)
val formatResult = cfgWithDialect.flatMap(_.tryFormat(code, Some(file)))
formatResult.toEither.left.map { x =>
val cause = ReflectionException.flatten(x)
properties.reporter.error(file, cause)
UnknownError(cause)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,38 +96,39 @@ case class ScalafmtReflect(
}
}

def format(
def tryFormat(
code: String,
config: ScalafmtReflectConfig,
fileOpt: Option[Path] = None
): String = {
): Try[String] = {
require(this eq config.fmtReflect)

val formatted = (formatMethodWithFilename, fileOpt) match {
case (Some(method), Some(file)) =>
val filename = file.toString
method.invoke(null, code, config.target, emptyRange, filename)
case _ =>
formatMethod.invoke(null, code, config.target, emptyRange)
}
clearTokenizerCache()
try {
Try {
val formatted = (formatMethodWithFilename, fileOpt) match {
case (Some(method), Some(file)) =>
val filename = file.toString
method.invoke(null, code, config.target, emptyRange, filename)
case _ =>
formatMethod.invoke(null, code, config.target, emptyRange)
}
clearTokenizerCache()
formattedGet.invoke(formatted).asInstanceOf[String]
} catch {
}.recoverWith {
case ReflectionException(e)
if tokenizeExceptionCls.isInstance(e) ||
parseExceptionCls.isInstance(e) =>
val pos = e.invoke("pos")
val range = positionRange(pos)
val shortMessage = e.invokeAs[String]("shortMessage")
throw PositionExceptionImpl(
val exception = PositionExceptionImpl(
fileOpt.orNull,
code,
shortMessage,
e.getMessage,
range,
e
)
Failure(exception)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ class ScalafmtReflectConfig private[dynamic] (val fmtReflect: ScalafmtReflect)(
}
}

lazy val withSbtDialect: ScalafmtReflectConfig =
new ScalafmtReflectConfig(fmtReflect)(
try target.invoke("forSbt")
catch {
case ReflectionException(_: NoSuchMethodException) =>
target.invoke("withDialect", (dialectCls, sbtDialect))
lazy val withSbtDialect: Try[ScalafmtReflectConfig] =
Try(target.invoke("forSbt"))
.recover { case ReflectionException(_: NoSuchMethodException) =>
target.invoke("withDialect", (dialectCls, sbtDialect))
}
)
.map(new ScalafmtReflectConfig(fmtReflect)(_))

def withoutRewriteRules: ScalafmtReflectConfig = {
if (getVersion < ScalafmtVersion(3, 2, 0)) withoutRewriteRulesPre320
Expand Down Expand Up @@ -93,8 +91,8 @@ class ScalafmtReflectConfig private[dynamic] (val fmtReflect: ScalafmtReflect)(
!rules.invokeAs[Boolean]("isEmpty")
}.getOrElse(false)

def format(code: String, file: Option[Path]): String =
fmtReflect.format(code, this, file)
def tryFormat(code: String, file: Option[Path]): Try[String] =
fmtReflect.tryFormat(code, this, file)

def indentMain: Option[Int] =
if (getVersion < ScalafmtVersion(3, 0, 0)) Some(2)
Expand Down

0 comments on commit 493cf44

Please sign in to comment.