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
  • Loading branch information
kitbellew committed Feb 18, 2020
1 parent 18552ba commit f080da3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,28 @@ class FormatOps(val tree: Tree, val initStyle: ScalafmtConfig) {
// Newline on every comma.
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_01)
splits
else
t.meta.rightOwner match {
case _: Term.PartialFunction |
Term.Block(List(_: Term.Function | _: Term.PartialFunction)) =>
Seq(Split(Newline, 0))
case _ =>
val closeBrace = matching(right)
val breakAfter = nextNonCommentSameLine(t).right
val multiLine = newlinesOnlyBeforeClosePolicy(closeBrace)
.orElse(decideNewlinesOnlyAfterToken(breakAfter))
Seq(
Split(Newline, 0, policy = SingleLineBlock(closeBrace))
.withOptimalToken(closeBrace, killOnFail = true),
Split(Space, 1, policy = multiLine)
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -906,21 +906,8 @@ class Router(formatOps: FormatOps) {
Split(NoSplit, 0)
)
// non-statement starting curly brace
case FormatToken(left, open @ T.LeftBrace(), _) =>
val close = matching(open)
val isComma = left.is[T.Comma]
val bodyHasNewlines = if (isComma) {
open.pos.endLine != close.pos.startLine
} else {
true
}
Seq(
Split(Space, 0),
Split(Newline, 0)
.onlyIf(isComma && newlines != 0 && !bodyHasNewlines)
.withOptimalToken(close, killOnFail = true)
.withPolicy(SingleLineBlock(close))
)
case FormatToken(_, _: T.LeftBrace, _) =>
Seq(Split(Space, 0))

// Delim
case FormatToken(_, T.Comma(), _) =>
Expand Down
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 @@ -714,7 +714,24 @@ object A {
}
}
>>>
Unable to format file due to bug in scalafmt
object A {
def foo() = {
x.map(
_.y(
abcd,
{ case ((abcdefghij, aswbasdfaw), asfda) => aswdf },
{
case (abcdefghij, sadfasdass) =>
(
asdfa.sadvfs(abcdefghij).get,
asdfasdfasfdasda.asdfas(asdfasdaas).get
)
},
foo
)
)
}
}
<<< #1599 2: modified, with partial func inside block
object A {
def foo() = {
Expand All @@ -727,7 +744,26 @@ object A {
}
}
>>>
Unable to format file due to bug in scalafmt
object A {
def foo() = {
x.map(
_.y(
abcd,
{ { case ((abcdefghij, aswbasdfaw), asfda) => aswdf } },
{
{
case (abcdefghij, sadfasdass) =>
(
asdfa.sadvfs(abcdefghij).get,
asdfasdfasfdasda.asdfas(asdfasdaas).get
)
}
},
foo
)
)
}
}
<<< #1599 3: modified, with lambda
object A {
def foo() = {
Expand All @@ -740,4 +776,20 @@ object A {
}
}
>>>
Unable to format file due to bug in scalafmt
object A {
def foo() = {
x.map(
_.y(
abcd,
{ case_abcdefghij_aswbasdfaw_asfda => aswdf },
{ case_abcdefghij_sadfasdass =>
(
asdfa.sadvfs(abcdefghij).get,
asdfasdfasfdasda.asdfas(asdfasdaas).get
)
},
foo
)
)
}
}

0 comments on commit f080da3

Please sign in to comment.