Skip to content

Commit

Permalink
ScalafmtReflectConfig: add access to indents
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 23, 2021
1 parent e3a921f commit 17f69b0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ final case class ScalafmtDynamic(
} yield codeFormatted
}

private def resolveConfig(
private[dynamic] def resolveConfig(
configPath: Path
): Either[ScalafmtDynamicError, ScalafmtReflectConfig] = {
if (!Files.exists(configPath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class ScalafmtReflectConfig private[dynamic] (val fmtReflect: ScalafmtReflect)(
private val projectField = target.invoke("project")
private val projectMatcherField = projectField.invoke("matcher")

private lazy val indentField = Try {
if (getVersion < ScalafmtVersion(3, 0, 0))
target.invoke("continuationIndent")
else
target.invoke("indent")
}

@inline def getVersion = fmtReflect.version

@inline def isIncludedInProject(path: Path): Boolean =
Expand Down Expand Up @@ -83,6 +90,16 @@ class ScalafmtReflectConfig private[dynamic] (val fmtReflect: ScalafmtReflect)(
def format(code: String, file: Option[Path]): String =
fmtReflect.format(code, this, file)

def indentMain: Option[Int] =
if (getVersion < ScalafmtVersion(3, 0, 0)) Some(2)
else indentField.map(_.invokeAs[Int]("main")).toOption

def indentCallSite: Option[Int] =
indentField.map(_.invokeAs[Int]("callSite")).toOption

def indentDefnSite: Option[Int] =
indentField.map(_.invokeAs[Int]("defnSite")).toOption

override def equals(obj: Any): Boolean = target.equals(obj)

override def hashCode(): Int = target.hashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,47 @@ class DynamicSuite extends FunSuite {
assertEquals(error, "Missing version")
}

private def checkDynamicConfig(
name: String,
version: String,
dialect: String,
rest: String*
)(f: ScalafmtReflectConfig => Unit): Unit = {
check(s"$name [v=$version d=$dialect]") { fmt =>
fmt.setVersion(version, dialect, rest: _*)
fmt.dynamic.resolveConfig(fmt.config) match {
case Left(e) => fail("failed to load config", e)
case Right(cfg) => f(cfg)
}
}
}

checkDynamicConfig(
s"check indent.main",
"3.0.0",
"scala211",
s"indent.main = 3"
) { cfg =>
assertEquals(cfg.indentMain, Some(3))
assertEquals(cfg.indentCallSite, Some(2))
assertEquals(cfg.indentDefnSite, Some(4))
}

Seq(("3.0.0", "indent"), ("2.5.3", "continuationIndent"))
.foreach { case (version, section) =>
checkDynamicConfig(
s"check $section.{call,defn}Site",
version,
"scala211",
s"$section.callSite = 3",
s"$section.defnSite = 5"
) { cfg =>
assertEquals(cfg.indentMain, Some(2))
assertEquals(cfg.indentCallSite, Some(3))
assertEquals(cfg.indentDefnSite, Some(5))
}
}

}

private object DynamicSuite {
Expand Down

0 comments on commit 17f69b0

Please sign in to comment.