Skip to content

Commit 50e0fd2

Browse files
committed
[WIP] Don't parse TextDocuments in AppendDiagnostics
1 parent 56c5909 commit 50e0fd2

File tree

3 files changed

+51
-99
lines changed

3 files changed

+51
-99
lines changed

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ import dotty.tools.io.{AbstractFile, JarArchive}
3030
import dotty.tools.dotc.util.Property
3131
import dotty.tools.dotc.semanticdb.DiagnosticOps.*
3232
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
3338

3439

3540
/** Extract symbol references and uses to semanticdb files.
@@ -63,22 +68,20 @@ class ExtractSemanticDB private (phaseMode: ExtractSemanticDB.PhaseMode, suffix:
6368

6469
override def runOn(units: List[CompilationUnit])(using ctx: Context): List[CompilationUnit] = {
6570
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
7475
warnings.get(unit.source).foreach { ws =>
7576
ExtractSemanticDB.appendDiagnostics(unit.source, ws.map(_.toSemanticDiagnostic))
7677
}
77-
else
78+
}
79+
else
80+
units.foreach { unit =>
7881
val extractor = ExtractSemanticDB.Extractor()
7982
extractor.extract(unit.tpdTree)
8083
ExtractSemanticDB.write(unit.source, extractor.occurrences.toList, extractor.symbolInfos.toList, extractor.synthetics.toList)
81-
}
84+
}
8285
units
8386
}
8487

@@ -145,15 +148,43 @@ object ExtractSemanticDB:
145148
val path = semanticdbPath(source)
146149
Using.Manager { use =>
147150
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)
150177

151178
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)
155184
} 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?
157188
case Success(_) => // success to update semanticdb, say nothing
158189
end appendDiagnostics
159190

compiler/test/dotty/tools/dotc/semanticdb/SemanticdbTests.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,12 @@ class SemanticdbTests:
8787
.resolve("semanticdb")
8888
.resolve(relpath)
8989
.resolveSibling(filename + ".semanticdb")
90+
println(semanticdbPath)
9091
val expectPath = source.resolveSibling(filename.replace(".scala", ".expect.scala"))
9192
val doc = Tools.loadTextDocument(source, relpath, semanticdbPath)
93+
println(semanticdbPath.getFileName().toString())
94+
if (semanticdbPath.getFileName().toString() == "ValPattern.scala.semanticdb")
95+
println(doc)
9296
Tools.metac(doc, rootSrc.relativize(source))(using metacSb)
9397
val obtained = trimTrailingWhitespace(SemanticdbTests.printTextDocument(doc))
9498
collectErrorOrUpdate(expectPath, obtained)

0 commit comments

Comments
 (0)