Skip to content

Commit

Permalink
Router: beforeCurlyLambdaParams for paren lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Sep 12, 2020
1 parent 9dada22 commit bfcde48
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -592,15 +592,15 @@ class Router(formatOps: FormatOps) {

// Term.Apply and friends
case FormatToken(T.LeftParen(), _, _)
if style.optIn.configStyleArguments &&
!style.newlines.alwaysBeforeCurlyLambdaParams &&
getLambdaAtSingleArgCallSite(formatToken).isDefined => {
if getLambdaAtSingleArgCallSite(formatToken).isDefined =>
val lambda = getLambdaAtSingleArgCallSite(formatToken).get
val close = matching(formatToken.left)
val newlinePolicy =
if (!style.danglingParentheses.callSite) None
else Some(decideNewlinesOnlyBeforeClose(close))
val noSplitMod = getNoSplit(formatToken, true)
val noSplitMod =
if (style.newlines.alwaysBeforeCurlyLambdaParams) null
else getNoSplit(formatToken, true)

def multilineSpaceSplit(implicit line: sourcecode.Line): Split = {
val lambdaLeft: Option[Token] =
Expand Down Expand Up @@ -631,15 +631,16 @@ class Router(formatOps: FormatOps) {
)
else {
val newlinePenalty = 3 + nestedApplies(leftOwner)
val noMultiline = style.newlines.beforeCurlyLambdaParams eq
Newlines.BeforeCurlyLambdaParams.multiline
Seq(
Split(noSplitMod, 0).withSingleLine(close),
multilineSpaceSplit,
if (noMultiline) Split.ignored else multilineSpaceSplit,
Split(Newline, newlinePenalty)
.withPolicyOpt(newlinePolicy)
.withIndent(style.continuationIndent.callSite, close, Before)
)
}
}

case FormatToken(T.LeftParen(), T.RightParen(), _) =>
val noNL = style.newlines.sourceIgnored || formatToken.noBreak
Expand Down
33 changes: 18 additions & 15 deletions scalafmt-tests/src/test/resources/unit/Lambda.stat
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,16 @@ object a {
x.map {
x => s"${x._1} -> ${x._2}"
}
x.map(x => s"${x._1} -> ${x._2}")
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(x =>
s"${x._1} -> ${x._2}")
x.zipWithIndex.map(
x => s"${x._1} -> ${x._2}")
x.zipWithIndex.map {
case (c, i) => s"$c -> $i"
}
Expand All @@ -233,11 +234,12 @@ object a {
case (c, i) if c != i =>
s"$c -> $i"
}
x.flatMap(result =>
for {
_ <- a
_ <- b
} yield ())
x.flatMap(
result =>
for {
_ <- a
_ <- b
} yield ())
}
<<< #2099 beforeCurlyLambdaParams = multilineWithCaseOnly
newlines.beforeCurlyLambdaParams = multilineWithCaseOnly
Expand Down Expand Up @@ -310,8 +312,8 @@ object a {
x.zipWithIndex.map {
x => s"${x._1} -> ${x._2}"
}
x.zipWithIndex.map(x =>
s"${x._1} -> ${x._2}")
x.zipWithIndex.map(
x => s"${x._1} -> ${x._2}")
x.zipWithIndex.map {
case (c, i) => s"$c -> $i"
}
Expand All @@ -323,9 +325,10 @@ object a {
case (c, i) if c != i =>
s"$c -> $i"
}
x.flatMap(result =>
for {
_ <- a
_ <- b
} yield ())
x.flatMap(
result =>
for {
_ <- a
_ <- b
} yield ())
}

0 comments on commit bfcde48

Please sign in to comment.