Skip to content

Commit 3edd1ce

Browse files
committed
Support Scala 3 dialect based on source path
1 parent e6f83e3 commit 3edd1ce

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
case class Scala3Class(a: Int)(using b: String)

src/FileParser.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import scala.meta._
2+
import scala.meta.Dialect
23

34
case class ScalaFile(
45
imports: List[String],
@@ -44,9 +45,8 @@ object FileParser {
4445
}
4546
}
4647

47-
def parse(content: String): ScalaFile = {
48+
def parse(content: String, dialect: Dialect = dialects.Scala213Source3): ScalaFile = {
4849
val input = Input.String(content)
49-
val dialect = dialects.Scala213Source3
5050
val exampleTree: Source = dialect(input).parse[Source].get
5151

5252
val tree =
@@ -78,6 +78,10 @@ object FileParser {
7878
val path = java.nio.file.Paths.get(filePath)
7979
val bytes = java.nio.file.Files.readAllBytes(path)
8080
val text = new String(bytes, "UTF-8")
81-
parse(text)
81+
val dialect =
82+
if (filePath.split(java.io.File.separator).contains("scala-3"))
83+
dialects.Scala3
84+
else dialects.Scala213Source3
85+
parse(text, dialect)
8286
}
8387
}

test/FileParser.test.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,13 @@ class FileParserTest extends munit.FunSuite {
6363
val parsedFile = FileParser.fromPathToClassDef(file)
6464
println(parsedFile)
6565
}
66+
test("uses Scala3 dialect for sources in scala-3 directory") {
67+
val file = Thread
68+
.currentThread()
69+
.getContextClassLoader
70+
.getResource("scala-3/Scala3Class.scala_test")
71+
.getPath
72+
val parsedFile = FileParser.fromPathToClassDef(file)
73+
assert(parsedFile.classes.head.name == "Scala3Class")
74+
}
6675
}

0 commit comments

Comments
 (0)