@@ -30,6 +30,11 @@ import dotty.tools.io.{AbstractFile, JarArchive}
30
30
import dotty .tools .dotc .util .Property
31
31
import dotty .tools .dotc .semanticdb .DiagnosticOps .*
32
32
import scala .util .{Using , Failure , Success }
33
+ import com .google .protobuf .Empty
34
+ import com .google .protobuf .UnknownFieldSet
35
+ import com .google .protobuf .UnknownFieldSet .Field
36
+ import java .io .ByteArrayOutputStream
37
+ import java .io .BufferedOutputStream
33
38
34
39
35
40
/** Extract symbol references and uses to semanticdb files.
@@ -63,22 +68,20 @@ class ExtractSemanticDB private (phaseMode: ExtractSemanticDB.PhaseMode, suffix:
63
68
64
69
override def runOn (units : List [CompilationUnit ])(using ctx : Context ): List [CompilationUnit ] = {
65
70
val appendDiagnostics = phaseMode == ExtractSemanticDB .PhaseMode .AppendDiagnostics
66
- val warnings =
67
- if (appendDiagnostics)
68
- ctx.reporter.allWarnings.groupBy(w => w.pos.source)
69
- else Map .empty
70
-
71
- units.asJava.parallelStream().forEach { unit =>
72
- val unitCtx = ctx.fresh.setCompilationUnit(unit).withRootImports
73
- if (appendDiagnostics)
71
+ if (appendDiagnostics)
72
+ val warnings = ctx.reporter.allWarnings.groupBy(w => w.pos.source)
73
+ units.asJava.parallelStream().forEach { unit =>
74
+ val unitCtx = ctx.fresh.setCompilationUnit(unit).withRootImports
74
75
warnings.get(unit.source).foreach { ws =>
75
76
ExtractSemanticDB .appendDiagnostics(unit.source, ws.map(_.toSemanticDiagnostic))
76
77
}
77
- else
78
+ }
79
+ else
80
+ units.foreach { unit =>
78
81
val extractor = ExtractSemanticDB .Extractor ()
79
82
extractor.extract(unit.tpdTree)
80
83
ExtractSemanticDB .write(unit.source, extractor.occurrences.toList, extractor.symbolInfos.toList, extractor.synthetics.toList)
81
- }
84
+ }
82
85
units
83
86
}
84
87
@@ -145,15 +148,43 @@ object ExtractSemanticDB:
145
148
val path = semanticdbPath(source)
146
149
Using .Manager { use =>
147
150
val in = use(Files .newInputStream(path))
148
- val sin = internal.SemanticdbInputStream .newInstance(in)
149
- val docs = TextDocuments .parseFrom(sin)
151
+ // val sin = internal.SemanticdbInputStream.newInstance(in)
152
+ val textDocuments = Empty .parseFrom(in)
153
+ val docsBytes = textDocuments.getUnknownFields().getField(TextDocuments .DOCUMENTS_FIELD_NUMBER ).getLengthDelimitedList()
154
+ val docFields = Empty .parseFrom(docsBytes.get(0 )).getUnknownFields()
155
+ if (source.file.name == " ValPattern.scala" )
156
+ println(docFields)
157
+ // docMap.put(7, )
158
+
159
+ // val docs = TextDocuments.parseFrom(sin)
160
+
161
+ val bos = use(new ByteArrayOutputStream ())
162
+ val sbos = internal.SemanticdbOutputStream .newInstance(bos)
163
+ val doc = TextDocument (diagnostics = diagnostics)
164
+ doc.writeTo(sbos)
165
+ sbos.flush()
166
+ val diagnosticsOnly = Empty .parseFrom(bos.toByteArray()).getUnknownFields()
167
+
168
+ val merged = docFields.toBuilder().mergeFrom(diagnosticsOnly).build()
169
+ // println(merged)
170
+ val field = Field .newBuilder().addLengthDelimited(merged.toByteString()).build()
171
+
172
+ val fields = textDocuments.getUnknownFields().toBuilder().mergeField(TextDocuments .DOCUMENTS_FIELD_NUMBER , field).build()
173
+ // println(fields)
174
+ val updated = textDocuments.toBuilder().setUnknownFields(fields).build()
175
+ if (source.file.name == " ValPattern.scala" )
176
+ println(updated)
150
177
151
178
val out = use(Files .newOutputStream(path))
152
- val sout = internal.SemanticdbOutputStream .newInstance(out)
153
- TextDocuments (docs.documents.map(_.withDiagnostics(diagnostics))).writeTo(sout)
154
- sout.flush()
179
+ val bout = new BufferedOutputStream (out)
180
+ updated.writeTo(bout)
181
+ bout.flush()
182
+ // val sout = internal.SemanticdbOutputStream.newInstance(out)
183
+ // TextDocuments(docs.documents.map(_.withDiagnostics(diagnostics))).writeTo(sout)
155
184
} match
156
- case Failure (ex) => // failed somehow, should we say something?
185
+ case Failure (ex) =>
186
+ println(ex.getMessage())
187
+ // failed somehow, should we say something?
157
188
case Success (_) => // success to update semanticdb, say nothing
158
189
end appendDiagnostics
159
190
0 commit comments