|
| 1 | +package dotty.semanticdb |
| 2 | + |
| 3 | +import java.nio.file._ |
| 4 | +import java.nio.charset.StandardCharsets |
| 5 | +import scala.meta.internal.{semanticdb => s} |
| 6 | +import scala.collection.JavaConverters._ |
| 7 | +import dotty.tools.dotc.util.SourceFile |
| 8 | + |
| 9 | +object Semanticdbs { |
| 10 | + |
| 11 | + /** |
| 12 | + * Utility to load SemanticDB for Scala source files. |
| 13 | + * |
| 14 | + * @param sourceroot The workspace root directory, by convention matches the directory of build.sbt |
| 15 | + * @param classpath The classpath for this project, can be a combination of jars and directories. |
| 16 | + * Matches the `fullClasspath` task key from sbt but can be only `classDirectory` |
| 17 | + * if you only care about reading SemanticDB files from a single project. |
| 18 | + */ |
| 19 | + class Loader(sourceroot: Path, classpath: List[Path]) { |
| 20 | + private val META_INF = Paths.get("META-INF", "semanticdb") |
| 21 | + private val classLoader = new java.net.URLClassLoader(classpath.map(_.toUri.toURL).toArray) |
| 22 | + /** Returns a SemanticDB for a single Scala source file, if any. The path must be absolute. */ |
| 23 | + def resolve(scalaAbsolutePath: Path): Option[s.TextDocument] = { |
| 24 | + val scalaRelativePath = sourceroot.relativize(scalaAbsolutePath) |
| 25 | + val filename = scalaRelativePath.getFileName.toString |
| 26 | + val semanticdbRelativePath = scalaRelativePath.resolveSibling(filename + ".semanticdb") |
| 27 | + val metaInfPath = META_INF.resolve(semanticdbRelativePath).toString |
| 28 | + Option(classLoader.findResource(metaInfPath)).map { url => |
| 29 | + val semanticdbAbsolutePath = Paths.get(url.toURI) |
| 30 | + Semanticdbs.loadTextDocument(scalaAbsolutePath, scalaRelativePath, semanticdbAbsolutePath) |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** Load SemanticDB TextDocument for a single Scala source file |
| 36 | + * |
| 37 | + * @param scalaAbsolutePath Absolute path to a Scala source file. |
| 38 | + * @param scalaRelativePath scalaAbsolutePath relativized by the sourceroot. |
| 39 | + * @param semanticdbAbsolutePath Absolute path to the SemanticDB file. |
| 40 | + */ |
| 41 | + def loadTextDocument( |
| 42 | + scalaAbsolutePath: Path, |
| 43 | + scalaRelativePath: Path, |
| 44 | + semanticdbAbsolutePath: Path |
| 45 | + ): s.TextDocument = { |
| 46 | + val reluri = scalaRelativePath.iterator.asScala.mkString("/") |
| 47 | + val sdocs = parseTextDocuments(semanticdbAbsolutePath) |
| 48 | + sdocs.documents.find(_.uri == reluri) match { |
| 49 | + case None => throw new NoSuchElementException(reluri) |
| 50 | + case Some(document) => |
| 51 | + val text = new String(Files.readAllBytes(scalaAbsolutePath), StandardCharsets.UTF_8) |
| 52 | + // Assert the SemanticDB payload is in-sync with the contents of the Scala file on disk. |
| 53 | + val md5FingerprintOnDisk = MD5.compute(text) |
| 54 | + if (document.md5 != md5FingerprintOnDisk) { |
| 55 | + throw new IllegalArgumentException("stale semanticdb: " + reluri) |
| 56 | + } else { |
| 57 | + // Update text document to include full text contents of the file. |
| 58 | + document.withText(text) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** Parses SemanticDB text documents from an absolute path to a `*.semanticdb` file. */ |
| 64 | + def parseTextDocuments(path: Path): s.TextDocuments = { |
| 65 | + // NOTE: a *.semanticdb file is of type s.TextDocuments, not s.TextDocument |
| 66 | + val in = Files.newInputStream(path) |
| 67 | + try s.TextDocuments.parseFrom(in) |
| 68 | + finally in.close() |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** Prettyprint a text document with symbol occurrences next to each resolved identifier. |
| 73 | + * |
| 74 | + * Useful for testing purposes to ensure that SymbolOccurrence values make sense and are correct. |
| 75 | + * Example output (NOTE, slightly modified to avoid "unclosed comment" errors): |
| 76 | + * {{{ |
| 77 | + * class Example *example/Example#* { |
| 78 | + * val a *example/Example#a.* : String *scala/Predef.String#* = "1" |
| 79 | + * } |
| 80 | + * }}} |
| 81 | + **/ |
| 82 | + def printTextDocument(doc: s.TextDocument): String = { |
| 83 | + val sb = new StringBuilder |
| 84 | + val occurrences = doc.occurrences.sorted |
| 85 | + val sourceFile = new SourceFile(doc.uri, doc.text) |
| 86 | + var offset = 0 |
| 87 | + occurrences.foreach { occ => |
| 88 | + val range = occ.range.get |
| 89 | + val end = sourceFile.lineToOffset(range.endLine) + range.endCharacter |
| 90 | + sb.append(doc.text.substring(offset, end)) |
| 91 | + sb.append(" /* ") |
| 92 | + .append(occ.symbol) |
| 93 | + .append(" */ ") |
| 94 | + offset = end |
| 95 | + } |
| 96 | + sb.append(doc.text.substring(offset)) |
| 97 | + sb.toString() |
| 98 | + } |
| 99 | + |
| 100 | + /** Sort symbol occurrences by their start position. */ |
| 101 | + implicit val occurrenceOrdering: Ordering[s.SymbolOccurrence] = |
| 102 | + new Ordering[s.SymbolOccurrence] { |
| 103 | + override def compare(x: s.SymbolOccurrence, y: s.SymbolOccurrence): Int = { |
| 104 | + if (x.range.isEmpty) 0 |
| 105 | + else if (y.range.isEmpty) 0 |
| 106 | + else { |
| 107 | + val a = x.range.get |
| 108 | + val b = y.range.get |
| 109 | + val byLine = Integer.compare( |
| 110 | + a.startLine, |
| 111 | + b.startLine |
| 112 | + ) |
| 113 | + if (byLine != 0) { |
| 114 | + byLine |
| 115 | + } else { |
| 116 | + val byCharacter = Integer.compare( |
| 117 | + a.startCharacter, |
| 118 | + b.startCharacter |
| 119 | + ) |
| 120 | + byCharacter |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments