Skip to content

Commit

Permalink
Router: check noSyntaxNL at the end of RHS
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 17, 2025
1 parent c3da206 commit 47d1f18
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
20 changes: 12 additions & 8 deletions scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 47d1f18

Please sign in to comment.