Skip to content

Commit

Permalink
Scalafmt: remove the format overload as redundant
Browse files Browse the repository at this point in the history
Also, fix usages to pass the filename properly.
  • Loading branch information
kitbellew committed Dec 24, 2019
1 parent b67cf0e commit 117477b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Scalafmt210 {
if (filename.endsWith(".sbt")) SRunner.sbt
else SRunner.default
val style = scalafmtStyle.copy(runner = runner)
Scalafmt.format(code, style) match {
Scalafmt.format(code, style, filename = filename) match {
case Formatted.Success(formattedCode) => formattedCode
case error =>
error match {
Expand Down
14 changes: 3 additions & 11 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/Scalafmt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ object Scalafmt {
*/
def format(
code: String,
style: ScalafmtConfig,
range: Set[Range],
filename: String
style: ScalafmtConfig = ScalafmtConfig.default,
range: Set[Range] = Set.empty,
filename: String = "<input>"
): Formatted = {
try {
val runner = style.runner
Expand Down Expand Up @@ -82,14 +82,6 @@ object Scalafmt {
}
}

def format(
code: String,
style: ScalafmtConfig = ScalafmtConfig.default,
range: Set[Range] = Set.empty[Range]
): Formatted = {
format(code, style, range, "<input>")
}

def parseHoconConfig(configString: String): Configured[ScalafmtConfig] =
Config.fromHoconString(configString, None)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class FidelityTest extends AnyFunSuite with FormatAssertions {
examples.foreach { example =>
test(example.filename) {
val formatted =
Scalafmt.format(example.code, ScalafmtConfig.default).get
Scalafmt.format(example.code, filename = example.filename).get
assertFormatPreservesAst(example.code, formatted)(
scala.meta.parsers.Parse.parseSource,
Scala211
Expand Down
38 changes: 23 additions & 15 deletions scalafmt-tests/src/test/scala/org/scalafmt/FormatTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,22 @@ class FormatTests

def run(t: DiffTest, parse: Parse[_ <: Tree]): Unit = {
val runner = scalafmtRunner(t.style.runner).copy(parser = parse)
val obtained =
Scalafmt.format(t.original, t.style.copy(runner = runner)) match {
case Formatted.Failure(e)
if t.style.onTestFailure.nonEmpty && e.getMessage.contains(
e.getMessage
) =>
t.expected
case Formatted.Failure(e: Incomplete) => e.formattedCode
case Formatted.Failure(e: SearchStateExploded) =>
logger.elem(e)
e.partialOutput
case x => x.get
}
val obtained = Scalafmt.format(
t.original,
t.style.copy(runner = runner),
filename = t.filename
) match {
case Formatted.Failure(e)
if t.style.onTestFailure.nonEmpty && e.getMessage.contains(
e.getMessage
) =>
t.expected
case Formatted.Failure(e: Incomplete) => e.formattedCode
case Formatted.Failure(e: SearchStateExploded) =>
logger.elem(e)
e.partialOutput
case x => x.get
}
debugResults += saveResult(t, obtained, onlyOne)
if (t.style.rewrite.rules.isEmpty &&
!t.style.assumeStandardLibraryStripMargin &&
Expand All @@ -74,8 +77,13 @@ class FormatTests
t.style.runner.dialect
)
}
val formattedAgain =
Scalafmt.format(obtained, t.style.copy(runner = runner)).get
val formattedAgain = Scalafmt
.format(
obtained,
t.style.copy(runner = runner),
filename = t.filename
)
.get
// getFormatOutput(t.style, true) // uncomment to debug
assertNoDiff(formattedAgain, obtained, "Idempotency violated")
if (!onlyManual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,13 @@ trait HasTests extends AnyFunSuiteLike with FormatAssertions {

def defaultRun(t: DiffTest, parse: Parse[_ <: Tree]): Unit = {
val runner = scalafmtRunner(t.style.runner).copy(parser = parse)
val obtained =
Scalafmt.format(t.original, t.style.copy(runner = runner)).get
val obtained = Scalafmt
.format(
t.original,
t.style.copy(runner = runner),
filename = t.filename
)
.get
if (t.style.rewrite.rules.isEmpty) {
assertFormatPreservesAst(t.original, obtained)(
parse,
Expand Down

0 comments on commit 117477b

Please sign in to comment.