diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index c6e01b5f5..943170cd0 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -1515,18 +1515,24 @@ class FormatOps( def getSpaceSplit(penalty: Int, policy: Policy = Policy.NoPolicy)(implicit fileLine: FileLine, ) = { - val spacePolicy = policy | penalize(penalty) val miniSlbEnd = getSlbEndOnLeft(next(ft)) - val slbLite = style.newlines.keep && - (body.parent match { - case Some(p: Term.Assign) => !p.parent.is[Term.ArgClause] || - style.binPack.callSite == BinPack.Site.Never - case _ => true - }) - val opt = if (style.newlines.keep) miniSlbEnd else blast - Split(Space, 0).withSingleLineNoOptimal(miniSlbEnd) - .andPolicy(spacePolicy) - .withOptimalToken(opt, killOnFail = slbLite, recurseOnly = slbLite) + val slbPolicy = SingleLineBlock(miniSlbEnd) + val slbFails = slbPolicy.exists { // wouldn't check, past expiration + case p: SingleLineBlock => p.failsLeftSyntaxNL(miniSlbEnd) + case _ => false + } + if (slbFails) Split.ignored + else { + val slbLite = style.newlines.keep && + (body.parent match { + case Some(p: Term.Assign) => !p.parent.is[Term.ArgClause] || + style.binPack.callSite == BinPack.Site.Never + case _ => true + }) + val opt = if (style.newlines.keep) miniSlbEnd else blast + Split(Space, 0, policy = slbPolicy & (policy | penalize(penalty))) + .withOptimalToken(opt, killOnFail = slbLite, recurseOnly = slbLite) + } } def getPolicySplits(penalty: Int, policy: Policy, nlCost: Int = 1)( implicit fileLine: FileLine, diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/PolicyOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/PolicyOps.scala index c40c515ca..644645077 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/PolicyOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/PolicyOps.scala @@ -75,10 +75,11 @@ object PolicyOps { override def terminal: Boolean = true override val prefix: String = "SLB" private val checkSyntax = noSyntaxNL || !style.newlines.ignoreInSyntax + def failsLeftSyntaxNL(ft: FT): Boolean = checkSyntax && ft.leftHasNewline override val f: Policy.Pf = { case Decision(ft, s) if !(ft.right.is[T.EOF] || okSLC && isLeftCommentThenBreak(ft)) => - if (checkSyntax && ft.leftHasNewline) Seq.empty else s.filterNot(_.isNL) + if (failsLeftSyntaxNL(ft)) Seq.empty else s.filterNot(_.isNL) } } diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat index fecd357d0..affd7f18c 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat @@ -11070,16 +11070,18 @@ object a { } >>> object a { - val foo1s = """bar + val foo1s = + """bar baz qux""" val foo1i = s"""bar baz qux""" - val foo2s = """|bar - |baz - |qux""".stripMargin + val foo2s = + """|bar + |baz + |qux""".stripMargin val foo2i = s"""|bar |baz @@ -11188,16 +11190,18 @@ object a { } >>> object a { - val foo1s = """bar + val foo1s = + """bar baz qux""" val foo1i = s"""bar baz qux""" - val foo2s = """|bar - |baz - |qux""".stripMargin + val foo2s = + """|bar + |baz + |qux""".stripMargin val foo2i = s"""|bar |baz