@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.com.intellij.psi.PsiElement
20
20
import org.jetbrains.kotlin.fir.FirElement
21
21
import org.jetbrains.kotlin.fir.FirPackageDirective
22
22
import org.jetbrains.kotlin.fir.declarations.*
23
+ import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertySetter
23
24
import org.jetbrains.kotlin.fir.expressions.*
24
25
import org.jetbrains.kotlin.fir.expressions.impl.FirElseIfTrueCondition
25
26
import org.jetbrains.kotlin.fir.expressions.impl.FirSingleExpressionBlock
@@ -55,8 +56,11 @@ class PsiElementAssociations(val typeMapping: KotlinTypeMapping, val file: FirFi
55
56
depth++
56
57
element.acceptChildren(this , data)
57
58
if (element is FirResolvedTypeRef ) {
58
- // not sure why this isn't taken care of by `FirResolvedTypeRefImpl#acceptChildren()`
59
- element.delegatedTypeRef?.accept(this , data)
59
+ // Do not visit FirUserTypeRef, since it's not mappable to a type.
60
+ if (element.delegatedTypeRef != null && element.delegatedTypeRef !is FirUserTypeRef ) {
61
+ // not sure why this isn't taken care of by `FirResolvedTypeRefImpl#acceptChildren()`
62
+ element.delegatedTypeRef?.accept(this , data)
63
+ }
60
64
}
61
65
depth--
62
66
}
@@ -69,12 +73,7 @@ class PsiElementAssociations(val typeMapping: KotlinTypeMapping, val file: FirFi
69
73
}
70
74
71
75
fun primary (psiElement : PsiElement ) =
72
- fir(psiElement) { filterFirElement(it) }
73
-
74
- private fun filterFirElement (firElement : FirElement ) : Boolean {
75
- return firElement.source is KtRealPsiSourceElement &&
76
- firElement !is FirUserTypeRef
77
- }
76
+ fir(psiElement) { it.source is KtRealPsiSourceElement }
78
77
79
78
fun methodDeclarationType (psi : PsiElement ): JavaType .Method ? {
80
79
return when (val fir = primary(psi)) {
@@ -173,7 +172,43 @@ class PsiElementAssociations(val typeMapping: KotlinTypeMapping, val file: FirFi
173
172
val directFirInfos = allFirInfos.filter { filter.invoke(it.fir) }
174
173
return if (directFirInfos.isNotEmpty())
175
174
// It might be more reliable to have explicit mappings in case something changes.
176
- directFirInfos[0 ].fir
175
+ return when {
176
+ directFirInfos.size == 1 -> directFirInfos[0 ].fir
177
+ else -> {
178
+ return when (p) {
179
+ is KtConstantExpression -> {
180
+ directFirInfos.firstOrNull { it.fir is FirConstExpression <* > }?.fir
181
+ }
182
+ is KtImportDirective -> {
183
+ directFirInfos.firstOrNull { it.fir is FirImport && it.fir !is FirErrorImport }?.fir
184
+ }
185
+ is KtNamedFunction -> {
186
+ val found = directFirInfos.firstOrNull { it.fir is FirFunction }?.fir
187
+ // if (found == null) {
188
+ // // Review how to expose unmatched types without causing an error.
189
+ // }
190
+ found
191
+ }
192
+ is KtNameReferenceExpression , is KtTypeReference -> {
193
+ val found = directFirInfos.firstOrNull { it.fir is FirResolvedTypeRef || it.fir is FirResolvedNamedReference }?.fir
194
+ // if (found == null) {
195
+ // // Review how to expose unmatched types without causing an error.
196
+ // }
197
+ found
198
+ }
199
+ is KtPropertyAccessor -> {
200
+ val found = directFirInfos.firstOrNull { it.fir is FirDefaultPropertySetter }?.fir
201
+ // if (found == null) {
202
+ // // Review how to expose unmatched types without causing an error.
203
+ // }
204
+ found
205
+ }
206
+ else -> {
207
+ directFirInfos[0 ].fir
208
+ }
209
+ }
210
+ }
211
+ }
177
212
else if (allFirInfos.isNotEmpty()) {
178
213
return when {
179
214
allFirInfos.size == 1 -> allFirInfos[0 ].fir
0 commit comments