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

FormatOps: in CtrlBodySplits, add consistency #4003

Merged
merged 2 commits into from
May 16, 2024
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 @@ -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
23 changes: 22 additions & 1 deletion scalafmt-tests/src/test/resources/scalajs/DefDef.stat
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ newlines.source = keep
binPack.preset = oneline
danglingParentheses.preset = false
newlines.configStyleDefnSite.prefer = true
# newlines.avoidForSimpleOverflow = [tooLong, punct, slc]
newlines.avoidForSimpleOverflow = [tooLong, punct, slc]
===
object a {
def getArrayUnderlyingTypedArrayClassRef(elemTypeRef: NonArrayTypeRef)(implicit tracking: GlobalRefTracking, pos: Position): Option[WithGlobals[VarRef]] = {
Expand All @@ -556,3 +556,24 @@ object a {
// foo
}
}
<<< break at maxColumn, with overflow enabled 3
preset = default
maxColumn = 80
newlines.source = keep
binPack.preset = oneline
danglingParentheses.preset = false
newlines.configStyleDefnSite.prefer = true
newlines.avoidForSimpleOverflow = [tooLong, punct, slc]
===
object a {
def foo = {
val registry = new js_FinalizationRegistry[js_Date, String, Any]((heldValue: String) => ())
}
}
>>>
object a {
def foo = {
val registry =
new js_FinalizationRegistry[js_Date, String, Any]((heldValue: String) => ())
}
}
Loading