Skip to content

Commit

Permalink
ScannerTokens: add RegionLine if indented further
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Mar 9, 2024
1 parent 8a51cbc commit e786544
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
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 @@ -664,27 +664,27 @@ class InfixSuite extends BaseDottySuite {
val layout =
"""|object a {
| foo.map {
| i => i + 1 *> bar
| }
| i => i + 1
| } *> bar
|}
|""".stripMargin
val tree = Defn.Object(
Nil,
tname("a"),
tpl(
Term.Apply(
Term.Select(tname("foo"), tname("map")),
blk(
Term.Function(
List(tparam("i")),
Term.ApplyInfix(
tname("i"),
tname("+"),
Nil,
List(Term.ApplyInfix(int(1), tname("*>"), Nil, List(tname("bar"))))
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
) :: Nil
),
tname("*>"),
Nil,
List(tname("bar"))
)
)
)
Expand All @@ -704,32 +704,37 @@ class InfixSuite extends BaseDottySuite {
val layout =
"""|object a {
| foo.map {
| i => i + 1 + 2 + 3 *> bar
| }
| i => i + 1 + 2 + 3
| } *> bar
|}
|""".stripMargin
val tree = Defn.Object(
Nil,
tname("a"),
tpl(
Term.Apply(
Term.Select(tname("foo"), tname("map")),
blk(
Term.Function(
List(tparam("i")),
Term.ApplyInfix(
Term.ApplyInfix(
Term.Apply(
Term.Select(tname("foo"), tname("map")),
blk(
Term.Function(
List(tparam("i")),
Term.ApplyInfix(
Term.ApplyInfix(tname("i"), tname("+"), Nil, List(int(1))),
Term.ApplyInfix(
Term.ApplyInfix(tname("i"), tname("+"), Nil, List(int(1))),
tname("+"),
Nil,
List(int(2))
),
tname("+"),
Nil,
List(int(2))
),
tname("+"),
Nil,
List(Term.ApplyInfix(int(3), tname("*>"), Nil, List(tname("bar"))))
List(int(3))
)
)
)
) :: Nil
) :: Nil
),
tname("*>"),
Nil,
List(tname("bar"))
)
)
)
Expand Down

0 comments on commit e786544

Please sign in to comment.