Skip to content

Commit

Permalink
scalafmt: don't ignore newlines in syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Mar 31, 2024
1 parent 4a47f04 commit d0be162
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 82 deletions.
1 change: 1 addition & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ align {
}
newlines {
avoidForSimpleOverflow = [punct, slc, tooLong]
ignoreInSyntax = false
}
rewrite {
rules = [
Expand Down
22 changes: 13 additions & 9 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ object Cli {
private def findRunner(
options: CliOptions
): MaybeRunner = options.hoconOpt.fold[MaybeRunner] {
Left(s"""error: missing Scalafmt configuration file.
|Consider creating '${options.getProposedConfigFile}'
|with the following (other parameters may also be required):
|${getProposedConfigVersion(options)}
|""".stripMargin)
Left(
s"""error: missing Scalafmt configuration file.
|Consider creating '${options.getProposedConfigFile}'
|with the following (other parameters may also be required):
|${getProposedConfigVersion(options)}
|""".stripMargin
)
} {
// Run format using
// - `scalafmt-dynamic` if the specified `version` setting doesn't match build version.
Expand All @@ -121,10 +123,12 @@ object Cli {
.toString
case _ => "--config-str option"
}
Left(s"""error: missing Scalafmt version.
|Consider adding the following to $where:
|${getProposedConfigVersion(options)}
|""".stripMargin)
Left(
s"""error: missing Scalafmt version.
|Consider adding the following to $where:
|${getProposedConfigVersion(options)}
|""".stripMargin
)
} {
case Left(error) =>
Left(s"error: invalid configuration: ${error}")
Expand Down
10 changes: 6 additions & 4 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/CliArgParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ object CliArgParser {
}
.text("(experimental) only format line range from=to")

note(s"""|Examples:
|$usageExamples
|Please file bugs to https://github.com/scalameta/scalafmt/issues
""".stripMargin)
note(
s"""|Examples:
|$usageExamples
|Please file bugs to https://github.com/scalameta/scalafmt/issues
""".stripMargin
)

checkConfig { c =>
if (c.config.isDefined && c.configStr.isDefined)
Expand Down
24 changes: 14 additions & 10 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/Error.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,25 @@ object Error {
extends Error(s"Missing => in case: \n$tree")

case class FormatterChangedAST(diff: String, output: String)
extends Error(s"""Formatter changed AST
|=====================
|$diff
|=====================
|${output.linesIterator.toVector.take(10).mkString("\n")}
|=====================
|Formatter changed AST
""".stripMargin)
extends Error(
s"""Formatter changed AST
|=====================
|$diff
|=====================
|${output.linesIterator.toVector.take(10).mkString("\n")}
|=====================
|Formatter changed AST
""".stripMargin
)

case class FormatterOutputDoesNotParse(msg: String, line: Int)
extends Error("Formatter output does not parse:\n" + msg)

case class UnexpectedTree[Expected <: Tree: ClassTag](obtained: Tree)
extends Error(s"""Expected: ${classTag[Expected].runtimeClass.getName}
|Obtained: ${log(obtained)}""".stripMargin)
extends Error(
s"""Expected: ${classTag[Expected].runtimeClass.getName}
|Obtained: ${log(obtained)}""".stripMargin
)

case class CantFormatFile(msg: String)
extends Error("scalafmt cannot format this file:\n" + msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,10 @@ private class BestFirstSearch private (
|nextSplits=$nextSplits
|splitsAfterPolicy=$splitsAfterPolicy""".stripMargin
if (runner.debug) {
logger.debug(s"""Failed to format
|$msg""".stripMargin)
logger.debug(
s"""Failed to format
|$msg""".stripMargin
)
}
complete(deepestYet)
SearchResult(deepestYet, reachedEOF = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ object Patch {
case (add: Add, _: Remove) => add.copy(keepTok = false)
case (rem: Remove, _: Remove) => rem
case _ =>
sys.error(s"""Can't merge token patches:
|1. $a
|2. $b""".stripMargin)
sys.error(
s"""Can't merge token patches:
|1. $a
|2. $b""".stripMargin
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ object LoggerOps {
val tokens =
s"TOKENS: ${t.tokens.map(x => reveal(x.syntax)).mkString(",")}"
if (tokensOnly) tokens
else s"""TYPE: ${t.getClass.getName.stripPrefix("scala.meta.")}
|SOURCE: $t
|STRUCTURE: ${t.show[Structure]}
|$tokens
|""".stripMargin
else
s"""TYPE: ${t.getClass.getName.stripPrefix("scala.meta.")}
|SOURCE: $t
|STRUCTURE: ${t.show[Structure]}
|$tokens
|""".stripMargin
}

def log(t: Option[Tree]): String = log(t, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ class DynamicSuite extends FunSuite {
}
def setVersion(newVersion: String, dialect: String, rest: String*): Unit = {
val dialectLine = Option(dialect).fold("")(x => s"runner.dialect = $x")
setConfig(s"""
|version="$newVersion"
|$dialectLine
|${rest.mkString("\n")}
|""".stripMargin)
setConfig(
s"""
|version="$newVersion"
|$dialectLine
|${rest.mkString("\n")}
|""".stripMargin
)
}
def relevant: String = {
out.toString.replace(config.toString, "path/.scalafmt.conf")
Expand Down Expand Up @@ -293,12 +295,14 @@ class DynamicSuite extends FunSuite {
}

check("regex-error") { f =>
f.setConfig(s"""|version = "$nightly"
|runner.dialect = "scala212"
|project.excludeFilters = [
| ".*foo("
|]
|""".stripMargin)
f.setConfig(
s"""|version = "$nightly"
|runner.dialect = "scala212"
|project.excludeFilters = [
| ".*foo("
|]
|""".stripMargin
)
val err = f.assertThrows[ScalafmtDynamicError.ConfigParseError]().getMessage
assertNoDiff(
err.takeRight(120),
Expand All @@ -310,12 +314,14 @@ class DynamicSuite extends FunSuite {
}

check("path-error") { f =>
f.setConfig(s"""|version = "$nightly"
|runner.dialect = "scala212"
|project.excludePaths = [
| "foo.scala"
|]
|""".stripMargin)
f.setConfig(
s"""|version = "$nightly"
|runner.dialect = "scala212"
|project.excludePaths = [
| "foo.scala"
|]
|""".stripMargin
)
val err = f.assertThrows[ScalafmtDynamicError.ConfigParseError]().getMessage
assertNoDiff(
err.takeRight(120),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ class CliOptionsTest extends FunSuite {
)

assertEquals(
ScalafmtConfig.fromHoconString("""|preset = defaultWithAlign
|maxColumn = 100
|""".stripMargin),
ScalafmtConfig.fromHoconString(
"""|preset = defaultWithAlign
|maxColumn = 100
|""".stripMargin
),
Configured.ok(ScalafmtConfig.defaultWithAlign.copy(maxColumn = 100))
)
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,47 +48,57 @@ class ScalafmtConfigTest extends FunSuite {

test("align preset no override") {
val config = ScalafmtConfig
.fromHoconString("""
|align = none
|align.stripMargin = true
""".stripMargin)
.fromHoconString(
"""
|align = none
|align.stripMargin = true
""".stripMargin
)
.get
// none was ignored
assertEquals(config.align, Align(stripMargin = true))
}

test("align preset with override") {
val config = ScalafmtConfig
.fromHoconString("""
|align.preset = none
|align.stripMargin = true
""".stripMargin)
.fromHoconString(
"""
|align.preset = none
|align.stripMargin = true
""".stripMargin
)
.get
assertEquals(config.align, Align.none.copy(stripMargin = true))
}

test("dialect override") {
val config1 = ScalafmtConfig
.fromHoconString("""
|runner.dialect = scala213
|""".stripMargin)
.fromHoconString(
"""
|runner.dialect = scala213
|""".stripMargin
)
.get
assert(!config1.runner.getDialect.allowToplevelTerms)
val config2 = ScalafmtConfig
.fromHoconString("""
|runner.dialectOverride.allowToplevelTerms = true
|runner.dialect = scala213
|""".stripMargin)
.fromHoconString(
"""
|runner.dialectOverride.allowToplevelTerms = true
|runner.dialect = scala213
|""".stripMargin
)
.get
assert(config2.runner.getDialect.allowToplevelTerms)
}

test("hasRewriteRules-and-withoutRewriteRules trailingCommas") {
val config1 = ScalafmtConfig
.fromHoconString("""
|runner.dialect = scala213
|rewrite.trailingCommas = never
|""".stripMargin)
.fromHoconString(
"""
|runner.dialect = scala213
|rewrite.trailingCommas = never
|""".stripMargin
)
.get
assert(config1.hasRewrites)
val config2 = config1.withoutRewrites
Expand All @@ -97,11 +107,13 @@ class ScalafmtConfigTest extends FunSuite {

test("hasRewriteRules-and-withoutRewriteRules docstrings") {
val config1 = ScalafmtConfig
.fromHoconString("""
|runner.dialect = scala213
|rewrite.trailingCommas = keep
|docstrings.removeEmpty = true
|""".stripMargin)
.fromHoconString(
"""
|runner.dialect = scala213
|rewrite.trailingCommas = keep
|docstrings.removeEmpty = true
|""".stripMargin
)
.get
assert(config1.hasRewrites)
val config2 = config1.withoutRewrites
Expand Down
12 changes: 7 additions & 5 deletions scalafmt-tests/src/test/scala/org/scalafmt/util/Report.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ object Report {

def explanation =
div(
p("""Formatting output from scalafmt's test suite.
|The formatter uses Dijkstra's shortest path to determine the
|formatting with the "cheapest" cost. The red regions are
|tokens the formatter visits often.
""".stripMargin),
p(
"""Formatting output from scalafmt's test suite.
|The formatter uses Dijkstra's shortest path to determine the
|formatting with the "cheapest" cost. The red regions are
|tokens the formatter visits often.
""".stripMargin
),
ul(
li("Declaration arguments: bin packed"),
li("Callsite arguments: one arg per line if overflowing")
Expand Down

0 comments on commit d0be162

Please sign in to comment.