FIR IDE: change destructing declaration reference behaviour to match FE1.0 plugin behaviour

This commit is contained in:
Ilya Kirillov
2021-08-10 18:42:03 +02:00
parent aef45f0997
commit 2e3b576342
5 changed files with 25 additions and 9 deletions
@@ -20,6 +20,10 @@ abstract class KtDestructuringDeclarationReference(
abstract override fun canRename(): Boolean
override fun resolve() = multiResolve(false).asSequence()
.map { it.element }
.first { it is KtDestructuringDeclarationEntry }
override fun handleElementRename(newElementName: String): PsiElement? {
if (canRename()) return expression
throw IncorrectOperationException()
@@ -19,10 +19,7 @@ import org.jetbrains.kotlin.idea.frontend.api.fir.utils.firRef
import org.jetbrains.kotlin.idea.frontend.api.symbols.*
import org.jetbrains.kotlin.idea.frontend.api.symbols.markers.KtSymbolKind
import org.jetbrains.kotlin.idea.frontend.api.symbols.markers.KtSymbolWithKind
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtObjectLiteralExpression
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.*
internal class KtFirSymbolContainingDeclarationProvider(
override val analysisSession: KtFirAnalysisSession,
@@ -81,13 +78,19 @@ internal class KtFirSymbolContainingDeclarationProvider(
}
return when (symbol.origin) {
KtSymbolOrigin.SOURCE -> thisSource.parentOfType()
KtSymbolOrigin.SOURCE -> thisSource.getContainingKtDeclaration()
?: error("Containing declaration should present for non-toplevel declaration")
KtSymbolOrigin.SOURCE_MEMBER_GENERATED -> thisSource as KtDeclaration
else -> error("Unsupported declaration origin ${symbol.origin}")
}
}
private fun PsiElement.getContainingKtDeclaration(): KtDeclaration? =
when (val container = this.parentOfType<KtDeclaration>()) {
is KtDestructuringDeclaration -> container.parentOfType()
else -> container
}
private fun getContainingDeclarationForLibrarySymbol(symbol: KtSymbolWithKind): KtSymbolWithKind = with(analysisSession) {
require(symbol.origin == KtSymbolOrigin.LIBRARY || symbol.origin == KtSymbolOrigin.JAVA)
check(symbol.symbolKind == KtSymbolKind.MEMBER)
@@ -23,7 +23,14 @@ class KtFirDestructuringDeclarationReference(
override fun KtAnalysisSession.resolveToSymbols(): Collection<KtSymbol> {
check(this is KtFirAnalysisSession)
val fir = expression.getOrBuildFirSafe<FirProperty>(firResolveState) ?: return emptyList()
val componentFunctionSymbol = (fir.initializer as? FirComponentCall)?.getCalleeSymbol() ?: return emptyList()
return listOfNotNull(componentFunctionSymbol.fir.buildSymbol(firSymbolBuilder))
return listOfNotNull(
fir.buildSymbol(firSymbolBuilder),
getComponentNSymbol(fir)
)
}
private fun KtFirAnalysisSession.getComponentNSymbol(fir: FirProperty): KtSymbol? {
val componentFunctionSymbol = (fir.initializer as? FirComponentCall)?.getCalleeSymbol() ?: return null
return componentFunctionSymbol.fir.buildSymbol(firSymbolBuilder)
}
}
@@ -1,2 +1,3 @@
Resolved to:
0: (in a) operator fun a.A.component2(): kotlin.Int
0: (in <local>: main) val b: kotlin.Int
1: (in a) operator fun a.A.component2(): kotlin.Int
@@ -1,2 +1,3 @@
Resolved to:
0: (in a.MyPair) operator fun component2(): kotlin.Int
0: (in <local>: main) val b: kotlin.Int
1: (in a.MyPair) operator fun component2(): kotlin.Int