@@ -14,133 +14,40 @@ import scala.tasty.Reflection
14
14
import scala .tasty .file .TastyConsumer
15
15
import java .lang .reflect .InvocationTargetException
16
16
17
- class TastyInspecter extends TastyConsumer {
18
- var source_path : Option [String ] = None
19
- final def apply (reflect : Reflection )(root : reflect.Tree ): Unit = {
20
- import reflect ._
21
- object ChildTraverser extends TreeTraverser {
22
- override def traverseTree (tree : Tree )(implicit ctx : Context ): Unit =
23
- tree match {
24
- case IsClassDef (cdef) => {
25
- cdef.symbol.annots.foreach { annot =>
26
- annot match {
27
- case Term .Apply (Term .Select (Term .New (t), _),
28
- List (Term .Literal (Constant .String (path))))
29
- if t.symbol.name == " SourceFile" =>
30
- // we found the path to a file. In this case, we do not need to
31
- // continue traversing the tree
32
- source_path = Some (path)
33
- case x => super .traverseTree(tree)
34
- }
35
- true
36
- }
37
- }
38
- case _ => {
39
- if (source_path == None )
40
- super .traverseTree(tree)
41
- else
42
- ()
43
- }
44
- }
45
- }
46
- ChildTraverser .traverseTree(root)(reflect.rootContext)
47
- }
48
- }
49
-
50
17
class Tests {
51
18
52
19
// TODO: update scala-0.13 on version change (or resolve automatically)
53
20
final def tastyClassDirectory =
54
- " out/bootstrap/dotty-semanticdb/scala-0.12/test-classes"
55
- val sourceroot = Paths .get(" semanticdb" , " input" ).toAbsolutePath
56
- val sourceDirectory = sourceroot.resolve(" src/main/scala" )
21
+ Paths .get(" out/bootstrap/dotty-semanticdb/scala-0.12/test-classes/" )
57
22
23
+ val sourceroot = Paths .get(" semanticdb/input" ).toAbsolutePath
24
+ val sourceDirectory = sourceroot.resolve(" src/main/scala" )
58
25
val semanticdbClassDirectory = sourceroot.resolve(" target/scala-2.12/classes" )
59
26
val semanticdbLoader =
60
27
new Semanticdbs .Loader (sourceroot, List (semanticdbClassDirectory))
61
28
62
- val source_to_tasty =
63
- getTastyFiles(
64
- Paths .get(" out" , " bootstrap" , " dotty-semanticdb/" ).toAbsolutePath)
65
-
66
29
/** Returns the SemanticDB for this Scala source file. */
67
30
def getScalacSemanticdb (scalaFile : Path ): s.TextDocument = {
68
31
semanticdbLoader.resolve(scalaFile).get
69
32
}
70
33
71
- /** List all tasty files occuring in the folder f or one of its subfolders */
72
- def recursiveListFiles (f : File ): Array [File ] = {
73
- val pattern = " .*test-classes/example.*\\ .tasty" .r
74
- val files = f.listFiles
75
- val folders = files.filter(_.isDirectory)
76
- val tastyfiles = files.filter(_.toString match {
77
- case pattern(x : _* ) => true
78
- case _ => false
79
- })
80
- tastyfiles ++ folders.flatMap(recursiveListFiles)
81
- }
82
-
83
- /** Returns a mapping from *.scala file to a list of tasty files. */
84
- def getTastyFiles (artifactsPath : Path ): HashMap [String , List [Path ]] = {
85
- val source_to_tasty : HashMap [String , List [Path ]] = HashMap ()
86
- val tastyfiles = recursiveListFiles(artifactsPath.toFile())
87
- recursiveListFiles(artifactsPath.toFile()).map(tasty_path => {
88
- val (classpath, classname) = getClasspathClassname(tasty_path.toPath())
89
- // We add an exception here to avoid crashing if we encountered
90
- // a bad tasty file
91
- try {
92
- val inspecter = new TastyInspecter
93
- ConsumeTasty (classpath, classname :: Nil , inspecter)
94
- inspecter.source_path.foreach(
95
- source =>
96
- source_to_tasty +=
97
- (source -> (tasty_path
98
- .toPath() :: source_to_tasty.getOrElse(source, Nil ))))
99
- } catch {
100
- case _ : InvocationTargetException => println(tasty_path)
101
- }
102
- })
103
- source_to_tasty
104
- }
105
-
106
- /** Infers a tuple (class path, class name) from a given path */
107
- def getClasspathClassname (file : Path ): (String , String ) = {
108
- val pat = """ (.*)\..*""" .r
109
- val classpath = file.getParent().getParent().toString()
110
- val modulename = file.getParent().getFileName().toString()
111
- val sourcename =
112
- file.toFile().getName().toString() match {
113
- case pat(name) => name
114
- case _ => " "
115
- }
116
- return (classpath, modulename + " ." + sourcename)
117
- }
118
-
119
- def getTastySemanticdb (scalaFile : Path ): s.TextDocument = {
120
- val scalac = getScalacSemanticdb(scalaFile)
121
-
122
- val tasty_files = source_to_tasty.getOrElse(
123
- sourceDirectory.resolve(scalaFile).toString,
124
- Nil )
125
-
126
- val tasty_classes = tasty_files.map(getClasspathClassname)
127
- // If we have more than one classpath then something went wrong
128
- if (tasty_classes.groupBy((a, _) => a).size != 1 ) {
129
- scalac
130
- } else {
131
- val (classpaths, classnames) = tasty_classes.unzip
132
- val sdbconsumer = new SemanticdbConsumer (scalaFile)
133
-
134
- val _ = ConsumeTasty (classpaths.head, classnames, sdbconsumer)
135
- sdbconsumer.toSemanticdb()
136
- }
34
+ /** Returns the SemanticDB for this Scala source file. */
35
+ def getTastySemanticdb (classPath : Path , scalaFile : Path ) : s.TextDocument = {
36
+ val classNames = Utils .getClassNames(classPath, scalaFile, " example/" )
37
+ println(classPath)
38
+ println(classNames)
39
+ println(scalaFile)
40
+ val sdbconsumer = new SemanticdbConsumer (scalaFile)
41
+
42
+ val _ = ConsumeTasty (classPath.toString, classNames, sdbconsumer)
43
+ sdbconsumer.toSemanticdb()
137
44
}
138
45
139
46
/** Fails the test if the s.TextDocument from tasty and semanticdb-scalac are not the same. */
140
47
def checkFile (filename : String ): Unit = {
141
48
val path = sourceDirectory.resolve(filename)
142
49
val scalac = getScalacSemanticdb(path)
143
- val tasty = getTastySemanticdb(path)
50
+ val tasty = getTastySemanticdb(tastyClassDirectory, path)
144
51
println(tasty)
145
52
val obtained = Semanticdbs .printTextDocument(tasty)
146
53
val expected = Semanticdbs .printTextDocument(scalac)
0 commit comments