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: skip removed block when traversing #4521

Merged
merged 2 commits into from
Nov 9, 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 @@ -1625,8 +1625,9 @@ class FormatOps(
case _: Term.TryClause => Split.ignored
// don't tuck curried apply
case t: Term.Apply if t.fun.is[Term.Apply] => slbSplit(expire)
case EndOfFirstCall(end) if !slbOnly => slbSplit(end)
case _ => slbSplit(expire)
case t =>
val endOpt = if (slbOnly) None else getEndOfFirstCall(t)
slbSplit(endOpt.getOrElse(expire))
}
}

Expand Down
25 changes: 13 additions & 12 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -555,19 +555,20 @@ object TreeOps {
case _ => Some(false)
}.isDefined

object EndOfFirstCall {
def unapply(tree: Tree): Option[Token] = traverse(tree, None)
.map(_.tokens.last)

def getEndOfFirstCall(
tree: Tree,
)(implicit ftoks: FormatTokens): Option[Token] = {
@tailrec
private def traverse(tree: Tree, res: Option[Tree]): Option[Tree] =
tree match {
case t: Term.Select if res.isDefined => traverse(t.qual, Some(t.qual))
case t: Term.ApplyType => traverse(t.fun, Some(t))
case t: Member.Apply => traverse(t.fun, Some(t.fun))
case t: Init => traverse(t.tpe, Some(t.tpe))
case _ => res
}
def traverse(tree: Tree, res: Option[Tree]): Option[Tree] = tree match {
case t: Term.Select if res.isDefined => traverse(t.qual, Some(t.qual))
case t: Term.ApplyType => traverse(t.fun, Some(t))
case t: Member.Apply => traverse(t.fun, Some(t.fun))
case t: Init => traverse(t.tpe, Some(t.tpe))
case Term.Block(arg :: Nil) if !ftoks.isEnclosedInBraces(tree) =>
traverse(arg, res)
case _ => res
}
traverse(tree, None).map(_.tokens.last)
}

@inline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11117,3 +11117,19 @@ runtime.unsafe
} yield ()
}
.getOrThrowFiberFailure()
<<< #4133 first call in removed braces
maxColumn = 78
newlines.avoidForSimpleOverflow = all
rewrite.rules = [RedundantBraces]
===
val WatchHeartBeatInterval: FiniteDuration = {
WatchFailureDetectorConfig.getMillisDuration("heartbeat-interval")
}.requiring(_ > Duration.Zero, "watch-failure-detector.heartbeat-interval must be > 0")
>>>
val WatchHeartBeatInterval: FiniteDuration =
WatchFailureDetectorConfig
.getMillisDuration("heartbeat-interval")
.requiring(
_ > Duration.Zero,
"watch-failure-detector.heartbeat-interval must be > 0"
)
14 changes: 14 additions & 0 deletions scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -10391,3 +10391,17 @@ runtime.unsafe.run {
runtime.unsafe.run {
for { _ <- ZIO.foreachDiscard(1.to(n))(_ => ZIO.yieldNow) } yield ()
}.getOrThrowFiberFailure()
<<< #4133 first call in removed braces
maxColumn = 78
newlines.avoidForSimpleOverflow = all
rewrite.rules = [RedundantBraces]
===
val WatchHeartBeatInterval: FiniteDuration = {
WatchFailureDetectorConfig.getMillisDuration("heartbeat-interval")
}.requiring(_ > Duration.Zero, "watch-failure-detector.heartbeat-interval must be > 0")
>>>
val WatchHeartBeatInterval: FiniteDuration = WatchFailureDetectorConfig
.getMillisDuration("heartbeat-interval").requiring(
_ > Duration.Zero,
"watch-failure-detector.heartbeat-interval must be > 0"
)
15 changes: 15 additions & 0 deletions scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -10868,3 +10868,18 @@ runtime.unsafe.run {
_ <- ZIO.foreachDiscard(1.to(n))(_ => ZIO.yieldNow)
} yield ()
}.getOrThrowFiberFailure()
<<< #4133 first call in removed braces
maxColumn = 78
newlines.avoidForSimpleOverflow = all
rewrite.rules = [RedundantBraces]
===
val WatchHeartBeatInterval: FiniteDuration = {
WatchFailureDetectorConfig.getMillisDuration("heartbeat-interval")
}.requiring(_ > Duration.Zero, "watch-failure-detector.heartbeat-interval must be > 0")
>>>
val WatchHeartBeatInterval: FiniteDuration =
WatchFailureDetectorConfig.getMillisDuration("heartbeat-interval")
.requiring(
_ > Duration.Zero,
"watch-failure-detector.heartbeat-interval must be > 0"
)
Original file line number Diff line number Diff line change
Expand Up @@ -11272,3 +11272,18 @@ runtime
} yield ()
}
.getOrThrowFiberFailure()
<<< #4133 first call in removed braces
maxColumn = 78
newlines.avoidForSimpleOverflow = all
rewrite.rules = [RedundantBraces]
===
val WatchHeartBeatInterval: FiniteDuration = {
WatchFailureDetectorConfig.getMillisDuration("heartbeat-interval")
}.requiring(_ > Duration.Zero, "watch-failure-detector.heartbeat-interval must be > 0")
>>>
val WatchHeartBeatInterval: FiniteDuration = WatchFailureDetectorConfig
.getMillisDuration("heartbeat-interval")
.requiring(
_ > Duration.Zero,
"watch-failure-detector.heartbeat-interval must be > 0"
)
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, 1115673, "total explored")
assertEquals(explored, 1115896, "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