[Analysis API] cleanup code
This commit is contained in:
+7
-2
@@ -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")
|
||||
}
|
||||
+4
-10
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
+13
-9
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user