Skip to content

Commit

Permalink
Add DistExecutor inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Aug 30, 2020
1 parent 9bb119b commit 95e914a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Minecraft Dev for IntelliJ
*
* https://minecraftdev.org
*
* Copyright (c) 2020 minecraft-dev
*
* MIT License
*/

package com.demonwav.mcdev.platform.forge.inspections

import com.demonwav.mcdev.platform.forge.util.ForgeConstants
import com.intellij.psi.PsiErrorElement
import com.intellij.psi.PsiExpression
import com.intellij.psi.PsiLambdaExpression
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiMethodReferenceExpression
import com.siyeh.ig.BaseInspection
import com.siyeh.ig.BaseInspectionVisitor

class DistExecutorInspection : BaseInspection() {
override fun getDisplayName() = "DistExecutor problems"

override fun getStaticDescription() = "DistExecutor problems"

override fun buildErrorString(vararg infos: Any?): String {
return infos[0] as String
}

override fun buildVisitor(): BaseInspectionVisitor {
return Visitor()
}

private class Visitor : BaseInspectionVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
val method = expression.resolveMethod() ?: return
if (method.containingClass?.qualifiedName != ForgeConstants.DIST_EXECUTOR) return
when (method.name) {
"safeCallWhenOn", "safeRunWhenOn" -> {
checkSafeArgument(method.name, expression.argumentList.expressions.getOrNull(1))
}
"safeRunForDist" -> {
for (arg in expression.argumentList.expressions) {
checkSafeArgument(method.name, arg)
}
}
}
}

private fun checkSafeArgument(methodName: String, expression: PsiExpression?) {
if (expression == null || expression is PsiErrorElement || expression.textLength == 0) return

if (expression !is PsiLambdaExpression) {
registerError(expression, "DistExecutor.$methodName must contain lambda argument")
return
}

val lambdaBody = expression.body
if (lambdaBody != null &&
lambdaBody !is PsiMethodReferenceExpression &&
lambdaBody !is PsiErrorElement &&
lambdaBody.textLength != 0
) {
registerError(lambdaBody, "DistExecutor.$methodName must contain a method reference inside a lambda")
}
}
}
}
7 changes: 7 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,13 @@
level="ERROR"
hasStaticDescription="true"
implementationClass="com.demonwav.mcdev.platform.forge.inspections.simpleimpl.MissingMessageConstructorInspection"/>
<localInspection displayName="DistExecutor problems"
groupName="MinecraftForge"
language="JAVA"
enabledByDefault="true"
level="WARNING"
hasStaticDescription="true"
implementationClass="com.demonwav.mcdev.platform.forge.inspections.DistExecutorInspection"/>
<!--endregion-->

<!--region FABRIC INSPECTIONS-->
Expand Down

0 comments on commit 95e914a

Please sign in to comment.