Skip to content

Commit 0dac7a4

Browse files
nicolas-guichardantonsviridov-src
authored andcommitted
Rework method disambiguator
SemanticDB and SCIP don't disambiguate methods by adding their full signature, instead they sort overloaded methods by declaration order, the first one is `method().`, the next one is `method(+1).`, the next one `method(+2).` etc. Down to 14 failing tests.
1 parent 9b749d6 commit 0dac7a4

File tree

2 files changed

+30
-37
lines changed

2 files changed

+30
-37
lines changed

semanticdb-kotlinc/src/main/kotlin/com/sourcegraph/semanticdb_kotlinc/SymbolsCache.kt

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import kotlin.contracts.ExperimentalContracts
66
import kotlin.contracts.contract
77
import org.jetbrains.kotlin.fir.analysis.checkers.declaration.isLocalMember
88
import org.jetbrains.kotlin.fir.analysis.checkers.getContainingSymbol
9+
import org.jetbrains.kotlin.fir.declarations.FirClass
910
import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin
10-
import org.jetbrains.kotlin.fir.declarations.FirFunction
11+
import org.jetbrains.kotlin.fir.declarations.utils.memberDeclarationNameOrNull
1112
import org.jetbrains.kotlin.fir.declarations.utils.nameOrSpecialName
1213
import org.jetbrains.kotlin.fir.packageFqName
1314
import org.jetbrains.kotlin.fir.resolve.getContainingDeclaration
15+
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
1416
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
1517
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
1618
import org.jetbrains.kotlin.fir.symbols.impl.*
17-
import org.jetbrains.kotlin.fir.types.ConeKotlinType
18-
import org.jetbrains.kotlin.fir.types.coneType
1919
import org.jetbrains.kotlin.name.FqName
2020
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeAsciiOnly
2121

@@ -163,44 +163,37 @@ class GlobalSymbolsCache(testing: Boolean = false) : Iterable<Symbol> {
163163
}
164164

165165
@OptIn(SymbolInternals::class)
166-
fun disambiguateCallableSymbol(callableSymbol: FirCallableSymbol<*>): String {
167-
val callableId = callableSymbol.callableId
168-
val callableName = callableId.callableName.asString()
169-
val fqName = callableId.packageName.asString()
170-
171-
// Get the FIR element associated with the callable symbol
172-
val firFunction = callableSymbol.fir as? FirFunction ?: return "$fqName.$callableName"
173-
174-
// Get parameter types from the function's value parameters
175-
val parameterTypes =
176-
firFunction.valueParameters.joinToString(separator = ", ") {
177-
it.returnTypeRef.coneType.render()
166+
private fun methodDisambiguator(symbol: FirFunctionSymbol<*>): String {
167+
val session = symbol.moduleData.session
168+
169+
val siblings =
170+
when (val containingSymbol = symbol.getContainingSymbol(session)) {
171+
is FirClassSymbol ->
172+
(containingSymbol.fir as FirClass).declarations.map { it.symbol }
173+
is FirFileSymbol -> containingSymbol.fir.declarations.map { it.symbol }
174+
null ->
175+
symbol.moduleData.session.symbolProvider.getTopLevelCallableSymbols(
176+
symbol.packageFqName(), symbol.name)
177+
else -> return "()"
178178
}
179179

180-
// Get the return type (for functions and properties)
181-
val returnType = firFunction.returnTypeRef.coneType.render()
182-
183-
// Create a string representing the fully qualified name + signature
184-
return "$fqName.$callableName($parameterTypes): $returnType"
185-
}
180+
var count = 0
181+
var found = false
182+
for (decl in siblings) {
183+
if (decl == symbol) {
184+
found = true
185+
break
186+
}
186187

187-
// Extension function to render a ConeKotlinType to a string
188-
private fun ConeKotlinType?.render(): String = this?.toString() ?: "Unit"
188+
if (decl.memberDeclarationNameOrNull?.equals(symbol.name) == true) {
189+
count++
190+
}
191+
}
189192

190-
private fun disambiguateClassSymbol(classSymbol: FirClassSymbol<*>): String {
191-
val classId = classSymbol.classId
192-
val fqName = classId.asString()
193-
// You can also add additional details like visibility or modifiers if needed
194-
return "class $fqName"
193+
if (count == 0 || !found) return "()"
194+
return "(+${count})"
195195
}
196196

197-
private fun methodDisambiguator(symbol: FirBasedSymbol<*>): String =
198-
when (symbol) {
199-
is FirCallableSymbol<*> -> disambiguateCallableSymbol(symbol)
200-
is FirClassSymbol<*> -> disambiguateClassSymbol(symbol)
201-
else -> "()"
202-
}
203-
204197
override fun iterator(): Iterator<Symbol> = globals.values.iterator()
205198
}
206199

semanticdb-kotlinc/src/test/kotlin/com/sourcegraph/semanticdb_kotlinc/test/SemanticdbSymbolsTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ class SemanticdbSymbolsTest {
183183
symbolsCacheData =
184184
SymbolCacheData(
185185
listOf(
186-
"kotlin/collections/mapOf(+1).".symbol(),
187-
"kotlin/io/println().".symbol()))))
186+
"kotlin/collections/mapOf(+2).".symbol(),
187+
"kotlin/io/println(+10).".symbol()))))
188188
.mapCheckExpectedSymbols()
189189

190190
@TestFactory

0 commit comments

Comments
 (0)