Skip to content

Commit a0247cf

Browse files
authored
Make ide plugin k2 compatible (#777)
1 parent cc5f4d1 commit a0247cf

31 files changed

Lines changed: 336 additions & 321 deletions

kt/plugins/godot-intellij-plugin/build.gradle.kts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1+
import org.jetbrains.changelog.Changelog
12
import org.jetbrains.changelog.markdownToHTML
2-
import versioninfo.intellij.BuildConfig
3-
import versioninfo.intellij.VersionRange
43
import versioninfo.fullGodotKotlinJvmVersion
54
import versioninfo.isSnapshot
6-
import org.jetbrains.changelog.Changelog
75

86
plugins {
97
// Java support
@@ -63,6 +61,10 @@ intellijPlatform.pluginVerification.ides.ide(intellijVersion)
6361
tasks {
6462
runIde {
6563
jvmArgs("-Xmx2000m")
64+
65+
jvmArgumentProviders += CommandLineArgumentProvider {
66+
listOf("-Didea.kotlin.plugin.use.k2=true")
67+
}
6668
}
6769

6870
patchPluginXml {
@@ -111,6 +113,13 @@ tasks {
111113
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
112114
compilerOptions {
113115
freeCompilerArgs.add("-Xjvm-default=all")
116+
freeCompilerArgs.add("-Xcontext-receivers") // We have to add context receiver support because the analysis api from k2 is using context receivers...
117+
}
118+
}
119+
120+
test {
121+
jvmArgumentProviders += CommandLineArgumentProvider {
122+
listOf("-Didea.kotlin.plugin.use.k2=true")
114123
}
115124
}
116125
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package godot.intellij.plugin.annotator.clazz
33
import com.intellij.lang.annotation.AnnotationHolder
44
import com.intellij.psi.PsiClass
55
import com.intellij.psi.PsiElement
6+
import godot.common.constants.Constraints
67
import godot.intellij.plugin.GodotPluginBundle
78
import godot.intellij.plugin.annotator.base.BaseAnnotator
89
import godot.intellij.plugin.annotator.general.checkNotGeneric
@@ -14,19 +15,17 @@ import godot.intellij.plugin.data.model.REGISTER_SIGNAL_ANNOTATION
1415
import godot.intellij.plugin.data.model.TOOL_ANNOTATION
1516
import godot.intellij.plugin.extension.anyFunctionHasAnnotation
1617
import godot.intellij.plugin.extension.anyPropertyHasAnnotation
18+
import godot.intellij.plugin.extension.asClassId
1719
import godot.intellij.plugin.extension.getRegisteredClassName
1820
import godot.intellij.plugin.extension.isAbstract
21+
import godot.intellij.plugin.extension.isOrInheritsType
1922
import godot.intellij.plugin.extension.registerProblem
2023
import godot.intellij.plugin.extension.registeredClassNameCache
21-
import godot.intellij.plugin.extension.resolveToDescriptor
2224
import godot.intellij.plugin.quickfix.ClassAlreadyRegisteredQuickFix
2325
import godot.intellij.plugin.quickfix.ClassNotRegisteredQuickFix
24-
import godot.common.constants.Constraints
2526
import godot.tools.common.constants.GodotKotlinJvmTypes
2627
import godot.tools.common.constants.godotCorePackage
2728
import org.jetbrains.kotlin.idea.base.util.module
28-
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
29-
import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperclassesWithoutAny
3029

3130
class RegisterClassAnnotator : BaseAnnotator {
3231
private val classNotRegisteredQuickFix by lazy { ClassNotRegisteredQuickFix() }
@@ -77,7 +76,7 @@ class RegisterClassAnnotator : BaseAnnotator {
7776
}
7877

7978
private fun checkExtendsGodotType(psiClass: PsiClass, holder: AnnotationHolder) {
80-
if (psiClass.resolveToDescriptor()?.getAllSuperclassesWithoutAny()?.any { it.fqNameSafe.asString() == "$godotCorePackage.${GodotKotlinJvmTypes.ktObject}" } != true) {
79+
if (!psiClass.isOrInheritsType(asClassId("$godotCorePackage.${GodotKotlinJvmTypes.ktObject}"))) {
8180
holder.registerProblem(
8281
GodotPluginBundle.message("problem.class.inheritance.notInheritingGodotObject"),
8382
psiClass.nameIdentifier

kt/plugins/godot-intellij-plugin/src/main/kotlin/godot/intellij/plugin/annotator/copy/CopyModificationAnnotator.kt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ package godot.intellij.plugin.annotator.copy
33
import com.intellij.codeInspection.ProblemHighlightType
44
import com.intellij.lang.annotation.AnnotationHolder
55
import com.intellij.lang.annotation.Annotator
6+
import com.intellij.psi.PsiAnnotationOwner
67
import com.intellij.psi.PsiElement
78
import godot.intellij.plugin.GodotPluginBundle
89
import godot.intellij.plugin.data.model.CORE_TYPE_LOCAL_COPY_ANNOTATION
910
import godot.intellij.plugin.extension.isInGodotRoot
1011
import godot.intellij.plugin.extension.registerProblem
11-
import org.jetbrains.kotlin.idea.caches.resolve.analyze
1212
import org.jetbrains.kotlin.idea.references.mainReference
13-
import org.jetbrains.kotlin.idea.references.resolveToDescriptors
1413
import org.jetbrains.kotlin.lexer.KtSingleValueToken
1514
import org.jetbrains.kotlin.psi.KtBinaryExpression
1615
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
17-
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
1816

1917
class CopyModificationAnnotator : Annotator {
2018
private val singleValueTokensToCheck = listOf("=", "+=", "-=", "*=", "/=", "%=")
@@ -112,18 +110,13 @@ class CopyModificationAnnotator : Annotator {
112110

113111
// if the receiver is a dot expression, we want to check it's selector for a local copy annotation, in any other case we check the main reference
114112
// case for dot expression: `basis.[x].x` <- the [x] is the selector of the dot expression [basis.x].[x] we care about
115-
val receiverReferenceToCheck =
116-
((receiverExpression as? KtDotQualifiedExpression)?.selectorExpression?.mainReference)
117-
?: receiverExpression.mainReference
113+
val receiverReferenceToCheck = ((receiverExpression as? KtDotQualifiedExpression)?.selectorExpression) ?: receiverExpression
118114

119-
// annotations on the receiver expression's reference (usually a property of an object)
120-
val receiverAnnotations = receiverReferenceToCheck
121-
?.resolveToDescriptors(selectorExpression.analyze(BodyResolveMode.PARTIAL))
122-
?.flatMap { declarationDescriptor -> declarationDescriptor.annotations }
123-
?.mapNotNull { annotationDescriptor -> annotationDescriptor.fqName?.asString() }
124-
?: emptyList()
115+
val annotationOwner = receiverReferenceToCheck.mainReference?.resolve() as? PsiAnnotationOwner
125116

126-
val receiverReferenceHasLocalCopyAnnotation = receiverAnnotations.contains(CORE_TYPE_LOCAL_COPY_ANNOTATION)
117+
val receiverReferenceHasLocalCopyAnnotation = annotationOwner
118+
?.annotations
119+
?.any { it.hasQualifiedName(CORE_TYPE_LOCAL_COPY_ANNOTATION) } == true
127120

128121
return when {
129122
// when the receiver reference has a local copy annotation, it means we're modifying a local copy (see the assumption in the kdoc!)

kt/plugins/godot-intellij-plugin/src/main/kotlin/godot/intellij/plugin/annotator/function/RegisterFunctionAnnotator.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import com.intellij.codeInspection.ProblemHighlightType
44
import com.intellij.lang.annotation.AnnotationHolder
55
import com.intellij.lang.annotation.Annotator
66
import com.intellij.psi.PsiElement
7+
import godot.common.constants.Constraints
78
import godot.intellij.plugin.GodotPluginBundle
89
import godot.intellij.plugin.annotator.general.checkNotGeneric
910
import godot.intellij.plugin.data.model.REGISTER_CLASS_ANNOTATION
1011
import godot.intellij.plugin.data.model.REGISTER_FUNCTION_ANNOTATION
12+
import godot.intellij.plugin.extension.asClassId
1113
import godot.intellij.plugin.extension.isInGodotRoot
1214
import godot.intellij.plugin.extension.registerProblem
1315
import godot.intellij.plugin.quickfix.FunctionNotRegisteredQuickFix
14-
import godot.common.constants.Constraints
1516
import godot.tools.common.constants.GodotTypes
17+
import org.jetbrains.kotlin.analysis.api.analyze
1618
import org.jetbrains.kotlin.asJava.toLightMethods
17-
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
1819
import org.jetbrains.kotlin.idea.util.findAnnotation
19-
import org.jetbrains.kotlin.name.FqName
2020
import org.jetbrains.kotlin.psi.KtNamedFunction
2121
import org.jetbrains.kotlin.psi.psiUtil.containingClass
2222

@@ -41,23 +41,26 @@ class RegisterFunctionAnnotator : Annotator {
4141
)
4242
}
4343

44-
if (element.findAnnotation(FqName(REGISTER_FUNCTION_ANNOTATION)) != null) {
44+
if (element.findAnnotation(asClassId(REGISTER_FUNCTION_ANNOTATION)) != null) {
4545
checkNotGeneric(element.toLightMethods().first(), holder)
4646
checkFunctionParameterCount(element, holder)
4747
}
4848
}
4949
}
5050

5151
private fun overriddenRegisteredAbstractFunctionNotRegistered(element: KtNamedFunction): Boolean {
52-
return element.containingClass()?.findAnnotation(FqName(REGISTER_CLASS_ANNOTATION)) != null &&
53-
element.findAnnotation(FqName(REGISTER_FUNCTION_ANNOTATION)) == null &&
54-
element.resolveToDescriptorIfAny()?.overriddenDescriptors?.any { it.annotations.hasAnnotation(FqName(REGISTER_FUNCTION_ANNOTATION)) } == true
52+
val hasRegisterAnnotation = analyze(element) {
53+
element.symbol.allOverriddenSymbols.any { it.annotations.contains(asClassId(REGISTER_FUNCTION_ANNOTATION)) }
54+
}
55+
return element.containingClass()?.findAnnotation(asClassId(REGISTER_CLASS_ANNOTATION)) != null &&
56+
element.findAnnotation(asClassId(REGISTER_FUNCTION_ANNOTATION)) == null &&
57+
hasRegisterAnnotation
5558
}
5659

5760
private fun overriddenNotificationFunctionNotRegistered(element: KtNamedFunction) =
58-
element.containingClass()?.findAnnotation(FqName(REGISTER_CLASS_ANNOTATION)) != null &&
61+
element.containingClass()?.findAnnotation(asClassId(REGISTER_CLASS_ANNOTATION)) != null &&
5962
GodotTypes.notificationFunctions.contains(element.name) &&
60-
element.findAnnotation(FqName(REGISTER_FUNCTION_ANNOTATION)) == null
63+
element.findAnnotation(asClassId(REGISTER_FUNCTION_ANNOTATION)) == null
6164

6265

6366
private fun checkFunctionParameterCount(element: KtNamedFunction, holder: AnnotationHolder) {

kt/plugins/godot-intellij-plugin/src/main/kotlin/godot/intellij/plugin/annotator/function/RpcAnnotator.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
package godot.intellij.plugin.annotator.function
22

3+
34
import com.intellij.codeInspection.ProblemHighlightType
45
import com.intellij.lang.annotation.AnnotationHolder
56
import com.intellij.lang.annotation.Annotator
67
import com.intellij.psi.PsiElement
78
import godot.intellij.plugin.GodotPluginBundle
89
import godot.intellij.plugin.data.model.RPC_ANNOTATION
10+
import godot.intellij.plugin.extension.asClassId
911
import godot.intellij.plugin.extension.isInGodotRoot
1012
import godot.intellij.plugin.extension.registerProblem
1113
import godot.intellij.plugin.quickfix.TransferModeIgnoresChannelQuickFix
1214
import godot.tools.common.constants.GodotKotlinJvmTypes
1315
import godot.tools.common.constants.godotAnnotationPackage
1416
import org.jetbrains.kotlin.idea.base.psi.kotlinFqName
15-
16-
1717
import org.jetbrains.kotlin.idea.references.mainReference
1818
import org.jetbrains.kotlin.idea.util.findAnnotation
19-
import org.jetbrains.kotlin.name.FqName
20-
2119
import org.jetbrains.kotlin.psi.KtAnnotated
2220
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
2321
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
@@ -28,8 +26,8 @@ class RpcAnnotator : Annotator {
2826
override fun annotate(element: PsiElement, holder: AnnotationHolder) {
2927
if (!element.isInGodotRoot()) return
3028

31-
if (element is KtAnnotated && element.findAnnotation(FqName(RPC_ANNOTATION)) != null) {
32-
val valueArgumentList = element.findAnnotation(FqName(RPC_ANNOTATION))?.valueArgumentList ?: return
29+
if (element is KtAnnotated && element.findAnnotation(asClassId(RPC_ANNOTATION)) != null) {
30+
val valueArgumentList = element.findAnnotation(asClassId(RPC_ANNOTATION))?.valueArgumentList ?: return
3331

3432
val transferModeValueArgument = valueArgumentList
3533
.arguments

0 commit comments

Comments
 (0)