|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2026 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.mixin.inspection |
| 22 | + |
| 23 | +import com.demonwav.mcdev.platform.mixin.util.isMixin |
| 24 | +import com.demonwav.mcdev.platform.mixin.util.mixinTargets |
| 25 | +import com.intellij.codeInspection.LocalQuickFix |
| 26 | +import com.intellij.codeInspection.LocalQuickFixOnPsiElement |
| 27 | +import com.intellij.codeInspection.ProblemsHolder |
| 28 | +import com.intellij.openapi.project.Project |
| 29 | +import com.intellij.psi.JavaElementVisitor |
| 30 | +import com.intellij.psi.JavaPsiFacade |
| 31 | +import com.intellij.psi.PsiElement |
| 32 | +import com.intellij.psi.PsiExpression |
| 33 | +import com.intellij.psi.PsiFile |
| 34 | +import com.intellij.psi.PsiSwitchBlock |
| 35 | +import com.intellij.psi.PsiSwitchExpression |
| 36 | +import com.intellij.psi.PsiSwitchStatement |
| 37 | +import com.intellij.psi.PsiTypeCastExpression |
| 38 | +import com.intellij.psi.codeStyle.JavaCodeStyleManager |
| 39 | +import com.intellij.psi.util.PsiTypesUtil |
| 40 | + |
| 41 | +class EnumMixinSwitchStatementInspection : MixinInspection() { |
| 42 | + override fun getStaticDescription() = "Reports when a switch statement or expression is used on an enum mixin" |
| 43 | + |
| 44 | + override fun buildVisitor(holder: ProblemsHolder) = object : JavaElementVisitor() { |
| 45 | + override fun visitSwitchStatement(statement: PsiSwitchStatement) { |
| 46 | + visitSwitchBlock(statement) |
| 47 | + } |
| 48 | + |
| 49 | + override fun visitSwitchExpression(expression: PsiSwitchExpression) { |
| 50 | + visitSwitchBlock(expression) |
| 51 | + } |
| 52 | + |
| 53 | + private fun visitSwitchBlock(block: PsiSwitchBlock) { |
| 54 | + val expression = block.expression ?: return |
| 55 | + val mixinClass = PsiTypesUtil.getPsiClass(expression.type) ?: return |
| 56 | + if (!mixinClass.isEnum || !mixinClass.isMixin) { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + val fixes = mutableListOf<LocalQuickFix>() |
| 61 | + val targetClass = mixinClass.mixinTargets.singleOrNull() |
| 62 | + if (targetClass != null) { |
| 63 | + fixes += AddCastFix(expression, targetClass.name) |
| 64 | + } |
| 65 | + holder.registerProblem(expression, "Switch used on enum mixin", *fixes.toTypedArray()) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private class AddCastFix(expression: PsiExpression, private val targetClass: String) : LocalQuickFixOnPsiElement(expression) { |
| 70 | + override fun getFamilyName() = "Add double cast to ${targetClass.substringAfterLast('/').replace('$', '.')}" |
| 71 | + override fun getText() = "Add cast to target class ${targetClass.substringAfterLast('/').replace('$', '.')}" |
| 72 | + |
| 73 | + override fun invoke(project: Project, psiFile: PsiFile, startElement: PsiElement, endElement: PsiElement) { |
| 74 | + val expression = startElement as? PsiExpression ?: return |
| 75 | + val doubleCast = JavaPsiFacade.getElementFactory(project).createExpressionFromText( |
| 76 | + "(${targetClass.replace('/', '.').replace('$', '.')}) (java.lang.Object) x", |
| 77 | + expression |
| 78 | + ) as PsiTypeCastExpression |
| 79 | + (doubleCast.operand as PsiTypeCastExpression).operand!!.replace(expression) |
| 80 | + val resultingExpression = expression.replace(doubleCast) |
| 81 | + JavaCodeStyleManager.getInstance(project).shortenClassReferences(resultingExpression) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments