Skip to content

Commit

Permalink
Merge pull request #3247 from kitbellew/483_1
Browse files Browse the repository at this point in the history
ScannerTokens: handle `case` after `= <indent>`
  • Loading branch information
kitbellew committed Jul 12, 2023
2 parents 765e002 + c6a1f8f commit 28d7f73
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ final class ScannerTokens(val tokens: Tokens)(implicit dialect: Dialect) {
case x :: RegionCaseMark :: xs => expr() :: x :: xs
case (_: RegionBrace) :: (_: RegionFor | RegionTemplateBody) :: _ => sepRegions
case (_: RegionBrace) :: _ => expr() :: sepRegions
case (_: RegionIndent) :: _ if prev.is[Equals] && prevToken.is[Indentation.Indent] =>
expr() :: sepRegions
// `case` is at top-level (likely quasiquote)
case Nil if prevPos == 0 => expr() :: Nil
case xs => xs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2665,4 +2665,89 @@ class SignificantIndentationSuite extends BaseDottySuite {
runTestAssert[Stat](code, assertLayout = Some(layout))(tree)
}

test("def body is partial function") {
val code =
"""|object a:
| def foo: Bar =
| case sym
| if baz =>
| end foo
|""".stripMargin
val layout =
"""|object a {
| def foo: Bar = {
| case sym if baz =>
| }
| end foo
|}
|""".stripMargin
runTestAssert[Stat](code, Some(layout)) {
Defn.Object(
Nil,
tname("a"),
tpl(
List(
Defn.Def(
Nil,
tname("foo"),
Nil,
Some(pname("Bar")),
Term.PartialFunction(
List(Case(Pat.Var(tname("sym")), Some(tname("baz")), Term.Block(Nil)))
)
),
Term.EndMarker(tname("foo"))
)
)
)
}
}

test("def body is non-partial function") {
val code =
"""|object a:
| def foo: Bar =
| case class Baz(baz: Int)
| new Baz(0)
|""".stripMargin
val layout =
"""|object a {
| def foo: Bar = {
| case class Baz(baz: Int)
| new Baz(0)
| }
|}
|""".stripMargin
runTestAssert[Stat](code, Some(layout)) {
Defn.Object(
Nil,
tname("a"),
tpl(
Defn.Def(
Nil,
tname("foo"),
Nil,
Some(pname("Bar")),
Term.Block(
List(
Defn.Class(
List(Mod.Case()),
pname("Baz"),
Nil,
Ctor.Primary(
Nil,
anon,
List(List(Term.Param(Nil, tname("baz"), Some(pname("Int")), None)))
),
tpl(Nil)
),
Term.New(Init(pname("Baz"), anon, List(List(int(0)))))
)
)
) :: Nil
)
)
}
}

}

0 comments on commit 28d7f73

Please sign in to comment.