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

Router: fold closing paren if no NL after opening #4513

Merged
merged 3 commits into from
Nov 7, 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 @@ -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 @@ -937,9 +936,15 @@ class Router(formatOps: FormatOps) {
).left

val spacePolicy = SingleLineBlock(lambdaToken) ==> {
Policy ? lambdaIsABlock || delayedBreakPolicy(
Policy.End == lambdaLeft.getOrElse(close),
)(newlinePolicy)
def before = Policy.End < close ==> Policy.on(close, "NODANGLE") {
case Decision(FormatToken(bc, `close`, _), _) =>
val isSpace = bc.is[T.Comment] || style.spaces.inParentheses
Seq(Split(Space(isSpace), 0))
}
Policy ? lambdaIsABlock ||
Policy.RelayOnSplit.by(Policy.End == lambdaLeft.getOrElse(close))(
(s, _) => s.isNL,
)(before)(newlinePolicy)
}
Split(noSplitMod, 0, policy = spacePolicy)
.withOptimalToken(lambdaToken, killOnFail = true)
Expand Down Expand Up @@ -2390,7 +2395,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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class CommunityScala2Suite(name: String)

class CommunityScala2_12Suite extends CommunityScala2Suite("scala-2.12") {

override protected def totalStatesVisited: Option[Int] = Some(34661412)
override protected def totalStatesVisited: Option[Int] = Some(34661420)

override protected def builds =
Seq(getBuild("v2.12.20", dialects.Scala212, 1277))
Expand All @@ -18,7 +18,7 @@ class CommunityScala2_12Suite extends CommunityScala2Suite("scala-2.12") {

class CommunityScala2_13Suite extends CommunityScala2Suite("scala-2.13") {

override protected def totalStatesVisited: Option[Int] = Some(43185118)
override protected def totalStatesVisited: Option[Int] = Some(43185150)

override protected def builds =
Seq(getBuild("v2.13.14", dialects.Scala213, 1287))
Expand Down
92 changes: 92 additions & 0 deletions scalafmt-tests/shared/src/test/resources/rewrite/AvoidInfix.stat
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,95 @@ def primaryConstructor: Option[MemberImpl with Constructor] = if (isClass) const
>>>
def primaryConstructor: Option[MemberImpl with Constructor] =
if (isClass) constructors.find(_.isPrimary) else None
<<< #4133 infix, with comment before closing paren, and postfix; overflow punct
maxColumn = 78
newlines.source = fold
newlines.avoidForSimpleOverflow = [punct]
===
def templates = symbols filter (x => x.isClass || x.isTrait || x == AnyRefClass/* which is now a type alias */) toSet
>>>
def templates = symbols.filter(x =>
x.isClass || x.isTrait || x == AnyRefClass /* which is now a type alias */
) toSet
<<< #4133 infix, with comment before closing paren, and postfix; overflow slc
maxColumn = 78
newlines.source = fold
newlines.avoidForSimpleOverflow = [slc]
===
def templates = symbols filter (x => x.isClass || x.isTrait || x == AnyRefClass/* which is now a type alias */) toSet
>>>
def templates = symbols
.filter(x =>
x.isClass || x.isTrait || x == AnyRefClass /* which is now a type alias */
) toSet
<<< #4133 infix, with comment before closing paren, and postfix; overflow all
maxColumn = 78
newlines.source = fold
newlines.avoidForSimpleOverflow = all
===
def templates = symbols filter (x => x.isClass || x.isTrait || x == AnyRefClass/* which is now a type alias */) toSet
>>>
def templates = symbols
.filter(x =>
x.isClass || x.isTrait || x == AnyRefClass /* which is now a type alias */
) toSet
<<< #4133 select, apply with comment before closing paren, and postfix; overflow punct
maxColumn = 78
newlines.source = fold
newlines.avoidForSimpleOverflow = [punct]
===
def templates = symbols.filter (x => x.isClass || x.isTrait || x == AnyRefClass/* which is now a type alias */) toSet
>>>
def templates = symbols.filter(x =>
x.isClass || x.isTrait || x == AnyRefClass /* which is now a type alias */
) toSet
<<< #4133 select, apply with comment before closing paren, and postfix; overflow slc
maxColumn = 78
newlines.source = fold
newlines.avoidForSimpleOverflow = [slc]
===
def templates = symbols.filter (x => x.isClass || x.isTrait || x == AnyRefClass/* which is now a type alias */) toSet
>>>
def templates = symbols
.filter(x =>
x.isClass || x.isTrait || x == AnyRefClass /* which is now a type alias */
) toSet
<<< #4133 select, apply with comment before closing paren, and postfix; overflow all
maxColumn = 78
newlines.source = fold
newlines.avoidForSimpleOverflow = all
===
def templates = symbols.filter (x => x.isClass || x.isTrait || x == AnyRefClass/* which is now a type alias */) toSet
>>>
def templates = symbols
.filter(x =>
x.isClass || x.isTrait || x == AnyRefClass /* which is now a type alias */
) toSet
<<< #4133 select, apply with comment before closing paren, and select; overflow punct
maxColumn = 78
newlines.source = fold
newlines.avoidForSimpleOverflow = [punct]
===
def templates = symbols.filter (x => x.isClass || x.isTrait || x == AnyRefClass/* which is now a type alias */).toSet
>>>
def templates = symbols.filter(x =>
x.isClass || x.isTrait || x == AnyRefClass /* which is now a type alias */
).toSet
<<< #4133 select, apply with comment before closing paren, and select; overflow slc
maxColumn = 78
newlines.source = fold
newlines.avoidForSimpleOverflow = [slc]
===
def templates = symbols.filter (x => x.isClass || x.isTrait || x == AnyRefClass/* which is now a type alias */).toSet
>>>
def templates = symbols.filter(x => x.isClass || x.isTrait || x == AnyRefClass /* which is now a type alias */ )
.toSet
<<< #4133 select, apply with comment before closing paren, and select; overflow all
maxColumn = 78
newlines.source = fold
newlines.avoidForSimpleOverflow = all
===
def templates = symbols.filter (x => x.isClass || x.isTrait || x == AnyRefClass/* which is now a type alias */).toSet
>>>
def templates = symbols.filter(x => x.isClass || x.isTrait || x == AnyRefClass /* which is now a type alias */ )
.toSet
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
val explored = Debug.explored.get()
logger.debug(s"Total explored: $explored")
if (!onlyUnit && !onlyManual)
assertEquals(explored, 1499037, "total explored")
assertEquals(explored, 1501221, "total explored")
val results = debugResults.result()
// TODO(olafur) don't block printing out test results.
// I don't want to deal with scalaz's Tasks :'(
Expand Down
Loading