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

Router: beforeCurlyLambdaParams for paren lambdas #2193

Merged
merged 3 commits into from
Sep 14, 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 @@ -592,44 +592,55 @@ 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 lambdaLeft: Option[Token] =
matchingOpt(functionExpire(lambda)._1).filter(_.is[T.LeftBrace])

val arrowFt = getFuncArrow(lambda).get
val lambdaIsABlock = lambdaLeft.contains(arrowFt.right)
val lambdaToken =
getOptimalTokenFor(if (lambdaIsABlock) next(arrowFt) else arrowFt)

val close = matching(formatToken.left)
val newlinePolicy =
if (!style.danglingParentheses.callSite) None
else Some(decideNewlinesOnlyBeforeClose(close))
val spacePolicy = SingleLineBlock(lambdaToken) | {
if (lambdaIsABlock) None
else
newlinePolicy.map(
delayedBreakPolicy(Policy.End.On(lambdaLeft.getOrElse(close)))
)
val noSplitMod =
if (style.newlines.alwaysBeforeCurlyLambdaParams) null
else getNoSplit(formatToken, true)

def multilineSpaceSplit(implicit line: sourcecode.Line): Split = {
val lambdaLeft: Option[Token] =
matchingOpt(functionExpire(lambda)._1).filter(_.is[T.LeftBrace])

val arrowFt = getFuncArrow(lambda).get
val lambdaIsABlock = lambdaLeft.contains(arrowFt.right)
val lambdaToken =
getOptimalTokenFor(if (lambdaIsABlock) next(arrowFt) else arrowFt)

val spacePolicy = SingleLineBlock(lambdaToken) | {
if (lambdaIsABlock) None
else
newlinePolicy.map(
delayedBreakPolicy(Policy.End.On(lambdaLeft.getOrElse(close)))
)
}
Split(noSplitMod, 0)
.withPolicy(spacePolicy)
.withOptimalToken(lambdaToken)
}

val noSplitMod = getNoSplit(formatToken, true)
val newlinePenalty = 3 + nestedApplies(leftOwner)
Seq(
Split(noSplitMod, 0)
.onlyIf(noSplitMod != null)
.withSingleLine(close),
Split(noSplitMod, 0, policy = spacePolicy)
.onlyIf(noSplitMod != null)
.withOptimalToken(lambdaToken),
Split(Newline, newlinePenalty)
.withPolicyOpt(newlinePolicy)
.withIndent(style.continuationIndent.callSite, close, Before)
)
}
if (noSplitMod == null)
Seq(
Split(Newline, 0)
.withPolicyOpt(newlinePolicy)
.withIndent(style.continuationIndent.callSite, close, Before)
)
else {
val newlinePenalty = 3 + nestedApplies(leftOwner)
val noMultiline = style.newlines.beforeCurlyLambdaParams eq
Newlines.BeforeCurlyLambdaParams.multiline
Seq(
Split(noSplitMod, 0).withSingleLine(close),
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
63 changes: 63 additions & 0 deletions scalafmt-tests/src/test/resources/unit/Lambda.stat
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,29 @@ newlines.beforeCurlyLambdaParams = false
===
object a {
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 { 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" }
x.flatMap(result => for {
_ <- a
_ <- b
} yield ()
)
}
>>>
object a {
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 { case (c, i) =>
s"$c -> $i"
}
Expand All @@ -174,29 +184,45 @@ object a {
case (c, i) if c != i =>
s"$c -> $i"
}
x.flatMap(result =>
for {
_ <- a
_ <- b
} yield ())
}
<<< #2099 beforeCurlyLambdaParams = true
newlines.beforeCurlyLambdaParams = true
===
object a {
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 { 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" }
x.flatMap(result => for {
_ <- a
_ <- b
} yield ()
)
}
>>>
object a {
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 {
case (c, i) => s"$c -> $i"
}
Expand All @@ -208,25 +234,41 @@ object a {
case (c, i) if c != i =>
s"$c -> $i"
}
x.flatMap(
result =>
for {
_ <- a
_ <- b
} yield ())
}
<<< #2099 beforeCurlyLambdaParams = multilineWithCaseOnly
newlines.beforeCurlyLambdaParams = multilineWithCaseOnly
===
object a {
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 { 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" }
x.flatMap(result => for {
_ <- a
_ <- b
} yield ()
)
}
>>>
object a {
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 {
case (c, i) => s"$c -> $i"
}
Expand All @@ -238,25 +280,40 @@ object a {
case (c, i) if c != i =>
s"$c -> $i"
}
x.flatMap(result =>
for {
_ <- a
_ <- b
} yield ())
}
<<< #2099 beforeCurlyLambdaParams = multiline
newlines.beforeCurlyLambdaParams = multiline
===
object a {
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 { 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" }
x.flatMap(result => for {
_ <- a
_ <- b
} yield ()
)
}
>>>
object a {
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 {
case (c, i) => s"$c -> $i"
}
Expand All @@ -268,4 +325,10 @@ object a {
case (c, i) if c != i =>
s"$c -> $i"
}
x.flatMap(
result =>
for {
_ <- a
_ <- b
} yield ())
}