Skip to content

Commit 6017117

Browse files
committed
Refactor path logic and fix symbol names
1 parent e3654a4 commit 6017117

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

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

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Flags._
1212
import Decorators._
1313
import Names.Name
1414
import StdNames.nme
15-
import util.SourcePosition
15+
import util.{SourceFile, SourcePosition}
1616
import collection.mutable
1717
import java.lang.Character.{isJavaIdentifierPart, isJavaIdentifierStart}
1818
import java.nio.file.Paths
@@ -34,17 +34,7 @@ class ExtractSemanticDB extends Phase {
3434
val extract = Extractor()
3535
val unit = ctx.compilationUnit
3636
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-
)
37+
ExtractSemanticDB.write(unit.source, extract.occurrences.toList)
4838
}
4939

5040
class Extractor extends TreeTraverser {
@@ -64,8 +54,6 @@ class ExtractSemanticDB extends Phase {
6454

6555
def isJavaIdent(str: String) =
6656
isJavaIdentifierStart(str.head) && str.tail.forall(isJavaIdentifierPart)
67-
|| str == nme.CONSTRUCTOR.toString
68-
|| str == nme.STATIC_CONSTRUCTOR.toString
6957

7058
def nameToString(name: Name) =
7159
val str = name.toString
@@ -76,22 +64,22 @@ class ExtractSemanticDB extends Phase {
7664
|| (sym.is(Param) || sym.owner.isClass) && isGlobal(sym.owner)
7765

7866
def ownerString(owner: Symbol): String =
79-
if owner.isRoot || owner.isEmptyPackage then "" else symbolName(owner) + "/"
67+
if owner.isRoot || owner.isEmptyPackage then "" else symbolName(owner)
8068

8169
def overloadIdx(sym: Symbol): String =
8270
val alts = sym.owner.info.decls.lookupAll(sym.name).toList
8371
if alts.tail.isEmpty then ""
8472
else
8573
val idx = alts.indexOf(sym)
8674
assert(idx >= 0)
87-
idx.toString
75+
"+" + idx.toString
8876

8977
def descriptor(sym: Symbol): String =
9078
if sym.is(ModuleClass) then
9179
descriptor(sym.sourceModule)
9280
else
9381
val str = nameToString(sym.name)
94-
if sym.is(Package) then str
82+
if sym.is(Package) then str + "/"
9583
else if sym.isType then str + "#"
9684
else if sym.isRealMethod then str + "(" + overloadIdx(sym) + ")"
9785
else if sym.is(TermParam) || sym.is(ParamAccessor) then "(" + str + ")"
@@ -154,26 +142,28 @@ object ExtractSemanticDB {
154142

155143
val name: String = "extractSemanticDB"
156144

157-
def write(
158-
source: Path,
159-
contents: String,
160-
sourceroot: Path,
161-
targetroot: Path,
162-
occurrences: Seq[SymbolOccurrence]
163-
): Unit =
164-
val relpath = sourceroot.relativize(source)
165-
val reluri = relpath.iterator().asScala.mkString("/")
166-
val outpath = targetroot
145+
def write(source: SourceFile, occurrences: List[SymbolOccurrence])(given ctx: Context): Unit =
146+
val sourcePath = source.file.jpath
147+
val sourceRoot = Paths.get(ctx.settings.sourceroot.value)
148+
val targetRoot =
149+
val targetRootSetting = ctx.settings.targetroot.value
150+
if targetRootSetting.isEmpty then ctx.settings.outputDir.value.jpath
151+
else Paths.get(targetRootSetting)
152+
println(i"extract from $sourcePath from $sourceRoot, targetRoot = $targetRoot")
153+
val relPath = sourceRoot.relativize(sourcePath)
154+
println(i"relPath = $relPath")
155+
val relURI = relPath.iterator().asScala.mkString("/")
156+
val outpath = targetRoot
167157
.resolve("META-INF")
168158
.resolve("semanticdb")
169-
.resolve(relpath)
170-
.resolveSibling(source.getFileName().toString() + ".semanticdb")
159+
.resolve(relPath)
160+
.resolveSibling(sourcePath.getFileName().toString() + ".semanticdb")
171161
Files.createDirectories(outpath.getParent())
172162
val doc: TextDocument = TextDocument(
173163
schema = Schema.SEMANTICDB4,
174164
language = Language.SCALA,
175-
uri = reluri,
176-
md5 = MD5.compute(contents),
165+
uri = relURI,
166+
md5 = MD5.compute(String(source.content)),
177167
occurrences = occurrences
178168
)
179169
val docs = TextDocuments(List(doc))

0 commit comments

Comments
 (0)