[Analysis API] rework containing declaration provider

now it should work for non-source declarations
This commit is contained in:
Ilya Kirillov
2022-10-14 15:31:01 +02:00
committed by teamcity
parent 2cf8f75a90
commit 97df0a0902
25 changed files with 807 additions and 118 deletions
@@ -0,0 +1,65 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.containingDeclarationProvider
import org.jetbrains.kotlin.analysis.api.components.KtDeclarationRendererOptions
import org.jetbrains.kotlin.analysis.api.components.KtTypeRendererOptions
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtClassLikeSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtNamedClassOrObjectSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbol
import org.jetbrains.kotlin.analysis.test.framework.base.AbstractAnalysisApiSingleFileTest
import org.jetbrains.kotlin.analysis.test.framework.services.expressionMarkerProvider
import org.jetbrains.kotlin.analysis.utils.printer.prettyPrint
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.assertions
abstract class AbstractContainingDeclarationProviderByMemberScopeTest : AbstractAnalysisApiSingleFileTest() {
override fun doTestByFileStructure(ktFile: KtFile, module: TestModule, testServices: TestServices) {
val declaration = testServices.expressionMarkerProvider.getElementOfTypeAtCaret<KtClassOrObject>(ktFile)
val memberToContainingClass = analyseForTest(declaration) {
val symbol = declaration.getClassOrObjectSymbol()
prettyPrint {
printCollection(symbol.getMemberScope().getAllSymbols().toList(), separator = "\n\n") { symbol ->
val containingDeclaration = symbol.getContainingSymbol() as KtClassLikeSymbol
append(symbol.render(renderingOptions))
append(" fromClass ")
append(containingDeclaration.classIdIfNonLocal?.asString())
if (symbol.typeParameters.isNotEmpty()) {
appendLine()
withIndent {
printCollection(symbol.typeParameters, separator = "\n") { typeParameter ->
val containingDeclarationForTypeParameter = typeParameter.getContainingSymbol()
append(typeParameter.render(renderingOptions))
append(" from ")
append(containingDeclarationForTypeParameter?.qualifiedNameString())
}
}
}
}
}
}
testServices.assertions.assertEqualsToTestDataFileSibling(memberToContainingClass)
}
private fun KtSymbol.qualifiedNameString() = when (this) {
is KtClassLikeSymbol -> classIdIfNonLocal!!.asString()
is KtCallableSymbol -> callableIdIfNonLocal!!.toString()
else -> error("unknown symbol $this")
}
companion object {
val renderingOptions = KtDeclarationRendererOptions.DEFAULT.copy(
typeRendererOptions = KtTypeRendererOptions.SHORT_NAMES,
modifiers = emptySet()
)
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.containingDeclarationProvider
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.analysis.test.framework.base.AbstractAnalysisApiSingleFileTest
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtVisitorVoid
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.assertions
abstract class AbstractContainingDeclarationProviderByPsiTest : AbstractAnalysisApiSingleFileTest() {
override fun doTestByFileStructure(ktFile: KtFile, module: TestModule, testServices: TestServices) {
val currentPath = mutableListOf<KtDeclaration>()
analyseForTest(ktFile.declarations.first()) {
ktFile.accept(object : KtVisitorVoid() {
override fun visitElement(element: PsiElement) {
element.acceptChildren(this)
}
override fun visitDeclaration(dcl: KtDeclaration) {
val parentDeclaration = currentPath.lastOrNull()
val currentDeclarationSymbol = dcl.getSymbol()
val expectedParentDeclarationSymbol = parentDeclaration?.getSymbol()
val actualParentDeclarationSymbol = currentDeclarationSymbol.getContainingSymbol()
testServices.assertions.assertEquals(expectedParentDeclarationSymbol, actualParentDeclarationSymbol) {
"Invalid parent declaration for $currentDeclarationSymbol, expected $expectedParentDeclarationSymbol but $actualParentDeclarationSymbol found"
}
currentPath.add(dcl)
super.visitDeclaration(dcl)
currentPath.removeLast()
}
})
}
}
}