Skip to content

Commit 95e914a

Browse files
committed
Add DistExecutor inspection
1 parent 9bb119b commit 95e914a

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Minecraft Dev for IntelliJ
3+
*
4+
* https://minecraftdev.org
5+
*
6+
* Copyright (c) 2020 minecraft-dev
7+
*
8+
* MIT License
9+
*/
10+
11+
package com.demonwav.mcdev.platform.forge.inspections
12+
13+
import com.demonwav.mcdev.platform.forge.util.ForgeConstants
14+
import com.intellij.psi.PsiErrorElement
15+
import com.intellij.psi.PsiExpression
16+
import com.intellij.psi.PsiLambdaExpression
17+
import com.intellij.psi.PsiMethodCallExpression
18+
import com.intellij.psi.PsiMethodReferenceExpression
19+
import com.siyeh.ig.BaseInspection
20+
import com.siyeh.ig.BaseInspectionVisitor
21+
22+
class DistExecutorInspection : BaseInspection() {
23+
override fun getDisplayName() = "DistExecutor problems"
24+
25+
override fun getStaticDescription() = "DistExecutor problems"
26+
27+
override fun buildErrorString(vararg infos: Any?): String {
28+
return infos[0] as String
29+
}
30+
31+
override fun buildVisitor(): BaseInspectionVisitor {
32+
return Visitor()
33+
}
34+
35+
private class Visitor : BaseInspectionVisitor() {
36+
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
37+
val method = expression.resolveMethod() ?: return
38+
if (method.containingClass?.qualifiedName != ForgeConstants.DIST_EXECUTOR) return
39+
when (method.name) {
40+
"safeCallWhenOn", "safeRunWhenOn" -> {
41+
checkSafeArgument(method.name, expression.argumentList.expressions.getOrNull(1))
42+
}
43+
"safeRunForDist" -> {
44+
for (arg in expression.argumentList.expressions) {
45+
checkSafeArgument(method.name, arg)
46+
}
47+
}
48+
}
49+
}
50+
51+
private fun checkSafeArgument(methodName: String, expression: PsiExpression?) {
52+
if (expression == null || expression is PsiErrorElement || expression.textLength == 0) return
53+
54+
if (expression !is PsiLambdaExpression) {
55+
registerError(expression, "DistExecutor.$methodName must contain lambda argument")
56+
return
57+
}
58+
59+
val lambdaBody = expression.body
60+
if (lambdaBody != null &&
61+
lambdaBody !is PsiMethodReferenceExpression &&
62+
lambdaBody !is PsiErrorElement &&
63+
lambdaBody.textLength != 0
64+
) {
65+
registerError(lambdaBody, "DistExecutor.$methodName must contain a method reference inside a lambda")
66+
}
67+
}
68+
}
69+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,13 @@
397397
level="ERROR"
398398
hasStaticDescription="true"
399399
implementationClass="com.demonwav.mcdev.platform.forge.inspections.simpleimpl.MissingMessageConstructorInspection"/>
400+
<localInspection displayName="DistExecutor problems"
401+
groupName="MinecraftForge"
402+
language="JAVA"
403+
enabledByDefault="true"
404+
level="WARNING"
405+
hasStaticDescription="true"
406+
implementationClass="com.demonwav.mcdev.platform.forge.inspections.DistExecutorInspection"/>
400407
<!--endregion-->
401408

402409
<!--region FABRIC INSPECTIONS-->

0 commit comments

Comments
 (0)