From 846af2f4ac45fcfedb50a6b69f2ae7166940a61c Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sat, 8 Jun 2024 07:46:50 +0200 Subject: [PATCH 1/2] Add scala.js tests with binpacked `val` rhs --- .../src/test/resources/scalajs/Apply.stat | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/scalafmt-tests/src/test/resources/scalajs/Apply.stat b/scalafmt-tests/src/test/resources/scalajs/Apply.stat index 6597e0f1b5..9c61e80988 100644 --- a/scalafmt-tests/src/test/resources/scalajs/Apply.stat +++ b/scalafmt-tests/src/test/resources/scalajs/Apply.stat @@ -1153,3 +1153,60 @@ object a { global.NewFromConstructor(ctor, argss.head: _*))( Apply(_, _)) } +<<< don't break after equals if bp, overflow +preset = default +maxColumn = 85 +newlines.source = keep +binPack.preset = oneline +danglingParentheses.preset = false +newlines.configStyleCallSite.prefer = true +newlines.avoidForSimpleOverflow = [tooLong, punct, slc] +=== +object a { +val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static, captures = Nil)( + prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref)) +val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static)( + prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref)) +val rhs = genScalaArg(prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))( + sym, index, formalArgsRegistry, param, static, captures = Nil) +} +>>> +object a { + val rhs = + genScalaArg(sym, index, formalArgsRegistry, param, static, captures = Nil)( + prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref)) + val rhs = + genScalaArg(sym, index, formalArgsRegistry, param, static)(prevArgsCount => + varDefs.take(prevArgsCount).toList.map(_.ref)) + val rhs = + genScalaArg(prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))( + sym, index, formalArgsRegistry, param, static, captures = Nil) +} +<<< don't break after equals if bp, !overflow +preset = default +maxColumn = 85 +newlines.source = keep +binPack.preset = oneline +danglingParentheses.preset = false +newlines.configStyleCallSite.prefer = true +=== +object a { +val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static, captures = Nil)( + prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref)) +val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static)( + prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref)) +val rhs = genScalaArg(prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))( + sym, index, formalArgsRegistry, param, static, captures = Nil) +} +>>> +object a { + val rhs = + genScalaArg(sym, index, formalArgsRegistry, param, static, captures = Nil)( + prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref)) + val rhs = + genScalaArg(sym, index, formalArgsRegistry, param, static)(prevArgsCount => + varDefs.take(prevArgsCount).toList.map(_.ref)) + val rhs = + genScalaArg(prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))( + sym, index, formalArgsRegistry, param, static, captures = Nil) +} From 303d135810b421878796704c7d8b54389e5dd6ac Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Fri, 14 Jun 2024 06:27:38 -0700 Subject: [PATCH 2/2] FormatOps: raise NL cost for binpacked ctrl body --- .../org/scalafmt/internal/FormatOps.scala | 24 +++++++++----- .../src/test/resources/scalajs/Apply.stat | 32 ++++++++----------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index 2fdf77c119..d97761bf03 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -1455,11 +1455,13 @@ class FormatOps( val expire = nextNonCommentSameLine(blastFT).left def penalize(penalty: Int) = Policy ? (penalty > 0) && new PolicyOps.PenalizeAllNewlines(Policy.End == blast, penalty) - def getNlSplit(penalty: Int)(implicit fileLine: FileLine): Split = - nlSplitFunc(1).andPolicy(penalize(penalty)).forThisLine(nextLine) - def getSplits(spaceSplit: Split) = ( + def getNlSplit(penalty: Int, nlCost: Int = 1)(implicit + fileLine: FileLine, + ): Split = nlSplitFunc(nlCost).andPolicy(penalize(penalty)) + .forThisLine(nextLine) + def getSplits(spaceSplit: Split, nlCost: Int = 1) = ( spaceSplit.withIndents(spaceIndents), - getNlSplit(1)(nextLine(spaceSplit.fileLine)), + getNlSplit(1, nlCost)(nextLine(spaceSplit.fileLine)), ) def getSlb(end: T, excl: TokenRanges)(implicit fileLine: FileLine) = SingleLineBlock(end, exclude = excl, noSyntaxNL = true) @@ -1476,9 +1478,9 @@ class FormatOps( val spacePolicy = policy | penalize(penalty) Split(Space, 0).withPolicy(spacePolicy).withOptimalToken(blast) } - def getPolicySplits(penalty: Int, policy: Policy)(implicit - fileLine: FileLine, - ) = getSplits(getSpaceSplit(penalty, policy)) + def getPolicySplits(penalty: Int, policy: Policy, nlCost: Int = 1)( + implicit fileLine: FileLine, + ) = getSplits(getSpaceSplit(penalty, policy), nlCost) def getSlbSplits( exclude: TokenRanges = TokenRanges.empty, policy: Policy = Policy.NoPolicy, @@ -1526,7 +1528,13 @@ class FormatOps( else getSplits(getSlbSplit(getLastToken(lia.op))) case _ => val callPolicy = CallSite.getFoldedPolicy(body) - getPolicySplits(if (callPolicy.nonEmpty) 0 else 1, callPolicy) + val nlCost = body match { + case Member.Apply(_, ac) + if styleMap.at(getHead(ac)).binPack.callSiteFor(ac).isOneline => + 3 + case _ => 1 + } + getPolicySplits(if (callPolicy.nonEmpty) 0 else 1, callPolicy, nlCost) } Seq(spaceSplit, nlSplit) diff --git a/scalafmt-tests/src/test/resources/scalajs/Apply.stat b/scalafmt-tests/src/test/resources/scalajs/Apply.stat index 9c61e80988..31fdcfe763 100644 --- a/scalafmt-tests/src/test/resources/scalajs/Apply.stat +++ b/scalafmt-tests/src/test/resources/scalajs/Apply.stat @@ -1172,15 +1172,13 @@ val rhs = genScalaArg(prevArgsCount => varDefs.take(prevArgsCount).toList.map(_. } >>> object a { - val rhs = - genScalaArg(sym, index, formalArgsRegistry, param, static, captures = Nil)( - prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref)) - val rhs = - genScalaArg(sym, index, formalArgsRegistry, param, static)(prevArgsCount => - varDefs.take(prevArgsCount).toList.map(_.ref)) - val rhs = - genScalaArg(prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))( - sym, index, formalArgsRegistry, param, static, captures = Nil) + val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static, captures = Nil)( + prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref)) + val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static)( + prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref)) + val rhs = genScalaArg(prevArgsCount => + varDefs.take(prevArgsCount).toList.map(_.ref))( + sym, index, formalArgsRegistry, param, static, captures = Nil) } <<< don't break after equals if bp, !overflow preset = default @@ -1200,13 +1198,11 @@ val rhs = genScalaArg(prevArgsCount => varDefs.take(prevArgsCount).toList.map(_. } >>> object a { - val rhs = - genScalaArg(sym, index, formalArgsRegistry, param, static, captures = Nil)( - prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref)) - val rhs = - genScalaArg(sym, index, formalArgsRegistry, param, static)(prevArgsCount => - varDefs.take(prevArgsCount).toList.map(_.ref)) - val rhs = - genScalaArg(prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))( - sym, index, formalArgsRegistry, param, static, captures = Nil) + val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static, + captures = Nil)(prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref)) + val rhs = genScalaArg(sym, index, formalArgsRegistry, param, static)( + prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref)) + val rhs = genScalaArg(prevArgsCount => + varDefs.take(prevArgsCount).toList.map(_.ref))( + sym, index, formalArgsRegistry, param, static, captures = Nil) }