|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2023 minecraft-dev |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published |
| 10 | + * by the Free Software Foundation, version 3.0 only. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.demonwav.mcdev.platform.fabric.inspection |
| 22 | + |
| 23 | +import com.demonwav.mcdev.platform.fabric.reference.EntryPointReference |
| 24 | +import com.demonwav.mcdev.platform.fabric.util.FabricConstants |
| 25 | +import com.intellij.codeInspection.InspectionManager |
| 26 | +import com.intellij.codeInspection.LocalInspectionTool |
| 27 | +import com.intellij.codeInspection.ProblemDescriptor |
| 28 | +import com.intellij.codeInspection.ProblemHighlightType |
| 29 | +import com.intellij.codeInspection.ProblemsHolder |
| 30 | +import com.intellij.json.psi.JsonElementVisitor |
| 31 | +import com.intellij.json.psi.JsonProperty |
| 32 | +import com.intellij.json.psi.JsonStringLiteral |
| 33 | +import com.intellij.psi.JavaPsiFacade |
| 34 | +import com.intellij.psi.PsiClass |
| 35 | +import com.intellij.psi.PsiClassType |
| 36 | +import com.intellij.psi.PsiElementVisitor |
| 37 | +import com.intellij.psi.PsiField |
| 38 | +import com.intellij.psi.PsiFile |
| 39 | +import com.intellij.psi.PsiMethod |
| 40 | +import com.intellij.psi.PsiModifier |
| 41 | +import com.intellij.psi.util.parentOfType |
| 42 | + |
| 43 | +class FabricEntrypointsInspection : LocalInspectionTool() { |
| 44 | + |
| 45 | + override fun getStaticDescription() = "Validates entrypoints declared in Fabric mod JSON files." |
| 46 | + |
| 47 | + override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { |
| 48 | + if (holder.file.name == FabricConstants.FABRIC_MOD_JSON) { |
| 49 | + return Visitor(holder) |
| 50 | + } |
| 51 | + return PsiElementVisitor.EMPTY_VISITOR |
| 52 | + } |
| 53 | + |
| 54 | + override fun processFile(file: PsiFile, manager: InspectionManager): List<ProblemDescriptor> { |
| 55 | + if (file.name == FabricConstants.FABRIC_MOD_JSON) { |
| 56 | + return super.processFile(file, manager) |
| 57 | + } |
| 58 | + return emptyList() |
| 59 | + } |
| 60 | + |
| 61 | + private class Visitor(private val holder: ProblemsHolder) : JsonElementVisitor() { |
| 62 | + |
| 63 | + override fun visitStringLiteral(literal: JsonStringLiteral) { |
| 64 | + for (reference in literal.references) { |
| 65 | + if (reference !is EntryPointReference.Reference) { |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + val resolved = reference.multiResolve(false) |
| 70 | + if (resolved.size > 1) { |
| 71 | + holder.registerProblem( |
| 72 | + literal, |
| 73 | + "Ambiguous member reference", |
| 74 | + ProblemHighlightType.GENERIC_ERROR_OR_WARNING, |
| 75 | + reference.rangeInElement, |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + val element = resolved.singleOrNull()?.element |
| 80 | + when { |
| 81 | + element is PsiClass && !literal.text.contains("::") -> { |
| 82 | + val propertyKey = literal.parentOfType<JsonProperty>()?.name |
| 83 | + val expectedType = propertyKey?.let { FabricConstants.ENTRYPOINT_BY_TYPE[it] } |
| 84 | + if (propertyKey != null && expectedType != null && |
| 85 | + !isEntrypointOfCorrectType(element, propertyKey) |
| 86 | + ) { |
| 87 | + holder.registerProblem( |
| 88 | + literal, |
| 89 | + "'$propertyKey' entrypoints must implement $expectedType", |
| 90 | + ProblemHighlightType.GENERIC_ERROR_OR_WARNING, |
| 91 | + reference.rangeInElement, |
| 92 | + ) |
| 93 | + } else if (element.constructors.isNotEmpty() && |
| 94 | + element.constructors.find { !it.hasParameters() } == null |
| 95 | + ) { |
| 96 | + holder.registerProblem( |
| 97 | + literal, |
| 98 | + "Entrypoint class must have an empty constructor", |
| 99 | + ProblemHighlightType.GENERIC_ERROR_OR_WARNING, |
| 100 | + reference.rangeInElement, |
| 101 | + ) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + element is PsiMethod -> { |
| 106 | + if (element.hasParameters()) { |
| 107 | + holder.registerProblem( |
| 108 | + literal, |
| 109 | + "Entrypoint method must have no parameters", |
| 110 | + ProblemHighlightType.GENERIC_ERROR_OR_WARNING, |
| 111 | + reference.rangeInElement, |
| 112 | + ) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + element is PsiField -> { |
| 117 | + if (!element.hasModifierProperty(PsiModifier.STATIC)) { |
| 118 | + holder.registerProblem( |
| 119 | + literal, |
| 120 | + "Entrypoint field must be static", |
| 121 | + ProblemHighlightType.GENERIC_ERROR_OR_WARNING, |
| 122 | + reference.rangeInElement, |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + val propertyKey = literal.parentOfType<JsonProperty>()?.name |
| 127 | + val fieldTypeClass = (element.type as? PsiClassType)?.resolve() |
| 128 | + val expectedType = propertyKey?.let { FabricConstants.ENTRYPOINT_BY_TYPE[it] } |
| 129 | + if (propertyKey != null && fieldTypeClass != null && expectedType != null && |
| 130 | + !isEntrypointOfCorrectType(fieldTypeClass, propertyKey) |
| 131 | + ) { |
| 132 | + holder.registerProblem( |
| 133 | + literal, |
| 134 | + "'$propertyKey' entrypoints must be of type $expectedType", |
| 135 | + ProblemHighlightType.GENERIC_ERROR_OR_WARNING, |
| 136 | + reference.rangeInElement, |
| 137 | + ) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private fun isEntrypointOfCorrectType(element: PsiClass, type: String): Boolean { |
| 145 | + val entrypointClass = FabricConstants.ENTRYPOINT_BY_TYPE[type] |
| 146 | + ?: return false |
| 147 | + val clazz = JavaPsiFacade.getInstance(element.project).findClass(entrypointClass, element.resolveScope) |
| 148 | + return clazz != null && element.isInheritor(clazz, true) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments