Skip to content

Commit

Permalink
Router: fix bp.callsite=oneline post-comma policy
Browse files Browse the repository at this point in the history
First, the subsequent break we should force is before a Dot which comes
from a Select, not a Comma.

Second, we should cancel forcing of that break if there's another break
which is output after the argument and before the dot or parenthesis.
  • Loading branch information
kitbellew committed May 19, 2024
1 parent ce4f2f9 commit e5ad60f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1440,13 +1440,21 @@ class Router(formatOps: FormatOps) {
case Some(FormatToken(_, t: T.Comma, _)) =>
splitOneArgPerLineAfterCommaOnBreak(t)
case Some(_) if callSite =>
def delayBreakBefore(token: T)(implicit fileLine: FileLine) =
delayedBreakPolicyFor(token)(decideNewlinesOnlyBeforeToken)
def delayBreakBefore(token: T): Policy = {
// force break if multiline and if there's no other break
val lastEnd = lastFT.left.end
delayedBreakPolicy(Policy.End > lastEnd)(
Policy.RelayOnSplit { case (s, nextft) =>
s.isNL && nextft.left.end > lastEnd // don't need anymore
}(decideNewlinesOnlyBeforeToken(token), NoPolicy),
)
}

val endCall = leftOwner.parent.flatMap(followedBySelectOrApply)
endCall.fold(Policy.noPolicy) { x =>
val beforeNext = nextNonCommentSameLine(getLastNonTrivial(x))
beforeNext.right match {
case c: T.Comma => delayBreakBefore(c)
case c: T.Dot => delayBreakBefore(c)
case LeftParenOrBracket() =>
nextNonCommentSameLineAfter(beforeNext).right match {
case _: T.Comment => NoPolicy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2803,9 +2803,7 @@ object a {
test("foo") {
a.b(c, d) shouldBe
E(Seq(F(1, "v1"), F(2, "v2")),
G(Some(Seq(h, i)),
Some(Seq(j, k)), a.b, c.d,
e.f.g, h.i.j)).foo
G(Some(Seq(h, i)), Some(Seq(j, k)), a.b, c.d, e.f.g, h.i.j)).foo
}
}
<<< binpack call, oneline, with syntaxNL, single arg
Expand Down
4 changes: 1 addition & 3 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -2803,9 +2803,7 @@ object a {
test("foo") {
a.b(c, d) shouldBe
E(Seq(F(1, "v1"), F(2, "v2")),
G(Some(Seq(h, i)),
Some(Seq(j, k)), a.b, c.d,
e.f.g, h.i.j)).foo
G(Some(Seq(h, i)), Some(Seq(j, k)), a.b, c.d, e.f.g, h.i.j)).foo
}
}
<<< binpack call, oneline, with syntaxNL, single arg
Expand Down

0 comments on commit e5ad60f

Please sign in to comment.