@@ -15,6 +15,7 @@ import StdNames.nme
15
15
import util .SourcePosition
16
16
import collection .mutable
17
17
import java .lang .Character .{isJavaIdentifierPart , isJavaIdentifierStart }
18
+ import java .nio .file .Paths
18
19
19
20
abstract class ExtractSemanticDB extends Phase {
20
21
import ast .tpd ._
@@ -33,6 +34,17 @@ abstract class ExtractSemanticDB extends Phase {
33
34
val extract = Extractor ()
34
35
val unit = ctx.compilationUnit
35
36
extract.traverse(unit.tpdTree)
37
+ val targetRootSetting = ctx.settings.targetroot.value
38
+ val targetRoot =
39
+ if targetRootSetting.isEmpty then ctx.settings.outputDir.value.jpath
40
+ else Paths .get(targetRootSetting)
41
+ ExtractSemanticDB .write(
42
+ unit.source.file.jpath,
43
+ String (unit.source.content),
44
+ Paths .get(ctx.settings.sourceroot.value),
45
+ Paths .get(ctx.settings.targetroot.value),
46
+ extract.occurrences.toList
47
+ )
36
48
}
37
49
38
50
class Extractor extends TreeTraverser {
@@ -126,5 +138,40 @@ abstract class ExtractSemanticDB extends Phase {
126
138
}
127
139
128
140
object ExtractSemanticDB {
141
+ import java .nio .file .Path
142
+ import scala .collection .JavaConverters ._
143
+ import java .nio .file .Files
144
+
129
145
val name : String = " extractSemanticDB"
146
+
147
+ def write (
148
+ source : Path ,
149
+ contents : String ,
150
+ sourceroot : Path ,
151
+ targetroot : Path ,
152
+ occurrences : Seq [SymbolOccurrence ]
153
+ ): Unit =
154
+ val relpath = sourceroot.relativize(source)
155
+ val reluri = relpath.iterator().asScala.mkString(" /" )
156
+ val outpath = targetroot
157
+ .resolve(" META-INF" )
158
+ .resolve(" semanticdb" )
159
+ .resolve(relpath)
160
+ .resolveSibling(source.getFileName().toString() + " .semanticdb" )
161
+ Files .createDirectories(outpath.getParent())
162
+ val doc : TextDocument = TextDocument (
163
+ schema = Schema .SEMANTICDB4 ,
164
+ language = Language .SCALA ,
165
+ uri = reluri,
166
+ md5 = MD5 .compute(contents),
167
+ occurrences = occurrences
168
+ )
169
+ val docs = TextDocuments (List (doc))
170
+ val out = Files .newOutputStream(outpath)
171
+ try
172
+ val stream = SemanticdbOutputStream .newInstance(out)
173
+ docs.writeTo(stream)
174
+ stream.flush()
175
+ finally
176
+ out.close()
130
177
}
0 commit comments