Skip to content

Commit

Permalink
bugfix: emit cases for enum
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Jun 26, 2023
1 parent 6e5167d commit 14b56d9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ class ScalaToplevelMtags(
}
loop(indent, isAfterNewline = false, currRegion, newExpectIgnoreBody)
case IMPORT =>
// skip imports becase they might have `given` kw
// skip imports because they might have `given` kw
acceptToStatSep()
loop(indent, isAfterNewline = false, currRegion, expectTemplate)
case COMMENT =>
// skip comment becase they might break indentation
// skip comment because they might break indentation
scanner.nextToken()
loop(indent, isAfterNewline = false, currRegion, expectTemplate)
case WHITESPACE if dialect.allowSignificantIndentation =>
Expand Down Expand Up @@ -295,6 +295,14 @@ class ScalaToplevelMtags(
expectTemplate
)
}
case MATCH if dialect.allowSignificantIndentation && nextIsNL =>
val nextIndent = acceptWhileIndented(indent)
loop(
nextIndent,
isAfterNewline = false,
currRegion,
None
)
case COLON if dialect.allowSignificantIndentation =>
(expectTemplate, nextIsNL) match {
case (Some(expect), true) if needToParseBody(expect) =>
Expand All @@ -310,6 +318,14 @@ class ScalaToplevelMtags(
currRegion,
None
)
case (_, true) =>
val nextIndent = acceptWhileIndented(indent)
loop(
nextIndent,
isAfterNewline = false,
currRegion,
expectTemplate
)
case _ =>
scanner.nextToken()
loop(
Expand Down Expand Up @@ -404,6 +420,7 @@ class ScalaToplevelMtags(
case CASE =>
val nextExpectTemplate = expectTemplate.filter(!_.isPackageBody)
acceptTrivia()
emitEnumCases(region)
loop(
indent,
isAfterNewline = false,
Expand Down Expand Up @@ -544,6 +561,30 @@ class ScalaToplevelMtags(

}

@tailrec
private def emitEnumCases(region: Region): Unit = {
scanner.curr.token match {
case IDENTIFIER =>
val pos = newPosition
val name = scanner.curr.name
term(
name,
pos,
Kind.METHOD,
SymbolInformation.Property.VAL.value
)
resetRegion(region)
acceptTrivia()
scanner.curr.token match {
case COMMA =>
acceptTrivia()
emitEnumCases(region)
case _ =>
}
case _ =>
}
}

/**
* Consumes the token stream until the matching closing delimiter
*/
Expand Down
48 changes: 47 additions & 1 deletion tests/unit/src/test/scala/tests/ScalaToplevelSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class ScalaToplevelSuite extends BaseSuite {
List(
"_empty_/A.", "_empty_/A.foo().", "_empty_/A.Z#", "_empty_/B#",
"_empty_/B#X#", "_empty_/B#foo().", "_empty_/B#v.", "_empty_/C#",
"_empty_/C#i.", "_empty_/D#", "_empty_/D#getI().", "_empty_/D#i.",
"_empty_/C#i.", "_empty_/D#", "_empty_/D#Da.", "_empty_/D#Db.",
"_empty_/D#getI().", "_empty_/D#i.",
),
all = true,
dialect = dialects.Scala3,
Expand Down Expand Up @@ -98,6 +99,7 @@ class ScalaToplevelSuite extends BaseSuite {
List(
"_empty_/A.", "_empty_/A.foo().", "_empty_/A.Z#", "_empty_/B#",
"_empty_/B#X#", "_empty_/B#foo().", "_empty_/C#", "_empty_/D#",
"_empty_/D#Da.", "_empty_/D#Db.",
),
all = true,
)
Expand Down Expand Up @@ -455,6 +457,50 @@ class ScalaToplevelSuite extends BaseSuite {
all = true,
)

check(
"cases-for-enum-broken-ident",
"""|package a
|enum Planets(val num: Int){
|num match
| case x => ???
|List(1, 2, 3).collect{case someNumber => ???}
|case Mercury extends Planets(1)
|case Venus extends Planets(2)
|case Earth extends Planets(3)
|}
|
|enum NotPlanets{ case Vase }
|""".stripMargin,
List("a/", "a/Planets#", "a/Planets#Earth.", "a/Planets#Mercury.",
"a/Planets#num.", "a/Planets#Venus.", "a/NotPlanets#",
"a/NotPlanets#Vase."),
dialect = dialects.Scala3,
all = true,
)

check(
"cases-for-enum-ident",
"""|package a
|enum Planets(val num: Int):
| num match
| case 1 =>
| case other =>
| List(1, 2, 3).collect:
| case someNumber =>
| case Mercury extends Planets(1)
| case Venus extends Planets(2)
| case Earth extends Planets(3)
|
|enum NotPlanets:
| case Vase
|""".stripMargin,
List("a/", "a/Planets#", "a/Planets#Earth.", "a/Planets#Mercury.",
"a/Planets#num.", "a/Planets#Venus.", "a/NotPlanets#",
"a/NotPlanets#Vase."),
dialect = dialects.Scala3,
all = true,
)

def check(
options: TestOptions,
code: String,
Expand Down

0 comments on commit 14b56d9

Please sign in to comment.