[Analysis API] cleanup code

This commit is contained in:
Ilya Kirillov
2022-06-15 17:27:57 +02:00
parent 37b0c20879
commit d716c68f0e
8 changed files with 58 additions and 56 deletions
@@ -11,9 +11,14 @@ import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
inline fun <reified S : KtSymbol> KtAnalysisSession.getSymbolByName(scope: KtElement, name: String): S {
inline fun <reified S : KtSymbol> KtAnalysisSession.getSymbolByNameSafe(scope: KtElement, name: String): S? {
return scope.collectDescendantsOfType<KtDeclaration> { it.name == name }
.map { it.getSymbol() }
.filterIsInstance<S>()
.single()
.singleOrNull()
}
inline fun <reified S : KtSymbol> KtAnalysisSession.getSymbolByName(scope: KtElement, name: String): S {
return getSymbolByNameSafe(scope, name)
?: error("Symbol with $name was not found in scope")
}
@@ -8,12 +8,11 @@ package org.jetbrains.kotlin.analysis.test.framework.services
import com.intellij.psi.PsiComment
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.components.buildSubstitutor
import org.jetbrains.kotlin.analysis.api.symbols.KtTypeParameterSymbol
import org.jetbrains.kotlin.analysis.api.types.KtSubstitutor
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtTypeParameter
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
object SubstitutionParser {
context(KtAnalysisSession)
@@ -35,16 +34,11 @@ object SubstitutionParser {
check(directivesAsString.startsWith(SUBSTITUTOR_PREFIX))
val substitutorAsMap = parseSubstitutions(directivesAsString.removePrefix(SUBSTITUTOR_PREFIX))
val allTypeParameterSymbols = scopeForTypeParameters
.collectDescendantsOfType<KtTypeParameter>()
.map { it.getTypeParameterSymbol() }
.groupBy { it.name.asString() }
.mapValues { it.value.single() }
return buildSubstitutor {
substitutorAsMap.forEach { (typeParameterName, typeString) ->
val typeParameterSymbol = allTypeParameterSymbols.getValue(typeParameterName)
val type = TypeParser.parseTypeFromString(typeString, scopeForTypeParameters, allTypeParameterSymbols)
val typeParameterSymbol = getSymbolByNameSafe<KtTypeParameterSymbol>(scopeForTypeParameters, typeParameterName)
?: error("Type parameter with name $typeParameterName was not found")
val type = TypeParser.parseTypeFromString(typeString, scopeForTypeParameters, scopeForTypeParameters)
substitution(typeParameterSymbol, type)
}
}
@@ -19,25 +19,29 @@ object TypeParser {
fun parseTypeFromString(
stringType: String,
contextElement: KtElement,
typeParameterByName: Map<String, KtTypeParameterSymbol>
scopeForTypeParameters: KtElement,
): KtType {
val type = KtPsiFactory(contextElement).createType(stringType)
return convertType(type.typeElement ?: incorrectType(type), typeParameterByName)
return convertType(type.typeElement ?: incorrectType(type), scopeForTypeParameters)
}
context (KtAnalysisSession)
private fun convertType(type: KtTypeElement, typeParameterByName: Map<String, KtTypeParameterSymbol>): KtType =
private fun convertType(type: KtTypeElement, scopeForTypeParameters: KtElement): KtType =
when (type) {
is KtUserType -> {
val qualifier = fullQualifier(type)
if (qualifier in typeParameterByName) {
buildTypeParameterType(typeParameterByName.getValue(qualifier))
} else {
buildClassType(ClassId.topLevel(FqName(qualifier))) {
type.typeArguments.forEach { argument ->
argument(convertType(argument.typeReference?.typeElement ?: incorrectType(type), typeParameterByName))
when (val typeParameter = getSymbolByNameSafe<KtTypeParameterSymbol>(scopeForTypeParameters, qualifier)) {
null -> {
buildClassType(ClassId.topLevel(FqName(qualifier))) {
type.typeArguments.forEach { argument ->
argument(convertType(argument.typeReference?.typeElement ?: incorrectType(type), scopeForTypeParameters))
}
}
}
else -> {
buildTypeParameterType(typeParameter)
}
}
}
else -> TODO(type::class.java.name)