From acd55952b7df7c94b7085182d83e46c80fa13b52 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 19 May 2024 20:44:50 -0700 Subject: [PATCH] PolicyOps: move penalizeNewlineByNesting --- .../main/scala/org/scalafmt/internal/FormatOps.scala | 12 ------------ .../src/main/scala/org/scalafmt/util/PolicyOps.scala | 12 ++++++++++++ 2 files changed, 12 insertions(+), 12 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 3dcf32febf..494bb5b953 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 @@ -317,18 +317,6 @@ class FormatOps( if (r.is[T.LeftBrace]) SplitTag.OneArgPerLine.activateOnly(s) else Decision.onlyNewlineSplits(s) - def penalizeNewlineByNesting(from: T, to: T)(implicit - fileLine: FileLine, - ): Policy = { - val policy = Policy.before(to) { case Decision(FormatToken(l, _, m), s) => - val nonBoolPenalty = if (isBoolOperator(l)) 0 else 5 - val penalty = nestedSelect(m.leftOwner) + nestedApplies(m.rightOwner) + - nonBoolPenalty - s.map(x => if (x.isNL) x.withPenalty(penalty) else x) - } - new Policy.Delay(policy, Policy.End < from) - } - val WithTemplateOnLeft = new ExtractFromMeta(_.leftOwner match { case lo: Stat.WithTemplate => Some(lo.templ) case _ => None diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/PolicyOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/PolicyOps.scala index 77abd83e29..4d2c7a9943 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/PolicyOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/PolicyOps.scala @@ -44,6 +44,18 @@ object PolicyOps { ) } + def penalizeNewlineByNesting(from: T, to: T)(implicit + fileLine: FileLine, + ): Policy = { + val policy = Policy.before(to) { case Decision(FormatToken(l, _, m), s) => + val nonBoolPenalty = if (TokenOps.isBoolOperator(l)) 0 else 5 + val penalty = TreeOps.nestedSelect(m.leftOwner) + + TreeOps.nestedApplies(m.rightOwner) + nonBoolPenalty + s.map(x => if (x.isNL) x.withPenalty(penalty) else x) + } + new Policy.Delay(policy, Policy.End < from) + } + /** Forces all splits up to including expire to be on a single line. * @param okSLC * if true, allow single-line comments