Skip to content

Commit

Permalink
FormatOps: in CtrlBodySplits, add consistency
Browse files Browse the repository at this point in the history
Previously, the code was computing splits differently for tokens with a
break and without one. Since we could potentially have no-break in the
source but format with one, this could introduce non-idempotency.
  • Loading branch information
kitbellew committed May 16, 2024
1 parent 60cf2d6 commit 416c540
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1644,21 +1644,20 @@ class FormatOps(
classicNoBreakFunc: => Split,
)(nlSplitFunc: Int => Split)(implicit style: ScalafmtConfig): Seq[Split] =
checkComment(ft, nlSplitFunc) { x =>
def getFolded(isKeep: Boolean) =
foldedNonComment(body, nlSplitFunc, isKeep = isKeep, spaceIndents)
style.newlines.getBeforeMultiline match {
case Newlines.fold => getFolded(false)
case Newlines.unfold =>
unfoldedNonComment(body, nlSplitFunc, spaceIndents, false)
case Newlines.classic | Newlines.keep if x.hasBreak =>
Seq(nlSplitFunc(0).forThisLine)
case Newlines.classic => Option(classicNoBreakFunc).fold {
foldedNonComment(body, nlSplitFunc, isKeep = true, spaceIndents)
} { func =>
case Newlines.classic if x.noBreak =>
Option(classicNoBreakFunc).fold(getFolded(true)) { func =>
val spcSplit = func.forThisLine
val nlSplit = nlSplitFunc(spcSplit.getCost(_ + 1, 0)).forThisLine
Seq(spcSplit, nlSplit)
}
case sh => // fold or keep without break
val isKeep = sh eq Newlines.keep
foldedNonComment(body, nlSplitFunc, isKeep, spaceIndents)
case Newlines.keep if x.noBreak => getFolded(true)
case _ => getFolded(true).filter(_.isNL) // keep/classic with break
}
}

Expand Down
13 changes: 6 additions & 7 deletions scalafmt-tests/src/test/resources/scalajs/DefDef.stat
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,9 @@ object a {
}
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
val registry =
- new js_FinalizationRegistry[js_Date, String, Any]((heldValue: String) =>
- ())
+ new js_FinalizationRegistry[js_Date, String, Any]((heldValue: String) => ())
}
object a {
def foo = {
val registry =
new js_FinalizationRegistry[js_Date, String, Any]((heldValue: String) => ())
}
}

0 comments on commit 416c540

Please sign in to comment.