User: joe Date: 24 Jun 23 22:18 Revision: df7a52c278bf2e41af801b2e04b662751d86c798 Summary: Add the option for different logic on Fabric in the added members name format inspection. Fabric Mixin adds prefixes to the name when @Unique is applied. TeamCity URL: https://ci.mcdev.io/viewModification.html?tab=vcsModificationFiles&modId=8551&personal=false Index: src/main/kotlin/platform/mixin/inspection/addedMembers/AddedMembersNameFormatInspection.kt =================================================================== --- src/main/kotlin/platform/mixin/inspection/addedMembers/AddedMembersNameFormatInspection.kt (revision 0da34782e5c29f3e4f13ec0cefdc9bca4e1e8baa) +++ src/main/kotlin/platform/mixin/inspection/addedMembers/AddedMembersNameFormatInspection.kt (revision df7a52c278bf2e41af801b2e04b662751d86c798) @@ -20,17 +20,23 @@ package com.demonwav.mcdev.platform.mixin.inspection.addedMembers +import com.demonwav.mcdev.facet.MinecraftFacet +import com.demonwav.mcdev.platform.fabric.FabricModuleType import com.demonwav.mcdev.util.decapitalize import com.demonwav.mcdev.util.findContainingClass +import com.demonwav.mcdev.util.findModule import com.demonwav.mcdev.util.onShown import com.demonwav.mcdev.util.toJavaIdentifier import com.intellij.codeInsight.CodeInsightBundle import com.intellij.codeInsight.FileModificationService import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement import com.intellij.codeInspection.ProblemsHolder +import com.intellij.icons.AllIcons import com.intellij.ide.util.SuperMethodWarningUtil import com.intellij.openapi.editor.Editor +import com.intellij.openapi.module.Module import com.intellij.openapi.project.Project +import com.intellij.openapi.ui.ComboBox import com.intellij.openapi.ui.ComponentValidator import com.intellij.openapi.ui.DialogWrapper import com.intellij.openapi.util.text.StringUtil @@ -42,10 +48,12 @@ import com.intellij.psi.PsiNamedElement import com.intellij.refactoring.rename.RenameProcessor import com.intellij.ui.DocumentAdapter -import com.intellij.ui.components.JBCheckBox +import com.intellij.ui.EnumComboBoxModel +import com.intellij.ui.components.JBLabel import com.intellij.ui.components.JBTextField import com.intellij.ui.dsl.builder.COLUMNS_SHORT import com.intellij.ui.dsl.builder.Cell +import com.intellij.ui.dsl.builder.RowLayout import com.intellij.ui.dsl.builder.columns import com.intellij.ui.dsl.builder.panel import com.intellij.ui.layout.ValidationInfoBuilder @@ -72,45 +80,43 @@ var validNameFixReplace = "MOD_ID\\\$\$0" @JvmField - var ignoreFields = false + var reportFields = ReportMode.NOT_ON_FABRIC @JvmField - var ignoreMethods = false + var reportMethods = ReportMode.NOT_ON_FABRIC @JvmField - var ignoreInheritedInterfaceMethods = false + var reportInheritedMethods = ReportMode.ALWAYS override fun getStaticDescription() = "Reports added members not matching the correct name format" override fun visitAddedField(holder: ProblemsHolder, field: PsiField) { - if (!ignoreFields) { + if (reportFields.shouldReport(field.findModule())) { visitAdded(holder, field) } } override fun visitAddedMethod(holder: ProblemsHolder, method: PsiMethod, isInherited: Boolean) { - if (!shouldIgnoreMethod(method, isInherited)) { + if (shouldReportMethod(method, isInherited)) { visitAdded(holder, method) } } - private fun shouldIgnoreMethod(method: PsiMethod, isInherited: Boolean): Boolean { - if (ignoreMethods) { - return true - } + private fun shouldReportMethod(method: PsiMethod, isInherited: Boolean): Boolean { + val module = method.findModule() if (isInherited) { - if (ignoreInheritedInterfaceMethods) { - return true + if (!reportInheritedMethods.shouldReport(module)) { + return false } val superMethods = method.findDeepestSuperMethods() - val isInterfaceMethod = superMethods.any { + val isWritableInterfaceMethod = superMethods.any { val clazz = it.findContainingClass() ?: return@any false clazz.isInterface && clazz.containingFile?.isWritable == true } - return !isInterfaceMethod + return isWritableInterfaceMethod + } else { + return reportMethods.shouldReport(module) } - - return false } private fun visitAdded(holder: ProblemsHolder, added: PsiNameIdentifierOwner) { @@ -162,38 +168,54 @@ .columns(COLUMNS_SHORT) .regexValidator() } - row("Valid name fix replace:") { + row { + layout(RowLayout.LABEL_ALIGNED) + val toolTip = + "Uses regex replacement syntax after matching from the regex in the option above.
" + + "\"MOD_ID\" is replaced with the project name, converted to a valid Java identifier." + label("Valid name fix replace:") + .applyToComponent { horizontalTextPosition = JBLabel.LEFT } + .applyToComponent { icon = AllIcons.General.ContextHelp } + .applyToComponent { toolTipText = toolTip } textField().doBindText(::validNameFixReplace).columns(COLUMNS_SHORT) } separator() - var ignoreFields: Cell? = null - row { - ignoreFields = checkBox("Ignore fields").doBindSelected(::ignoreFields) + row("Report fields:") { + comboBox(EnumComboBoxModel(ReportMode::class.java)).doBindItem(::reportFields) } - var ignoreMethods: Cell? = null + row("Report methods:") { + comboBox(EnumComboBoxModel(ReportMode::class.java)).doBindItem(::reportMethods) + } row { - ignoreMethods = checkBox("Ignore methods").doBindSelected(::ignoreMethods) + layout(RowLayout.LABEL_ALIGNED) + label("Report inherited methods:") + .applyToComponent { horizontalTextPosition = JBLabel.LEFT } + .applyToComponent { icon = AllIcons.General.ContextHelp } + .applyToComponent { toolTipText = "Reports methods that are inherited from duck interfaces" } + comboBox(EnumComboBoxModel(ReportMode::class.java)).doBindItem(::reportInheritedMethods) } - // make sure ignore fields and ignore methods can't be selected at the same time - ignoreFields!!.component.addActionListener { - if (ignoreFields!!.component.isSelected) { - ignoreMethods!!.component.isSelected = false - } - } + } + } - ignoreMethods!!.component.addActionListener { - if (ignoreMethods!!.component.isSelected) { - ignoreFields!!.component.isSelected = false - } - } - row { - checkBox("Ignore inherited interface methods").doBindSelected(::ignoreInheritedInterfaceMethods) + enum class ReportMode(private val displayName: String) { + ALWAYS("Always"), ON_FABRIC("On Fabric"), NOT_ON_FABRIC("Not on Fabric"), NEVER("Never"); + + override fun toString() = displayName + + fun shouldReport(module: Module?): Boolean { + if (this == NEVER) { + return false } + if (this == ALWAYS) { + return true - } + } + val isFabric = module?.let { MinecraftFacet.getInstance(module, FabricModuleType) != null } ?: false + return (this == ON_FABRIC) == isFabric - } -} + } + } +} private fun String.toRegexOrDefault(@Language("RegExp") default: String): Regex { return try { @@ -217,11 +239,15 @@ return this } -private fun Cell.doBindSelected(property: KMutableProperty0): Cell { - component.isSelected = property.get() +private fun Cell>.doBindItem(property: KMutableProperty0): Cell> { + component.selectedItem = property.get() component.addActionListener { - property.set(component.isSelected) + @Suppress("UNCHECKED_CAST") + val selectedItem = component.selectedItem as T? + if (selectedItem != null) { + property.set(selectedItem) - } + } + } return this }