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

ScalafmtRunner: always allow top-level terms #3357

Merged
merged 1 commit into from
Oct 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ case class ScalafmtConfig(
)

def forSbt: ScalafmtConfig =
copy(runner = runner.forSbt, rewrite = rewrite.forSbt)
copy(rewrite = rewrite.forSbt)

private lazy val expandedFileOverride = Try {
val langPrefix = "lang:"
Expand Down Expand Up @@ -237,7 +237,7 @@ case class ScalafmtConfig(
private[scalafmt] lazy val docstringsWrapMaxColumn: Int =
docstrings.wrapMaxColumn.getOrElse(maxColumn)

@inline private[scalafmt] def dialect = runner.getDialect
@inline private[scalafmt] def dialect = runner.getDialectForParser

private[scalafmt] def getTrailingCommas = rewrite.trailingCommas.style

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ case class ScalafmtRunner(
fatalWarnings: Boolean = false
) {
@inline private[scalafmt] def getDialect = dialect.dialect
private[scalafmt] lazy val getDialectForParser: Dialect =
getDialect.withAllowToplevelTerms(true).withAllowToplevelStatements(true)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tgodzik moved it here.

@inline private[scalafmt] def dialectName = {
val name = dialect.name
if (dialectOverride.values.isEmpty) name else s"$name [with overrides]"
}
@inline private[scalafmt] def getParser = parser.parse

@inline private def topLevelDialect = dialect.copy(
dialect = getDialect
.withAllowToplevelTerms(true)
.withToplevelSeparator("")
)
@inline
def withDialect(dialect: sourcecode.Text[Dialect]): ScalafmtRunner =
withDialect(NamedDialect(dialect))

@inline
private[scalafmt] def withDialect(dialect: NamedDialect): ScalafmtRunner =
Expand All @@ -48,11 +48,8 @@ case class ScalafmtRunner(
private[scalafmt] def withParser(parser: ScalafmtParser): ScalafmtRunner =
copy(parser = parser)

def forSbt: ScalafmtRunner = withDialect(topLevelDialect)

private[scalafmt] def forCodeBlock: ScalafmtRunner = copy(
debug = false,
dialect = topLevelDialect,
eventCallback = null,
parser = ScalafmtParser.Source
)
Expand All @@ -64,7 +61,7 @@ case class ScalafmtRunner(
if (null != eventCallback) evts.foreach(eventCallback)

def parse(input: meta.inputs.Input): Parsed[_ <: Tree] =
getParser(input, getDialect)
getParser(input, getDialectForParser)

@inline def isDefaultDialect = dialect.name == NamedDialect.defaultName

Expand All @@ -85,7 +82,7 @@ object ScalafmtRunner {
maxStateVisits = 1000000
)

val sbt = default.forSbt
val sbt = default.withDialect(meta.dialects.Sbt)

implicit val encoder: ConfEncoder[ScalafmtRunner] =
generic.deriveEncoder
Expand Down
14 changes: 7 additions & 7 deletions scalafmt-tests/src/test/scala/org/scalafmt/util/ErrorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import org.scalafmt.Scalafmt
import munit.FunSuite

class ErrorTest extends FunSuite {
test("errors are caught") {
val nonSourceFile = Seq(
"class A {",
"val x = 1",
"println(1)"
)
nonSourceFile.foreach { original =>
private val nonSourceFile = Seq(
"class A {",
"val x + 1",
"println 1"
)
nonSourceFile.foreach { original =>
test(s"errors are caught: $original") {
Scalafmt.format(original, HasTests.unitTest40) match {
case _: Formatted.Success => fail("expected failure, got success")
case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait FormatAssertions {
): Unit =
assertFormatPreservesAst(filename, original, obtained)(
runner.getParser,
runner.getDialect
runner.getDialectForParser
)

def assertFormatPreservesAst[T <: Tree](
Expand Down