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

Splits: ensure break between yield and case #4858

Merged
merged 3 commits into from
Mar 4, 2025
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 @@ -3377,25 +3377,31 @@ object SplitsAfterYield extends Splits {
cfg: ScalafmtConfig,
): Seq[Split] = {
import fo._, tokens._, ft._
leftOwner match {
case t: Term.ForYield =>
val lastToken = getLast(t.body)
if (
t.body.is[Tree.Block] && right.is[T.LeftBrace] &&
matchingOptRight(ft).exists(_.idx >= lastToken.idx)
) Seq(Split(Space, 0))
else {
val indent = Indent(cfg.indent.main, lastToken, ExpiresOn.After)
if (cfg.newlines.avoidAfterYield && !rightOwner.is[Term.If]) {
val noIndent = !isRightCommentWithBreak(ft)
Seq(Split(Space, 0).withIndent(indent, noIndent))
} else Seq(
// Either everything fits in one line or break on =>
Split(Space, 0).withSingleLine(lastToken),
Split(Newline, 1).withIndent(indent),
)
}
case _ => Seq.empty
(leftOwner match {
case t: Term.ForYield => Some(t.body)
case _ => None
}).fold(Seq.empty[Split]) {
case b: Term.PartialFunction
if dialect.allowSignificantIndentation &&
nextNonComment(ft).right.is[T.KwCase] =>
val split = Split(Newline, 0)
.withIndent(cfg.indent.getSignificant, getLast(b), ExpiresOn.After)
Seq(split)
case b: Tree.Block
if right.is[T.LeftBrace] &&
matchingOptRight(ft).exists(_.idx >= getLast(b).idx) =>
Seq(Split(Space, 0))
case b =>
val lastToken = getLast(b)
val indent = Indent(cfg.indent.main, lastToken, ExpiresOn.After)
if (cfg.newlines.avoidAfterYield && !rightOwner.is[Term.If]) {
val noIndent = !isRightCommentWithBreak(ft)
Seq(Split(Space, 0).withIndent(indent, noIndent))
} else Seq(
// Either everything fits in one line or break on =>
Split(Space, 0).withSingleLine(lastToken),
Split(Newline, 1).withIndent(indent),
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8527,3 +8527,12 @@ object a {
x = "string"
)
}
<<< #4857
for
foo <- Some(42)
yield
case Some(x) => foo
>>>
for foo <- Some(42)
yield
case Some(x) => foo
Original file line number Diff line number Diff line change
Expand Up @@ -8190,3 +8190,12 @@ object a {
x = "string"
)
}
<<< #4857
for
foo <- Some(42)
yield
case Some(x) => foo
>>>
for foo <- Some(42)
yield
case Some(x) => foo
Original file line number Diff line number Diff line change
Expand Up @@ -8549,3 +8549,13 @@ object a {
x = "string"
)
}
<<< #4857
for
foo <- Some(42)
yield
case Some(x) => foo
>>>
for
foo <- Some(42)
yield
case Some(x) => foo
Original file line number Diff line number Diff line change
Expand Up @@ -8877,3 +8877,13 @@ object a {
x = "string"
)
}
<<< #4857
for
foo <- Some(42)
yield
case Some(x) => foo
>>>
for foo <- Some(42)
yield
case Some(x) =>
foo
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,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, 2537604, "total explored")
assertEquals(explored, 2537916, "total explored")
// TODO(olafur) don't block printing out test results.
TestPlatformCompat.executeAndWait(PlatformFileOps.writeFile(
FileOps.getPath("target", "index.html"),
Expand Down