Skip to content

Commit

Permalink
FormatOps: in one-arg-per-line, handle lbrace case
Browse files Browse the repository at this point in the history
Fixes #1599.
  • Loading branch information
kitbellew committed Feb 24, 2020
1 parent 5c9ec4b commit 0270bb3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,14 @@ class FormatOps(val tree: Tree, val initStyle: ScalafmtConfig) {
case Decision(t @ FormatToken(_: T.Comma, right, _), splits)
if owner == t.meta.leftOwner &&
// TODO(olafur) what the right { decides to be single line?
!right.is[T.LeftBrace] &&
// If comment is bound to comma, see unit/Comment.
(!right.is[T.Comment] || t.newlinesBetween != 0) =>
splits.filter(_.modification.isNewline)
if (!right.is[T.LeftBrace])
splits.filter(_.modification.isNewline)
else if (!style.activeForEdition_2020_03)
splits
else
SplitTag.OneArgPerLine.activateOnly(splits)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,14 +893,34 @@ class Router(formatOps: FormatOps) {
else isDefnSite(leftOwner) && !style.binPack.unsafeDefnSite
} =>
val close = matching(open)
val oneArgPerLineSplits =
if (!style.activeForEdition_2020_03) Seq.empty
else
(rightOwner match {
case _: Term.PartialFunction | Term.Block(
List(_: Term.Function | _: Term.PartialFunction)
) =>
Seq(Split(Newline, 0))
case _ =>
val breakAfter =
rhsOptimalToken(next(nextNonCommentSameLine(formatToken)))
val multiLine =
newlinesOnlyBeforeClosePolicy(close)
.orElse(decideNewlinesOnlyAfterToken(breakAfter))
Seq(
Split(Newline, 0, policy = SingleLineBlock(close))
.withOptimalToken(close, killOnFail = true),
Split(Space, 1, policy = multiLine)
)
}).map(_.onlyFor(SplitTag.OneArgPerLine))
def oneLineBody = open.pos.endLine == close.pos.startLine
Seq(
Split(Space, 0),
Split(Newline, 0)
.onlyIf(newlines != 0 && oneLineBody)
.withOptimalToken(close, killOnFail = true)
.withPolicy(SingleLineBlock(close))
)
) ++ oneArgPerLineSplits
case FormatToken(_, _: T.LeftBrace, _) =>
Seq(Split(Space, 0))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ object SplitTag {
case object Active extends Base
case object Ignored extends Base

case object OneArgPerLine extends Custom

}
3 changes: 2 additions & 1 deletion scalafmt-tests/src/test/resources/default/Apply.stat
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ writeRead(
// Use list because Array has a shitty toString
{ (b: List[Byte], os) =>
os.writePosVarInt(b.size); os.writeBytes(b.toArray)
}, { is =>
},
{ is =>
val bytes = new Array[Byte](is.readPosVarInt)
is.readFully(bytes)
bytes.toList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,8 @@ object A {
x.map(
_.y(
abcd,
{ case ((abcdefghij, aswbasdfaw), asfda) => aswdf }, {
{ case ((abcdefghij, aswbasdfaw), asfda) => aswdf },
{
case (abcdefghij, sadfasdass) =>
(
asdfa.sadvfs(abcdefghij).get,
Expand Down Expand Up @@ -748,7 +749,8 @@ object A {
x.map(
_.y(
abcd,
{ { case ((abcdefghij, aswbasdfaw), asfda) => aswdf } }, {
{ { case ((abcdefghij, aswbasdfaw), asfda) => aswdf } },
{
{
case (abcdefghij, sadfasdass) =>
(
Expand Down Expand Up @@ -779,12 +781,12 @@ object A {
x.map(
_.y(
abcd,
{ case_abcdefghij_aswbasdfaw_asfda => aswdf }, {
case_abcdefghij_sadfasdass =>
(
asdfa.sadvfs(abcdefghij).get,
asdfasdfasfdasda.asdfas(asdfasdaas).get
)
{ case_abcdefghij_aswbasdfaw_asfda => aswdf },
{ case_abcdefghij_sadfasdass =>
(
asdfa.sadvfs(abcdefghij).get,
asdfasdfasfdasda.asdfas(asdfasdaas).get
)
},
foo
)
Expand Down

0 comments on commit 0270bb3

Please sign in to comment.