Skip to content

Commit ba36954

Browse files
committed
basic disimbiguator
1 parent a76c25f commit ba36954

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

semanticdb/src/dotty/semanticdb/SemanticdbConsumer.scala

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import dotty.tools.dotc.tastyreflect
88
import scala.collection.mutable.HashMap
99

1010
class SemanticdbConsumer extends TastyConsumer {
11-
var stack : List[String] = Nil
11+
var stack: List[String] = Nil
12+
13+
/*
1214
val symbolsDefs : HashMap[String, Int] = HashMap()
1315
val symbolsVals : HashMap[String, Int] = HashMap()
1416
@@ -29,41 +31,62 @@ class SemanticdbConsumer extends TastyConsumer {
2931
symbolsVals += (path -> 1)
3032
""
3133
}
32-
}
34+
}*/
3335

3436
final def apply(reflect: Reflection)(root: reflect.Tree): Unit = {
3537
import reflect._
3638
object Traverser extends TreeTraverser {
39+
val symbolsCache: HashMap[tasty.Symbol, String] = HashMap()
40+
val symbolPathsDisimbiguator: HashMap[String, Int] = HashMap()
3741

3842
def packageDefToOccurence(term: Term): String = {
3943
//println(term, term.pos.start, term.pos.end)
4044
val Term.Ident(id) = term
4145
return stack.head + id + "/"
4246
}
4347

48+
def disimbiguate(symbol_path: String): String = {
49+
if (symbolPathsDisimbiguator.contains(symbol_path)) {
50+
symbolPathsDisimbiguator +=
51+
(symbol_path -> (symbolPathsDisimbiguator(symbol_path) + 1))
52+
"(+" + (symbolPathsDisimbiguator(symbol_path) - 1) + ")"
53+
} else {
54+
symbolPathsDisimbiguator += (symbol_path -> 1)
55+
"()"
56+
}
57+
}
58+
4459
def iterateParent(symbol: Symbol): String = {
45-
if (symbol.name == "<none>") then {
46-
// TODO had a "NoDenotation" test to avoid
47-
// relying on the name itself
48-
""
60+
if (symbolsCache.contains(symbol)) {
61+
return symbolsCache(symbol)
4962
} else {
50-
val previous_symbol = iterateParent(symbol.owner)
51-
val next_atom =
52-
symbol match {
53-
case IsPackageSymbol(symbol) => symbol.name + "/"
54-
case IsClassSymbol(symbol) => symbol.name + "#"
55-
case IsDefSymbol(symbol) => symbol.name + "."
56-
case IsValSymbol(symbol) => symbol.name + "."
57-
case owner => {
58-
""
59-
}
60-
}
61-
previous_symbol + next_atom
63+
val out_symbol_path =
64+
if (symbol.name == "<none>") then {
65+
// TODO had a "NoDenotation" test to avoid
66+
// relying on the name itself
67+
""
68+
} else {
69+
val previous_symbol = iterateParent(symbol.owner)
70+
val next_atom =
71+
symbol match {
72+
case IsPackageSymbol(symbol) => symbol.name + "/"
73+
case IsClassSymbol(symbol) => symbol.name + "#"
74+
case IsDefSymbol(symbol) =>
75+
symbol.name + disimbiguate(previous_symbol + symbol.name) + "."
76+
case IsValSymbol(symbol) => symbol.name + "."
77+
case owner => {
78+
""
79+
}
80+
}
81+
previous_symbol + next_atom
82+
}
83+
symbolsCache += (symbol -> out_symbol_path)
84+
out_symbol_path
6285
}
6386
}
6487

6588
override def traverseTree(tree: Tree)(implicit ctx: Context): Unit = {
66-
val previous_path = stack.head
89+
val previous_path = stack.head
6790

6891
tree match {
6992
/*case IsClassDef(body) =>

0 commit comments

Comments
 (0)