Skip to content

Inconsistent formatting of for/do #3013

@charpov

Description

@charpov

This Scala 3 code is formatted as follows:

class Format:
   def f =
      val received = mutable.Buffer.empty[String]
      for (m <- 0 until 10) do
         val msg = m.toString
         received.addOne(msg)

   def g =
      val received = mutable.Buffer.empty[String]
      for (m <- 0 until 10)
         do
            val msg = m.toString
            received += msg

There is a newline inserted before the second do but not before the first, and I don't understand why the behavior is not the same for both. (If I manually insert a newline before the first do, Scalafmt keeps it, but what I really want is to avoid the newline before the second do.) Unless I'm unaware of some arcane configuration, this seems inconsistent.

I'm running Scalafmt through IntelliJ, and my configuration is below:

version = 3.3.0
runner.dialect = scala3

fileOverride {
  "glob:**/*.sbt" {
    runner.dialect = sbt1
  }
}

preset = defaultWithAlign
maxColumn = 100
indent.main = 3

indentYieldKeyword = false
trailingCommas = preserve
includeNoParensInSelectChains = false
optIn.breaksInsideChains = false

binPack.literalsIncludeSimpleExpr = true

newlines {
  alwaysBeforeElseAfterCurlyIf = true
  beforeMultiline = keep
  beforeCurlyLambdaParams = multilineWithCaseOnly
  usingParamListModifierPrefer = before
  avoidForSimpleOverflow = [tooLong,punct,slc]
  avoidInResultType = true
}

spaces {
  beforeContextBoundColon = Always
  inImportCurlyBraces = true
  afterSymbolicDefs = true
}

literals {
  long=Upper
  float=Lower
  double=Lower
  hexPrefix=Lower
  scientific=Upper
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions