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

ScannerTokens: add RegionLine if indented further #3628

Merged
merged 2 commits into from Mar 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -911,6 +911,16 @@ final class ScannerTokens(val tokens: Tokens)(implicit dialect: Dialect) {
if (prevToken.is[Indentation]) _ => None
else getOutdentIfNeeded(_).fold(getIndentIfNeeded, x => Some(Right(x)))

def maybeWithLF(regions: List[SepRegion]) = {
val res = getIfCanProduceLF(regions, nextIndent)
if (res.isEmpty) sepRegionsOrig match {
case (ri: SepRegionIndented) :: _ if ri.indent < nextIndent =>
Some(Left(RegionLine(nextIndent) :: sepRegionsOrig))
case _ => res
}
else res
}

@tailrec
def iter(regions: List[SepRegion]): Option[Either[List[SepRegion], TokenRef]] = {
val res = regionsToRes(regions).orElse {
Expand All @@ -927,12 +937,12 @@ final class ScannerTokens(val tokens: Tokens)(implicit dialect: Dialect) {
if (next.is[Dot]) None
else
rc.asBody() match {
case Some(body) => getIfCanProduceLF(body :: rs, nextIndent)
case Some(body) => maybeWithLF(body :: rs)
case None => iter(rs)
}
case _ => iter(rs)
}
case rs => getIfCanProduceLF(rs, nextIndent)
case rs => maybeWithLF(rs)
}
else res
}
Expand Down
Expand Up @@ -653,4 +653,107 @@ class InfixSuite extends BaseDottySuite {
runTestAssert[Stat](code, Some(layout))(tree)
}

test("scalafmt #3825 1") {
val code =
"""|object a:
| foo
| .map: i =>
| i + 1
| *> bar
|""".stripMargin
val layout =
"""|object a {
| foo.map {
| i => i + 1
| } *> bar
|}
|""".stripMargin
val tree = Defn.Object(
Nil,
tname("a"),
tpl(
Term.ApplyInfix(
Term.Apply(
Term.Select(tname("foo"), tname("map")),
blk(
Term.Function(
List(tparam("i")),
Term.ApplyInfix(tname("i"), tname("+"), Nil, List(int(1)))
)
) :: Nil
),
tname("*>"),
Nil,
List(tname("bar"))
)
)
)
runTestAssert[Stat](code, layout)(tree)
}

test("scalafmt #3825 2") {
val code =
"""|object a:
| object b:
| foo
| .map: i =>
| i + 1
| + 2
| + 3
| *> bar
| baz
| qux
|""".stripMargin
val layout =
"""|object a {
| object b {
| foo.map {
| i => i + 1 + 2 + 3
| } *> bar
| baz
| }
| qux
|}
|""".stripMargin
val tree = Defn.Object(
Nil,
tname("a"),
tpl(
Defn.Object(
Nil,
tname("b"),
tpl(
Term.ApplyInfix(
Term.Apply(
Term.Select(tname("foo"), tname("map")),
blk(
Term.Function(
List(tparam("i")),
Term.ApplyInfix(
Term.ApplyInfix(
Term.ApplyInfix(tname("i"), tname("+"), Nil, List(int(1))),
tname("+"),
Nil,
List(int(2))
),
tname("+"),
Nil,
List(int(3))
)
)
) :: Nil
),
tname("*>"),
Nil,
List(tname("bar"))
),
tname("baz")
)
),
tname("qux")
)
)
runTestAssert[Stat](code, layout)(tree)
}

}