|
1 | 1 | package godot.intellij.plugin.extension |
2 | 2 |
|
3 | 3 | import com.intellij.psi.PsiAnnotation |
| 4 | +import com.intellij.psi.PsiClass |
4 | 5 | import com.intellij.psi.PsiElement |
| 6 | +import com.intellij.psi.PsiField |
| 7 | +import com.intellij.psi.PsiMethod |
5 | 8 | import com.intellij.psi.PsiModifierListOwner |
| 9 | +import godot.intellij.plugin.data.model.GODOT_API_MEMBER_ANNOTATION |
| 10 | +import godot.intellij.plugin.data.model.GODOT_MEMBER_ANNOTATION |
6 | 11 | import godot.tools.common.constants.GodotKotlinJvmTypes |
7 | 12 | import godot.tools.common.constants.godotAnnotationPackage |
| 13 | +import org.jetbrains.kotlin.descriptors.ClassDescriptor |
| 14 | +import org.jetbrains.kotlin.descriptors.FunctionDescriptor |
| 15 | +import org.jetbrains.kotlin.descriptors.PropertyDescriptor |
| 16 | +import org.jetbrains.kotlin.idea.caches.resolve.analyze |
| 17 | +import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor |
| 18 | +import org.jetbrains.kotlin.name.FqName |
| 19 | +import org.jetbrains.kotlin.psi.KtAnnotated |
| 20 | +import org.jetbrains.kotlin.psi.KtAnnotationEntry |
| 21 | +import org.jetbrains.kotlin.psi.KtClass |
| 22 | +import org.jetbrains.kotlin.psi.KtElement |
| 23 | +import org.jetbrains.kotlin.psi.KtFunction |
| 24 | +import org.jetbrains.kotlin.psi.KtProperty |
| 25 | +import org.jetbrains.kotlin.resolve.BindingContext |
| 26 | +import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils.descriptorToDeclaration |
| 27 | +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode |
8 | 28 |
|
9 | 29 |
|
10 | 30 | fun PsiElement.isRegistered(): Boolean { |
11 | | - if (this !is PsiModifierListOwner) return false |
| 31 | + return when (this) { |
| 32 | + is KtAnnotated -> when (this) { |
| 33 | + is KtClass -> hasRegistrationAnnotation() |
| 34 | + is KtProperty -> hasRegistrationAnnotation() || isSignal() || overridesRegistered() |
| 35 | + is KtFunction -> hasRegistrationAnnotation() || overridesGodotApiFunction() || overridesRegistered() |
| 36 | + else -> false |
| 37 | + } |
| 38 | + |
| 39 | + is PsiModifierListOwner -> when(this) { |
| 40 | + is PsiClass -> hasRegistrationAnnotation() |
| 41 | + is PsiField -> hasRegistrationAnnotation() || isSignal() || overridesRegistered() |
| 42 | + is PsiMethod -> hasRegistrationAnnotation() || overridesGodotApiFunction() || overridesRegistered() |
| 43 | + else -> false |
| 44 | + } |
| 45 | + |
| 46 | + else -> return false |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +private fun KtProperty.overridesRegistered(): Boolean { |
| 51 | + return (this.descriptor as? PropertyDescriptor)?.overriddenDescriptors?.any { overriddenDescriptor -> |
| 52 | + overriddenDescriptor.annotations.hasAnnotation(FqName(GODOT_API_MEMBER_ANNOTATION)) |
| 53 | + } ?: false |
| 54 | +} |
| 55 | + |
| 56 | +private fun KtFunction.overridesRegistered(): Boolean { |
| 57 | + return (this.descriptor as? FunctionDescriptor)?.overriddenDescriptors?.any { overriddenDescriptor -> |
| 58 | + overriddenDescriptor.annotations.hasAnnotation(FqName(GODOT_MEMBER_ANNOTATION)) |
| 59 | + } ?: false |
| 60 | +} |
| 61 | + |
| 62 | +private fun KtAnnotated.hasRegistrationAnnotation(): Boolean { |
| 63 | + return this.resolveAllAnnotationsRecursively().any { |
| 64 | + it.fqName?.asString() == "$godotAnnotationPackage.${GodotKotlinJvmTypes.Annotations.godotMember}" |
| 65 | + || it.fqName?.asString() == "$godotAnnotationPackage.${GodotKotlinJvmTypes.Annotations.godotScript}" |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +private fun KtFunction.overridesGodotApiFunction(): Boolean { |
| 70 | + return (this.descriptor as? FunctionDescriptor)?.overriddenDescriptors?.any { overriddenDescriptor -> |
| 71 | + overriddenDescriptor.annotations.hasAnnotation(FqName(GODOT_API_MEMBER_ANNOTATION)) |
| 72 | + } ?: false |
| 73 | +} |
| 74 | + |
| 75 | +private fun KtAnnotated.resolveAllAnnotationsRecursively(): List<KtClass> { |
| 76 | + return annotationEntries.mapNotNull { it.toAnnotationClass()?.first }.flatMap { listOf(it) + it.resolveAllAnnotationsRecursively() } |
| 77 | +} |
| 78 | + |
| 79 | +private fun KtAnnotationEntry.toAnnotationClass(): Pair<KtClass, ClassDescriptor>? { |
| 80 | + val context = (this as KtElement).analyze(BodyResolveMode.PARTIAL) |
| 81 | + val annotationDescriptor = context.get(BindingContext.ANNOTATION, this) ?: return null |
| 82 | + |
| 83 | + val classDescriptor = annotationDescriptor.type.constructor.declarationDescriptor as? ClassDescriptor ?: return null |
| 84 | + val ktClass = descriptorToDeclaration(classDescriptor) as? KtClass ?: return null |
| 85 | + |
| 86 | + return if (ktClass.isAnnotation() && ktClass.isWritable) { |
| 87 | + ktClass to classDescriptor |
| 88 | + } else { |
| 89 | + null |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +private fun PsiModifierListOwner.hasRegistrationAnnotation(): Boolean { |
12 | 94 | return this.annotations.any { it.isRegistrationAnnotation() } |
13 | 95 | } |
14 | 96 |
|
15 | | -private fun PsiAnnotation.isRegistrationAnnotation(): Boolean { |
| 97 | +private fun PsiAnnotation.isRegistrationAnnotation(alreadyResolvedAnnotations: MutableList<PsiAnnotation> = mutableListOf()): Boolean { |
16 | 98 | return this.qualifiedName == "$godotAnnotationPackage.${GodotKotlinJvmTypes.Annotations.godotScript}" || |
17 | 99 | this.qualifiedName == "$godotAnnotationPackage.${GodotKotlinJvmTypes.Annotations.godotMember}" || |
18 | | - this.resolveAnnotationType()?.annotations?.any { it.isRegistrationAnnotation() } == true |
| 100 | + run { |
| 101 | + val annotationsOnAnnotation = this.resolveAnnotationType()?.annotations ?: return false |
| 102 | + annotationsOnAnnotation |
| 103 | + .filter { it !in alreadyResolvedAnnotations } |
| 104 | + .any { it.isRegistrationAnnotation(alreadyResolvedAnnotations.apply { addAll(annotationsOnAnnotation) }) } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +private fun PsiField.getOverriddenFields(): List<PsiField> { |
| 109 | + val overriddenFields = mutableListOf<PsiField>() |
| 110 | + val containingClass = this.containingClass ?: return overriddenFields |
| 111 | + |
| 112 | + var superClass: PsiClass? = containingClass.superClass |
| 113 | + |
| 114 | + while (superClass != null) { |
| 115 | + for (superField in superClass.fields) { |
| 116 | + if (this.name == superField.name && this.type.isAssignableFrom(superField.type)) { |
| 117 | + overriddenFields.add(superField) |
| 118 | + } |
| 119 | + } |
| 120 | + superClass = superClass.superClass |
| 121 | + } |
| 122 | + |
| 123 | + return overriddenFields |
| 124 | +} |
| 125 | + |
| 126 | +private fun PsiField.overridesRegistered(): Boolean { |
| 127 | + return getOverriddenFields().any { it.isRegistered() } |
| 128 | +} |
| 129 | + |
| 130 | +private fun PsiMethod.overridesRegistered(): Boolean { |
| 131 | + val superMethods = this.findSuperMethods() |
| 132 | + |
| 133 | + return superMethods.any { it.isRegistered() } |
| 134 | +} |
| 135 | + |
| 136 | +private fun PsiMethod.overridesGodotApiFunction(): Boolean { |
| 137 | + val superMethods = this.findSuperMethods() |
| 138 | + |
| 139 | + return superMethods.any { superMethod -> |
| 140 | + superMethod.annotations.any { superMethodAnnotation -> superMethodAnnotation.qualifiedName == GODOT_API_MEMBER_ANNOTATION } |
| 141 | + } |
19 | 142 | } |
0 commit comments