Skip to content

Commit

Permalink
FormatOps: in folded ctrl body, use adjustedBody
Browse files Browse the repository at this point in the history
It is already checked for any removed braces, while the original `body`
is not and hence could lead to non-idempotent formatting.
  • Loading branch information
kitbellew committed Jun 21, 2024
1 parent 644e78a commit 08cf6b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1510,13 +1510,13 @@ class FormatOps(
val callPolicy = CallSite.getFoldedPolicy(lia.lhs)
// lia is enclosed in parens if and only if lia == ia (== body)
if (callPolicy.nonEmpty) getPolicySplits(0, callPolicy)
else if (isBodyEnclosedAsBlock(body))
else if (isBodyEnclosedAsBlock(ia))
if (isKeep) getPolicySplits(0, Policy.NoPolicy)
else getSplits(getSlbSplit(getLastToken(lia.lhs)))
else getSplits(getSlbSplit(getLastToken(lia.op)))
case _ =>
val callPolicy = CallSite.getFoldedPolicy(body)
val nlCost = body match {
case b =>
val callPolicy = CallSite.getFoldedPolicy(b)
val nlCost = b match {
case Member.Apply(_, ac)
if styleMap.at(getHead(ac)).binPack.callSiteFor(ac).isOneline =>
3
Expand Down
40 changes: 20 additions & 20 deletions scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1766,16 +1766,16 @@ object a {
}
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
nest: Int,
- ): Option[NumBlanks] = topStatBlankLinesSorted.iterator
- .takeWhile(_.minBreaks <= numBreaks).find { x =>
- x.checkNest(nest) && x.pattern.forall(_.matcher(prefix).find())
+ ): Option[NumBlanks] =
+ topStatBlankLinesSorted.iterator.takeWhile(_.minBreaks <= numBreaks).find {
+ x => x.checkNest(nest) && x.pattern.forall(_.matcher(prefix).find())
}.flatMap(_.blanks)
object a {
def getTopStatBlankLines(
prefix: String,
numBreaks: Int,
nest: Int,
): Option[NumBlanks] = topStatBlankLinesSorted.iterator
.takeWhile(_.minBreaks <= numBreaks).find { x =>
x.checkNest(nest) && x.pattern.forall(_.matcher(prefix).find())
}.flatMap(_.blanks)
}
<<< fold ctrl body with redundant braces, !trailing commas
maxColumn = 80
newlines.source = fold
Expand All @@ -1794,13 +1794,13 @@ object a {
}
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
nest: Int
- ): Option[NumBlanks] = topStatBlankLinesSorted.iterator
- .takeWhile(_.minBreaks <= numBreaks).find { x =>
- x.checkNest(nest) && x.pattern.forall(_.matcher(prefix).find())
+ ): Option[NumBlanks] =
+ topStatBlankLinesSorted.iterator.takeWhile(_.minBreaks <= numBreaks).find {
+ x => x.checkNest(nest) && x.pattern.forall(_.matcher(prefix).find())
}.flatMap(_.blanks)
object a {
def getTopStatBlankLines(
prefix: String,
numBreaks: Int,
nest: Int
): Option[NumBlanks] = topStatBlankLinesSorted.iterator
.takeWhile(_.minBreaks <= numBreaks).find { x =>
x.checkNest(nest) && x.pattern.forall(_.matcher(prefix).find())
}.flatMap(_.blanks)
}

0 comments on commit 08cf6b2

Please sign in to comment.