KT-61890 [AA] Use ContextCollector in KtFirScopeProvider

^KT-61890 Fixed
This commit is contained in:
Roman Golyshev
2023-09-12 18:54:25 +02:00
committed by teamcity
parent 4f136cde31
commit 47a00bf97e
39 changed files with 2916 additions and 95 deletions
@@ -23,13 +23,16 @@ import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithMembers
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.LLFirResolveSession
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.getOrBuildFirFile
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.ContextCollector
import org.jetbrains.kotlin.analysis.utils.errors.unexpectedElementError
import org.jetbrains.kotlin.analysis.utils.printer.parentOfType
import org.jetbrains.kotlin.fir.declarations.FirClass
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
import org.jetbrains.kotlin.fir.declarations.utils.delegateFields
import org.jetbrains.kotlin.fir.java.JavaScopeProvider
import org.jetbrains.kotlin.fir.java.declarations.FirJavaClass
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.fir.resolve.SessionHolderImpl
import org.jetbrains.kotlin.fir.resolve.calls.FirSyntheticPropertiesScope
import org.jetbrains.kotlin.fir.resolve.scope
import org.jetbrains.kotlin.fir.resolve.scopeSessionKey
@@ -38,6 +41,7 @@ import org.jetbrains.kotlin.fir.scopes.impl.*
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhaseWithCallableMembers
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtElement
@@ -252,9 +256,22 @@ internal class KtFirScopeProvider(
originalFile: KtFile,
positionInFakeFile: KtElement
): KtScopeContext {
val fakeFile = positionInFakeFile.containingKtFile
// If the position is in KDoc, we want to pass the owning declaration to the ContextCollector.
// That way, the resulting scope will contain all the nested declarations which can be references by KDoc.
val parentKDoc = positionInFakeFile.parentOfType<KDoc>()
val correctedPosition = parentKDoc?.owner ?: positionInFakeFile
val context = ContextCollector.process(
fakeFile.getOrBuildFirFile(firResolveSession),
SessionHolderImpl(analysisSession.useSiteSession, getScopeSession()),
correctedPosition,
)
val towerDataContext =
analysisSession.firResolveSession.getTowerContextProvider(originalFile).getClosestAvailableParentContext(positionInFakeFile)
?: errorWithAttachment("Cannot find enclosing declaration for ${positionInFakeFile::class}") {
context?.towerDataContext
?: errorWithAttachment("Cannot find context for ${positionInFakeFile::class}") {
withPsiEntry("positionInFakeFile", positionInFakeFile)
}
val towerDataElementsIndexed = towerDataContext.towerDataElements.asReversed().withIndex()
@@ -52,6 +52,18 @@ public class FirIdeDependentAnalysisSourceModuleScopeContextForPositionTestGener
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/annotationWithoutDeclaration.kt");
}
@Test
@TestMetadata("classPropertyDelegate.kt")
public void testClassPropertyDelegate() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/classPropertyDelegate.kt");
}
@Test
@TestMetadata("classPropertyInitializer.kt")
public void testClassPropertyInitializer() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/classPropertyInitializer.kt");
}
@Test
@TestMetadata("contextReceiver.kt")
public void testContextReceiver() throws Exception {
@@ -142,6 +154,36 @@ public class FirIdeDependentAnalysisSourceModuleScopeContextForPositionTestGener
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/smartCastInWhenEntryCondition.kt");
}
@Test
@TestMetadata("superTypeConstructor.kt")
public void testSuperTypeConstructor() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/superTypeConstructor.kt");
}
@Test
@TestMetadata("superTypeConstructor_lambda.kt")
public void testSuperTypeConstructor_lambda() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/superTypeConstructor_lambda.kt");
}
@Test
@TestMetadata("superTypeConstructor_nestedClasses.kt")
public void testSuperTypeConstructor_nestedClasses() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/superTypeConstructor_nestedClasses.kt");
}
@Test
@TestMetadata("superTypeConstructor_nestedClasses_typeArgument.kt")
public void testSuperTypeConstructor_nestedClasses_typeArgument() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/superTypeConstructor_nestedClasses_typeArgument.kt");
}
@Test
@TestMetadata("superTypeDelegate.kt")
public void testSuperTypeDelegate() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/superTypeDelegate.kt");
}
@Test
@TestMetadata("syntheticPropertiesScope.kt")
public void testSyntheticPropertiesScope() throws Exception {
@@ -52,6 +52,18 @@ public class FirIdeNormalAnalysisSourceModuleScopeContextForPositionTestGenerate
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/annotationWithoutDeclaration.kt");
}
@Test
@TestMetadata("classPropertyDelegate.kt")
public void testClassPropertyDelegate() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/classPropertyDelegate.kt");
}
@Test
@TestMetadata("classPropertyInitializer.kt")
public void testClassPropertyInitializer() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/classPropertyInitializer.kt");
}
@Test
@TestMetadata("contextReceiver.kt")
public void testContextReceiver() throws Exception {
@@ -142,6 +154,36 @@ public class FirIdeNormalAnalysisSourceModuleScopeContextForPositionTestGenerate
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/smartCastInWhenEntryCondition.kt");
}
@Test
@TestMetadata("superTypeConstructor.kt")
public void testSuperTypeConstructor() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/superTypeConstructor.kt");
}
@Test
@TestMetadata("superTypeConstructor_lambda.kt")
public void testSuperTypeConstructor_lambda() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/superTypeConstructor_lambda.kt");
}
@Test
@TestMetadata("superTypeConstructor_nestedClasses.kt")
public void testSuperTypeConstructor_nestedClasses() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/superTypeConstructor_nestedClasses.kt");
}
@Test
@TestMetadata("superTypeConstructor_nestedClasses_typeArgument.kt")
public void testSuperTypeConstructor_nestedClasses_typeArgument() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/superTypeConstructor_nestedClasses_typeArgument.kt");
}
@Test
@TestMetadata("superTypeDelegate.kt")
public void testSuperTypeDelegate() throws Exception {
runTest("analysis/analysis-api/testData/components/scopeProvider/scopeContextForPosition/superTypeDelegate.kt");
}
@Test
@TestMetadata("syntheticPropertiesScope.kt")
public void testSyntheticPropertiesScope() throws Exception {