Skip to content

Commit 7ba35b6

Browse files
committed
Add semantic-db project
1 parent d90e8c3 commit 7ba35b6

File tree

9 files changed

+113
-6
lines changed

9 files changed

+113
-6
lines changed

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ val `dotty-sbt-bridge-bootstrapped` = Build.`dotty-sbt-bridge-bootstrapped`
1212
val `dotty-language-server` = Build.`dotty-language-server`
1313
val `dotty-bench` = Build.`dotty-bench`
1414
val `dotty-bench-bootstrapped` = Build.`dotty-bench-bootstrapped`
15+
val `dotty-semantic-db` = Build.`dotty-semantic-db`
1516
val `scala-library` = Build.`scala-library`
1617
val `scala-compiler` = Build.`scala-compiler`
1718
val `scala-reflect` = Build.`scala-reflect`

compiler/src/dotty/tools/dotc/consumetasty/ConsumeTasty.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dotty.tools.dotc.consumetasty
22

33
import dotty.tools.dotc
44
import dotty.tools.dotc.core.Contexts._
5+
import dotty.tools.dotc.quoted.QuoteDriver
56

67
import scala.tasty.file.TastyConsumer
78

@@ -15,7 +16,7 @@ object ConsumeTasty {
1516
new TastyFromClass(tastyConsumer)
1617
}
1718

18-
val currentClasspath = System.getProperty("java.class.path")
19+
val currentClasspath = QuoteDriver.currentClasspath
1920
val args = "-from-tasty" +: "-classpath" +: s"$classpath:$currentClasspath" +: classes
2021
(new Consume).process(args.toArray)
2122
}

compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,24 @@ class QuoteDriver extends Driver {
7575

7676
override def initCtx: Context = {
7777
val ictx = contextBase.initialCtx
78-
var classpath = System.getProperty("java.class.path")
78+
ictx.settings.classpath.update(QuoteDriver.currentClasspath)(ictx)
79+
ictx
80+
}
81+
82+
}
83+
84+
object QuoteDriver {
85+
86+
def currentClasspath: String = {
87+
val classpath0 = System.getProperty("java.class.path")
7988
this.getClass.getClassLoader match {
8089
case cl: URLClassLoader =>
8190
// Loads the classes loaded by this class loader
8291
// When executing `run` or `test` in sbt the classpath is not in the property java.class.path
8392
val newClasspath = cl.getURLs.map(_.getFile())
84-
classpath = newClasspath.mkString("", java.io.File.pathSeparator, if (classpath == "") "" else java.io.File.pathSeparator + classpath)
85-
case _ =>
93+
newClasspath.mkString("", java.io.File.pathSeparator, if (classpath0 == "") "" else java.io.File.pathSeparator + classpath0)
94+
case _ => classpath0
8695
}
87-
ictx.settings.classpath.update(classpath)(ictx)
88-
ictx
8996
}
9097

9198
}

project/Build.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,12 @@ object Build {
381381
dottyLib + File.pathSeparator + dottyInterfaces + File.pathSeparator + otherDeps
382382
}
383383

384+
lazy val semanticDBSettings = Seq(
385+
baseDirectory in (Compile, run) := baseDirectory.value / "..",
386+
baseDirectory in Test := baseDirectory.value / "..",
387+
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test
388+
)
389+
384390
// Settings shared between dotty-doc and dotty-doc-bootstrapped
385391
lazy val dottyDocSettings = Seq(
386392
baseDirectory in (Compile, run) := baseDirectory.value / "..",
@@ -904,6 +910,8 @@ object Build {
904910
lazy val `dotty-bench` = project.in(file("bench")).asDottyBench(NonBootstrapped)
905911
lazy val `dotty-bench-bootstrapped` = project.in(file("bench")).asDottyBench(Bootstrapped)
906912

913+
lazy val `dotty-semantic-db` = project.in(file("semantic-db")).asDottySemanticDB(Bootstrapped)
914+
907915
// Depend on dotty-library so that sbt projects using dotty automatically
908916
// depend on the dotty-library
909917
lazy val `scala-library` = project.
@@ -1297,6 +1305,10 @@ object Build {
12971305
settings(commonBenchmarkSettings).
12981306
enablePlugins(JmhPlugin)
12991307

1308+
def asDottySemanticDB(implicit mode: Mode): Project = project.withCommonSettings.
1309+
dependsOn(dottyCompiler).
1310+
settings(semanticDBSettings)
1311+
13001312
def asDist(implicit mode: Mode): Project = project.
13011313
enablePlugins(PackPlugin).
13021314
withCommonSettings.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dotty.semanticdb
2+
3+
import scala.tasty.Tasty
4+
import scala.tasty.file.TastyConsumer
5+
import scala.tasty.util.TreeTraverser
6+
7+
class DBConsumer extends TastyConsumer {
8+
9+
final def apply(tasty: Tasty)(root: tasty.Tree): Unit = {
10+
import tasty._
11+
object Traverser extends TreeTraverser[tasty.type](tasty) {
12+
13+
override def traverseTree(tree: Tree)(implicit ctx: Context): Unit = tree match {
14+
case IsDefinition(tree) =>
15+
println(tree.name)
16+
super.traverseTree(tree)
17+
case tree =>
18+
super.traverseTree(tree)
19+
}
20+
21+
}
22+
Traverser.traverseTree(root)(tasty.rootContext)
23+
}
24+
25+
def println(x: Any): Unit = Predef.println(x)
26+
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dotty.semanticdb
2+
3+
import scala.tasty.Tasty
4+
import scala.tasty.util.TreeTraverser
5+
import scala.tasty.file._
6+
7+
object Main {
8+
def main(args: Array[String]): Unit = {
9+
val extraClasspath = "." // TODO allow to set it from the args with -classpath XYZ
10+
val classes = args.toList
11+
if (args.isEmpty) {
12+
println("Dotty Semantic DB: No classes where passed as argument")
13+
} else {
14+
println("Running Dotty Semantic DB on: " + args.mkString(" "))
15+
ConsumeTasty(extraClasspath, classes, new DBConsumer)
16+
}
17+
}
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dotty.semanticdb
2+
3+
import scala.tasty.Tasty
4+
import scala.tasty.util.TreeTraverser
5+
import scala.tasty.file._
6+
7+
import org.junit.Test
8+
import org.junit.Assert._
9+
10+
class Tests {
11+
12+
// TODO: update scala-0.10 on version change (or resolve automatically)
13+
final def testClasspath = "out/bootstrap/dotty-semantic-db/scala-0.10/test-classes"
14+
15+
@Test def testMain(): Unit = {
16+
testOutput(
17+
"tests.SimpleClass",
18+
"SimpleClass;<init>;"
19+
)
20+
testOutput(
21+
"tests.SimpleDef",
22+
"SimpleDef;<init>;foo;"
23+
)
24+
}
25+
26+
def testOutput(className: String, expected: String): Unit = {
27+
val out = new StringBuilder
28+
ConsumeTasty(testClasspath, List(className), new DBConsumer {
29+
override def println(x: Any): Unit = out.append(x).append(";")
30+
})
31+
assertEquals(expected, out.result())
32+
}
33+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package tests
2+
3+
class SimpleClass
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package tests
2+
3+
class SimpleDef {
4+
def foo(): Int = 0
5+
}

0 commit comments

Comments
 (0)