Skip to content

Commit d00e18a

Browse files
committed
more generic disimbiguator using tastyreflect 'methods' function
1 parent 10f57a3 commit d00e18a

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

semanticdb/src/dotty/semanticdb/SemanticdbConsumer.scala

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class SemanticdbConsumer extends TastyConsumer {
3838

3939
object Traverser extends TreeTraverser {
4040
val symbolsCache: HashMap[Symbol, String] = HashMap()
41-
val symbolPathsDisimbiguator: HashMap[String, Int] = HashMap()
4241
implicit class TreeExtender(tree: Tree) {
4342
def isUserCreated: Boolean = {
4443
val children: List[Position] =
@@ -85,14 +84,29 @@ class SemanticdbConsumer extends TastyConsumer {
8584
def isJavaClass: Boolean = false
8685
}
8786

88-
def disimbiguate(symbol_path: String): String = {
89-
if (symbolPathsDisimbiguator.contains(symbol_path)) {
90-
symbolPathsDisimbiguator +=
91-
(symbol_path -> (symbolPathsDisimbiguator(symbol_path) + 1))
92-
"(+" + (symbolPathsDisimbiguator(symbol_path) - 1) + ")"
93-
} else {
94-
symbolPathsDisimbiguator += (symbol_path -> 1)
87+
def resolveClass(symbol: ClassSymbol): Symbol =
88+
(symbol.companionClass, symbol.companionModule) match {
89+
case (_, Some(module)) if symbol.flags.isObject => module
90+
case (Some(c), _) => c
91+
case _ => symbol
92+
}
93+
94+
def disimbiguate(symbol_path: String, symbol: Symbol): String = {
95+
val symbolcl = resolveClass(symbol.owner.asClass)
96+
val methods = symbolcl.asClass.method(symbol.name)
97+
val (methods_count, method_pos) =
98+
methods.foldLeft((0, -1))((x:Tuple2[Int, Int], m:Symbol) => {
99+
if (m == symbol)
100+
(x._1+1, x._1)
101+
else
102+
(x._1+1, x._2)
103+
})
104+
val real_pos = methods_count - method_pos - 1
105+
106+
if (real_pos == 0) {
95107
"()"
108+
} else {
109+
"(+" + real_pos + ")"
96110
}
97111
}
98112

@@ -111,20 +125,15 @@ class SemanticdbConsumer extends TastyConsumer {
111125
if (symbol.isPackage) {
112126
d.Package(symbol.name)
113127
} else if (symbol.isObject) {
114-
symbol.asClass.companionModule match {
115-
case Some(module) => d.Term(module.name)
116-
case _ => d.Term(symbol.name)
117-
}
128+
d.Term(resolveClass(symbol.asClass).name)
118129
} else if (symbol.isMethod) {
119130
d.Method(symbol.name,
120-
disimbiguate(previous_symbol + symbol.name))
131+
disimbiguate(previous_symbol + symbol.name, symbol))
121132
} else if (symbol.isValueParameter) {
122133
d.Parameter(symbol.name)
123134
} else if (symbol.isTypeParameter) {
124135
d.TypeParameter(symbol.name)
125136
} else if (symbol.isType || symbol.isTrait) {
126-
//println(symbol.name, symbol.companionClass.name, symbol.companionModule.name, symbol.flags.toString)
127-
128137
d.Type(symbol.name)
129138
} else {
130139
d.Term(symbol.name)

0 commit comments

Comments
 (0)