Skip to content

Commit

Permalink
Policy: allow ignoring after in RelayOnSplit
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 7, 2024
1 parent 101e9c8 commit 996a2c9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2920,11 +2920,11 @@ class FormatOps(
def policyWithDelay(policy: Policy) = {
val beforeDelimsEnd = beforeDelims.right.end
// force break if multiline and if there's no other break
delayedBreakPolicy(Policy.End == beforeDelims.right, exclude)(
delayedBreakPolicy(Policy.End == beforeDelims.right, exclude) {
Policy.RelayOnSplit { case (s, nextft) =>
s.isNL && nextft.right.end > beforeDelimsEnd // don't need anymore
}(policy, NoPolicy),
)
}(policy)(NoPolicy)
}
}

(afterDelims.right match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,22 +287,25 @@ object Policy {

class RelayOnSplit(
before: Policy,
pred: (Split, FormatToken) => Boolean,
trigger: (Split, FormatToken) => Boolean,
triggerEnd: Policy.End.WithPos,
after: Policy,
)(implicit fileLine: FileLine)
extends WithConv {
override def f: Pf = before.f
override def rank: Int = before.rank
override def unexpired(split: Split, nextft: FormatToken): Policy =
if (pred(split, nextft)) after.unexpired(split, nextft)
if (trigger(split, nextft)) after.unexpired(split, nextft)
else if (!triggerEnd.notExpiredBy(nextft)) NoPolicy
else super.unexpired(split, nextft)

override def noDequeue: Boolean = before.noDequeue
override def toString: String = s"REL?:[$fileLine]($before ??? $after)"

protected def conv(func: Policy => Policy): Policy = {
val filtered = func(before)
if (filtered eq before) this else new RelayOnSplit(filtered, pred, after)
if (filtered eq before) this
else new RelayOnSplit(filtered, trigger, triggerEnd, after)
}

override def appliesUntil(nextft: FormatToken)(
Expand All @@ -315,10 +318,15 @@ object Policy {
}

object RelayOnSplit {
def by(triggerEnd: Policy.End.WithPos)(
trigger: (Split, FormatToken) => Boolean,
)(before: Policy)(after: Policy)(implicit fileLine: FileLine): Policy =
if (before.isEmpty) after
else new RelayOnSplit(before, trigger, triggerEnd, after)
def apply(
pred: (Split, FormatToken) => Boolean,
)(before: Policy, after: Policy)(implicit fileLine: FileLine): Policy =
if (before.isEmpty) after else new RelayOnSplit(before, pred, after)
trigger: (Split, FormatToken) => Boolean,
)(before: Policy)(after: Policy)(implicit fileLine: FileLine): Policy =
by(Policy.End.Never)(trigger)(before)(after)
}

class Switch(before: Policy, trigger: Token, after: Policy)(implicit
Expand Down Expand Up @@ -449,6 +457,9 @@ object Policy {
override def toString: String = s"@${token.structure}"
}
}
case object Never extends WithPos {
override def notExpiredBy(ft: FormatToken): Boolean = true
}
}

implicit def implicitOptionPolicyToPolicy(obj: Option[Policy]): Policy = obj
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,7 @@ class Router(formatOps: FormatOps) {
val exclude = slbParensExclude.getOrElse(TokenRanges.empty)
val slbPolicy =
if (exclude.isEmpty) slbParensPolicy
else Policy.RelayOnSplit { case (s, _) => s.isNL }(
slbParensPolicy,
else Policy.RelayOnSplit((s, _) => s.isNL)(slbParensPolicy)(
Policy.on(close, "BracesToParensFailed") { case _ => Nil },
)
Split(slbMod, 0).withSingleLine(
Expand Down Expand Up @@ -2390,7 +2389,7 @@ class Router(formatOps: FormatOps) {
)
}
}
Policy.RelayOnSplit((s, _) => s.isNL)(onArrowPolicy, postArrowPolicy)
Policy.RelayOnSplit((s, _) => s.isNL)(onArrowPolicy)(postArrowPolicy)
}
Seq(Split(mod, 0, policy = policy))

Expand Down

0 comments on commit 996a2c9

Please sign in to comment.