Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newlines: break before params of multiline lambda #2191

Merged
merged 2 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,6 @@ case class Newlines(
lazy val avoidForSimpleOverflowTooLong: Boolean =
avoidForSimpleOverflow.contains(AvoidForSimpleOverflow.tooLong)

lazy val neverBeforeCurlyLambdaParams = !alwaysBeforeCurlyBraceLambdaParams &&
(beforeCurlyLambdaParams eq BeforeCurlyLambdaParams.never)

lazy val alwaysBeforeCurlyLambdaParams = alwaysBeforeCurlyBraceLambdaParams ||
(beforeCurlyLambdaParams eq BeforeCurlyLambdaParams.always)

Expand Down Expand Up @@ -328,11 +325,13 @@ object Newlines {
object BeforeCurlyLambdaParams {
case object always extends BeforeCurlyLambdaParams
case object never extends BeforeCurlyLambdaParams
case object multiline extends BeforeCurlyLambdaParams
case object multilineWithCaseOnly extends BeforeCurlyLambdaParams
implicit val codec: ConfCodec[BeforeCurlyLambdaParams] =
ReaderUtil.oneOfCustom[BeforeCurlyLambdaParams](
never,
always,
multiline,
multilineWithCaseOnly
) {
case Conf.Bool(true) => Configured.Ok(always)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,13 @@ class Router(formatOps: FormatOps) {
case Some(owner: Term.Function) =>
val arrow = getFuncArrow(lastLambda(owner))
val expire = arrow.getOrElse(tokens(owner.tokens.last))
val nlOnly = Some(style.newlines.alwaysBeforeCurlyLambdaParams)
val nlOnly =
if (style.newlines.alwaysBeforeCurlyLambdaParams) Some(true)
else if (
style.newlines.beforeCurlyLambdaParams eq
Newlines.BeforeCurlyLambdaParams.multiline
) None
else Some(false)
(expire, arrow.map(_.left), 0, nlOnly)
case Some(t: Case) if t.cond.isEmpty && (leftOwner match {
case Term.PartialFunction(List(`t`)) => true
Expand All @@ -206,8 +212,8 @@ class Router(formatOps: FormatOps) {
val nlOnly =
if (style.newlines.alwaysBeforeCurlyLambdaParams) Some(true)
else if (
style.newlines.beforeCurlyLambdaParams eq
Newlines.BeforeCurlyLambdaParams.multilineWithCaseOnly
style.newlines.beforeCurlyLambdaParams ne
Newlines.BeforeCurlyLambdaParams.never
) None
else Some(false)
(arrow, Some(arrow.left), 0, nlOnly)
Expand Down
30 changes: 30 additions & 0 deletions scalafmt-tests/src/test/resources/unit/Lambda.stat
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,33 @@ object a {
s"$c -> $i"
}
}
<<< #2099 beforeCurlyLambdaParams = multiline
newlines.beforeCurlyLambdaParams = multiline
===
object a {
x.map { x => s"${x._1} -> ${x._2}" }
x.map { case (c, i) => s"$c -> $i" }
x.zipWithIndex.map { x => s"${x._1} -> ${x._2}" }
x.zipWithIndex.map { case (c, i) => s"$c -> $i" }
x.zipWithIndex.map { case (c, i) => s"$c -> $i (long comment)" }
x.zipWithIndex.map { case (c, i) if c != i => s"$c -> $i" }
}
>>>
object a {
x.map { x => s"${x._1} -> ${x._2}" }
x.map { case (c, i) => s"$c -> $i" }
x.zipWithIndex.map {
x => s"${x._1} -> ${x._2}"
}
x.zipWithIndex.map {
case (c, i) => s"$c -> $i"
}
x.zipWithIndex.map {
case (c, i) =>
s"$c -> $i (long comment)"
}
x.zipWithIndex.map {
case (c, i) if c != i =>
s"$c -> $i"
}
}