Skip to content

Commit d16d538

Browse files
committed
Cleanup code
1 parent 4d7cad2 commit d16d538

File tree

7 files changed

+76
-86
lines changed

7 files changed

+76
-86
lines changed

kt/godot-library/src/main/kotlin/godot/annotation/GodotScript.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ package godot.annotation
55
*
66
* @param customName Register the class name with this string rather than with `fully_qualified_ClassName`. **Note:** If this class name is already registered, a compilation error will be thrown! So make sure your custom class name is unique!
77
*/
8-
@Target(AnnotationTarget.CLASS)
8+
@Target(
9+
AnnotationTarget.CLASS,
10+
AnnotationTarget.ANNOTATION_CLASS
11+
)
912
@Retention(AnnotationRetention.RUNTIME)
1013
annotation class GodotScript(val customName: String = "")

kt/godot-library/src/main/kotlin/godot/annotation/Tool.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ package godot.annotation
88
*/
99
@Target(AnnotationTarget.CLASS)
1010
@Retention(AnnotationRetention.RUNTIME)
11+
@GodotScript
1112
annotation class Tool

kt/plugins/godot-intellij-plugin/src/main/kotlin/godot/intellij/plugin/annotator/clazz/RegisterClassAnnotator.kt

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,32 @@ import com.intellij.psi.PsiElement
66
import godot.intellij.plugin.GodotPluginBundle
77
import godot.intellij.plugin.annotator.base.BaseAnnotator
88
import godot.intellij.plugin.annotator.general.checkNotGeneric
9-
import godot.intellij.plugin.data.model.GODOT_MEMBER_ANNOTATION
109
import godot.intellij.plugin.data.model.GODOT_SCRIPT_ANNOTATION
11-
import godot.intellij.plugin.data.model.TOOL_ANNOTATION
12-
import godot.intellij.plugin.extension.anyFunctionHasAnnotation
13-
import godot.intellij.plugin.extension.anyPropertyHasAnnotation
10+
import godot.intellij.plugin.extension.extendsGodotType
1411
import godot.intellij.plugin.extension.getRegisteredClassName
12+
import godot.intellij.plugin.extension.hasDefaultConstructor
1513
import godot.intellij.plugin.extension.isAbstract
14+
import godot.intellij.plugin.extension.isAnyMemberRegistered
1615
import godot.intellij.plugin.extension.isRegistered
1716
import godot.intellij.plugin.extension.registerProblem
1817
import godot.intellij.plugin.extension.registeredClassNameCache
19-
import godot.intellij.plugin.extension.resolveToDescriptor
2018
import godot.intellij.plugin.quickfix.ClassAlreadyRegisteredQuickFix
2119
import godot.intellij.plugin.quickfix.ClassNotRegisteredQuickFix
2220
import godot.tools.common.constants.Constraints
23-
import godot.tools.common.constants.GodotKotlinJvmTypes
24-
import godot.tools.common.constants.godotCorePackage
2521
import org.jetbrains.kotlin.idea.base.util.module
26-
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
27-
import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperclassesWithoutAny
2822

2923
class RegisterClassAnnotator : BaseAnnotator {
3024
private val classNotRegisteredQuickFix by lazy { ClassNotRegisteredQuickFix() }
3125

3226
override fun checkElement(element: PsiElement, holder: AnnotationHolder) {
3327
when(element) {
3428
is PsiClass -> {
35-
if (!element.isRegistered() || element.isAnnotationType) {
29+
if (!element.isRegistered()) {
3630
val errorLocation = element.nameIdentifier ?: element.navigationElement
37-
if (element.getAnnotation(TOOL_ANNOTATION) != null) {
38-
holder.registerProblem(
39-
GodotPluginBundle.message("problem.class.notRegistered.butHasToolAnnotation"),
40-
errorLocation,
41-
classNotRegisteredQuickFix
42-
)
43-
}
44-
if (!element.isAbstract && element.anyPropertyHasAnnotation(GODOT_MEMBER_ANNOTATION)) {
45-
holder.registerProblem(
46-
GodotPluginBundle.message("problem.class.notRegistered.properties"),
47-
errorLocation,
48-
classNotRegisteredQuickFix
49-
)
50-
}
51-
if (!element.isAbstract && element.anyPropertyHasAnnotation(GODOT_MEMBER_ANNOTATION)) {
52-
holder.registerProblem(
53-
GodotPluginBundle.message("problem.class.notRegistered.signals"),
54-
errorLocation,
55-
classNotRegisteredQuickFix
56-
)
57-
}
58-
if (!element.isAbstract && element.anyFunctionHasAnnotation(GODOT_MEMBER_ANNOTATION)) {
31+
32+
if (!element.isAbstract && element.isAnyMemberRegistered()) {
5933
holder.registerProblem(
60-
GodotPluginBundle.message("problem.class.notRegistered.functions"),
34+
GodotPluginBundle.message("problem.class.notRegistered.members"),
6135
errorLocation,
6236
classNotRegisteredQuickFix
6337
)
@@ -75,17 +49,16 @@ class RegisterClassAnnotator : BaseAnnotator {
7549
}
7650

7751
private fun checkExtendsGodotType(psiClass: PsiClass, holder: AnnotationHolder) {
78-
if (psiClass.resolveToDescriptor()?.getAllSuperclassesWithoutAny()?.any { it.fqNameSafe.asString() == "$godotCorePackage.${GodotKotlinJvmTypes.ktObject}" } != true) {
52+
if (!psiClass.extendsGodotType()) {
7953
holder.registerProblem(
80-
GodotPluginBundle.message("problem.class.inheritance.notInheritingGodotObject"),
81-
psiClass.nameIdentifier
82-
?: psiClass.navigationElement
54+
message = GodotPluginBundle.message("problem.class.inheritance.notInheritingGodotObject"),
55+
errorLocation = psiClass.nameIdentifier ?: psiClass.navigationElement
8356
)
8457
}
8558
}
8659

8760
private fun checkDefaultConstructorExistence(psiClass: PsiClass, holder: AnnotationHolder) {
88-
if (psiClass.constructors.isNotEmpty() && psiClass.constructors.filter { constructor -> !constructor.hasParameters() }.size != 1) {
61+
if (!psiClass.hasDefaultConstructor()) {
8962
// TODO: create quick fix (not trivial to create a secondary constructor from a primary one, i failed miserably)
9063
holder.registerProblem(
9164
GodotPluginBundle.message("problem.class.constructor.defaultConstructorMissing"),
@@ -98,7 +71,7 @@ class RegisterClassAnnotator : BaseAnnotator {
9871
private fun checkConstructorParameterCount(psiClass: PsiClass, holder: AnnotationHolder) {
9972
psiClass
10073
.constructors
101-
.filter { it.getAnnotation(GODOT_MEMBER_ANNOTATION) != null }
74+
.filter { it.isRegistered() }
10275
.forEach { ktConstructor ->
10376
if (ktConstructor.parameterList.parametersCount > Constraints.MAX_CONSTRUCTOR_ARG_COUNT) {
10477
holder.registerProblem(
@@ -112,10 +85,10 @@ class RegisterClassAnnotator : BaseAnnotator {
11285
private fun checkConstructorOverloading(psiClass: PsiClass, holder: AnnotationHolder) {
11386
val constructors = psiClass
11487
.constructors
115-
.filter { it.getAnnotation(GODOT_MEMBER_ANNOTATION) != null }
88+
.filter { it.isRegistered() }
11689

11790
val constructorsByArgCount = constructors
118-
.filter { it.getAnnotation(GODOT_MEMBER_ANNOTATION) != null }
91+
.filter { it.isRegistered() }
11992
.groupBy { it.parameterList.parametersCount }
12093

12194
if (constructorsByArgCount.size != constructors.size) {

kt/plugins/godot-intellij-plugin/src/main/kotlin/godot/intellij/plugin/annotator/property/RegisterPropertiesAnnotator.kt

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package godot.intellij.plugin.annotator.property
33
import com.intellij.lang.annotation.AnnotationHolder
44
import com.intellij.lang.annotation.Annotator
55
import com.intellij.psi.PsiElement
6+
import com.intellij.psi.PsiField
7+
import com.intellij.psi.PsiTypeParameterListOwner
68
import godot.intellij.plugin.GodotPluginBundle
79
import godot.intellij.plugin.annotator.general.checkNotGeneric
810
import godot.intellij.plugin.extension.isCoreType
@@ -37,26 +39,43 @@ class RegisterPropertiesAnnotator : Annotator {
3739
override fun annotate(element: PsiElement, holder: AnnotationHolder) {
3840
if (!element.isInGodotRoot()) return
3941

40-
if (element is KtProperty) {
41-
if (element.isRegistered()) {
42-
checkNotGeneric(element.toLightElements().firstIsInstance(), holder)
43-
checkMutability(element, holder)
44-
checkRegisteredType(element, holder)
45-
lateinitChecks(element, holder)
46-
nullableChecks(element, holder)
42+
when(element) {
43+
is KtProperty -> {
44+
if (element.isRegistered()) {
45+
checkNotGeneric(element.toLightElements().firstIsInstance(), holder)
46+
checkMutability(element, holder)
47+
checkRegisteredType(element, holder)
48+
lateinitChecks(element, holder)
49+
nullableChecks(element, holder)
50+
}
51+
// outside to check if the property is also registered
52+
propertyHintAnnotationChecker.checkPropertyHintAnnotations(element, holder)
53+
}
54+
is PsiField -> {
55+
if (element.isRegistered()) {
56+
(element as? PsiTypeParameterListOwner)?.let { checkNotGeneric(it, holder) }
57+
checkMutability(element, holder)
58+
}
4759
}
48-
// outside to check if the property is also registered
49-
propertyHintAnnotationChecker.checkPropertyHintAnnotations(element, holder)
5060
}
5161
}
5262

53-
private fun checkMutability(ktProperty: KtProperty, holder: AnnotationHolder) {
54-
if (!ktProperty.isVar) {
55-
holder.registerProblem(
56-
GodotPluginBundle.message("problem.property.mutability"),
57-
ktProperty.valOrVarKeyword,
58-
mutabilityQuickFix
59-
)
63+
private fun checkMutability(psiElement: PsiElement, holder: AnnotationHolder) {
64+
when(psiElement) {
65+
is KtProperty -> if (!psiElement.isVar) {
66+
holder.registerProblem(
67+
GodotPluginBundle.message("problem.property.mutability"),
68+
psiElement.valOrVarKeyword,
69+
mutabilityQuickFix
70+
)
71+
}
72+
is PsiField -> if (psiElement.modifierList?.hasModifierProperty("final") == true) {
73+
holder.registerProblem(
74+
GodotPluginBundle.message("problem.property.mutability"),
75+
psiElement.modifierList?.children?.firstOrNull { it.text.contains("final") } ?: psiElement
76+
)
77+
}
78+
else -> return
6079
}
6180
}
6281

kt/plugins/godot-intellij-plugin/src/main/kotlin/godot/intellij/plugin/extension/psiClass.kt

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
11
package godot.intellij.plugin.extension
22

33
import com.intellij.psi.PsiClass
4-
import com.intellij.psi.PsiMethod
54
import com.intellij.psi.PsiModifier
6-
import godot.intellij.plugin.data.model.GODOT_SCRIPT_ANNOTATION
7-
import org.jetbrains.kotlin.asJava.classes.KtUltraLightClass
5+
import godot.tools.common.constants.GodotKotlinJvmTypes
6+
import godot.tools.common.constants.godotCorePackage
87
import org.jetbrains.kotlin.descriptors.ClassDescriptor
98
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
109
import org.jetbrains.kotlin.idea.caches.resolve.util.getJavaClassDescriptor
11-
import org.jetbrains.kotlin.idea.util.findAnnotation
12-
import org.jetbrains.kotlin.name.FqName
1310
import org.jetbrains.kotlin.psi.KtClass
1411
import org.jetbrains.kotlin.psi.psiUtil.isAbstract
12+
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
13+
import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperclassesWithoutAny
1514

1615
val PsiClass.isAbstract: Boolean
1716
get() = when(this) {
1817
is KtClass -> isAbstract()
1918
else -> isInterface || modifierList?.hasModifierProperty(PsiModifier.ABSTRACT) ?: false
2019
}
2120

22-
fun PsiClass.anyFunctionHasAnnotation(annotationFqName: String) = this
23-
.methods
24-
.filterIsInstance<PsiMethod>()
25-
.any { declaration ->
26-
declaration.getAnnotation(annotationFqName) != null
27-
}
28-
29-
fun PsiClass.anyPropertyHasAnnotation(annotationFqName: String) = when(this) {
30-
is KtUltraLightClass -> (this.kotlinOrigin as? KtClass)?.getProperties()?.any { property -> property.findAnnotation(FqName(annotationFqName)) != null } == true
31-
else -> this
32-
.fields
33-
.any { declaration ->
34-
declaration.getAnnotation(annotationFqName) != null
35-
}
21+
fun PsiClass.isAnyMemberRegistered(): Boolean {
22+
return this.methods.any { it.isRegistered() } || this.methods.any { it.isRegistered() }
3623
}
3724

38-
val PsiClass.isRegistered: Boolean
39-
get() = getAnnotation(GODOT_SCRIPT_ANNOTATION) != null
25+
fun PsiClass.extendsGodotType(): Boolean {
26+
return this
27+
.resolveToDescriptor()
28+
?.getAllSuperclassesWithoutAny()
29+
?.any { it.fqNameSafe.asString() == "$godotCorePackage.${GodotKotlinJvmTypes.ktObject}" } == true
30+
}
4031

32+
fun PsiClass.hasDefaultConstructor(): Boolean {
33+
return when(this) {
34+
is KtClass -> this.constructors.any { it.parameters.isEmpty() }
35+
else -> this.constructors.isEmpty() || this.constructors.any { it.parameters.isEmpty() }
36+
}
37+
}
4138

42-
fun PsiClass.resolveToDescriptor(): ClassDescriptor? {
39+
private fun PsiClass.resolveToDescriptor(): ClassDescriptor? {
4340
return when(this) {
4441
is KtClass -> resolveToDescriptorIfAny()
4542
else -> getJavaClassDescriptor()

kt/plugins/godot-intellij-plugin/src/main/kotlin/godot/intellij/plugin/extension/psiElementExt.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
3030
fun PsiElement.isRegistered(): Boolean {
3131
return when (this) {
3232
is KtAnnotated -> when (this) {
33-
is KtClass -> hasRegistrationAnnotation()
33+
is KtClass -> hasRegistrationAnnotation() && !this.isAnnotation()
3434
is KtProperty -> hasRegistrationAnnotation() || isSignal() || overridesRegistered()
3535
is KtFunction -> hasRegistrationAnnotation() || overridesGodotApiFunction() || overridesRegistered()
3636
else -> false
3737
}
3838

3939
is PsiModifierListOwner -> when(this) {
40-
is PsiClass -> hasRegistrationAnnotation()
40+
is PsiClass -> hasRegistrationAnnotation() && !this.isAnnotationType
4141
is PsiField -> hasRegistrationAnnotation() || isSignal() || overridesRegistered()
4242
is PsiMethod -> hasRegistrationAnnotation() || overridesGodotApiFunction() || overridesRegistered()
4343
else -> false

kt/plugins/godot-intellij-plugin/src/main/resources/messages/generalLabels.properties

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ wizard.projectSettings.buildSettings.graalvm.windowsDeveloperVCVarsPath=WindowsD
2929
wizard.projectSettings.buildSettings.graalvm.windowsDeveloperVCVarsPath.browseDialogTitle=WindowsDeveloper VC vars path
3030
wizard.projectSettings.buildSettings.graalvm.windowsDeveloperVCVarsPath.comment=Absolute path to WindowsDeveloper VC vars path
3131
wizard.projectSettings.buildSettings.graalvm.isIOSEnabled=IOS enabled:
32-
problem.class.notRegistered.butHasToolAnnotation=This class is marked as tool class but not registered with @RegisterClass
33-
problem.class.notRegistered.properties=This class contains registered properties but is not registered
34-
problem.class.notRegistered.functions=This class contains registered functions but is not registered
35-
problem.class.notRegistered.signals=This class contains registered signals but is not registered
32+
problem.class.notRegistered.members=This class contains registered members but is not registered
3633
problem.class.inheritance.notInheritingGodotObject=Registered class not inheriting godot type
3734
problem.class.constructor.defaultConstructorMissing=There is no default constructor defined. Godot needs a default constructor (no args)
3835
problem.class.constructor.toManyParams=Godot cannot handle constructors for registered classes with more than {0} parameters. Reduce your parameter count

0 commit comments

Comments
 (0)