Skip to content

Commit

Permalink
Router: preserve breaks for source.keep in case
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Oct 16, 2020
1 parent b768f88 commit b7fe3d8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1777,12 +1777,15 @@ class Router(formatOps: FormatOps) {
}

// Pat
case tok @ FormatToken(T.Ident("|"), _, _)
if leftOwner.is[Pat.Alternative] =>
Seq(
Split(Space, 0),
Split(Newline, 1)
)
case FormatToken(T.Ident("|"), _, _) if leftOwner.is[Pat.Alternative] =>
if (style.newlines.source eq Newlines.keep)
Seq(Split(Space.orNL(newlines == 0), 0))
else
Seq(Split(Space, 0), Split(Newline, 1))
case FormatToken(_, T.Ident("|"), _) if rightOwner.is[Pat.Alternative] =>
val noNL = style.newlines.source.ne(Newlines.keep) || newlines == 0
Seq(Split(Space.orNL(noNL), 0))

case FormatToken(
T.Ident(_) | Literal() | T.Interpolation.End() | T.Xml.End(),
T.Ident(_) | Literal() | T.Xml.Start(),
Expand Down Expand Up @@ -1927,7 +1930,9 @@ class Router(formatOps: FormatOps) {
}
Space(style.spaces.inParentheses && allowSpace)
}
Seq(Split(modNoNL, 0))
val isNL = rightOwner.is[Pat.Alternative] &&
style.newlines.source.eq(Newlines.keep) && newlines != 0
Seq(Split(if (isNL) Newline else modNoNL, 0))

case FormatToken(left, _: T.KwCatch | _: T.KwFinally, _)
if style.newlines.alwaysBeforeElseAfterCurlyIf
Expand Down
22 changes: 14 additions & 8 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3130,12 +3130,15 @@ a match {
}
>>>
a match {
case (Root / "foo" | Root / "bar" |
Root / "baz" / _ / "qux") =>
case (Root / "foo"
| Root / "bar"
| Root / "baz" / _ / "qux"
) =>
(if (foo) (foo) + bar
else foo + (bar)) + (baz)
case (Root / "foo" | Root / "bar" |
Root / "baz" / _ / "qux") => (
case (Root / "foo"
| Root / "bar"
| Root / "baz" / _ / "qux") => (
foo
)
}
Expand All @@ -3155,12 +3158,15 @@ a match {
}
>>>
a match {
case ( Root / "foo" | Root / "bar" |
Root / "baz" / _ / "qux" ) =>
case ( Root / "foo"
| Root / "bar"
| Root / "baz" / _ / "qux"
) =>
( if (foo) ( foo ) + bar
else foo + ( bar ) ) + ( baz )
case ( Root / "foo" | Root / "bar" |
Root / "baz" / _ / "qux" ) => (
case ( Root / "foo"
| Root / "bar"
| Root / "baz" / _ / "qux" ) => (
foo
)
}

0 comments on commit b7fe3d8

Please sign in to comment.