@@ -22,11 +22,15 @@ package com.demonwav.mcdev.platform.mixin.inspection.addedMembers
2222
2323import com.demonwav.mcdev.facet.MinecraftFacet
2424import com.demonwav.mcdev.platform.fabric.FabricModuleType
25+ import com.demonwav.mcdev.util.RegexConverter
2526import com.demonwav.mcdev.util.decapitalize
27+ import com.demonwav.mcdev.util.doBindItem
28+ import com.demonwav.mcdev.util.doBindText
2629import com.demonwav.mcdev.util.findContainingClass
2730import com.demonwav.mcdev.util.findModule
28- import com.demonwav.mcdev.util.onShown
31+ import com.demonwav.mcdev.util.regexValidator
2932import com.demonwav.mcdev.util.toJavaIdentifier
33+ import com.demonwav.mcdev.util.toRegexOrDefault
3034import com.intellij.codeInsight.CodeInsightBundle
3135import com.intellij.codeInsight.FileModificationService
3236import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo
@@ -37,37 +41,24 @@ import com.intellij.ide.util.SuperMethodWarningUtil
3741import com.intellij.openapi.editor.Editor
3842import com.intellij.openapi.module.Module
3943import com.intellij.openapi.project.Project
40- import com.intellij.openapi.ui.ComboBox
41- import com.intellij.openapi.ui.ComponentValidator
42- import com.intellij.openapi.ui.DialogWrapper
4344import com.intellij.openapi.util.text.StringUtil
4445import com.intellij.psi.PsiElement
46+ import com.intellij.psi.PsiEnumConstant
4547import com.intellij.psi.PsiField
4648import com.intellij.psi.PsiFile
4749import com.intellij.psi.PsiMethod
4850import com.intellij.psi.PsiNameIdentifierOwner
4951import com.intellij.psi.PsiNamedElement
5052import com.intellij.psi.util.PsiTreeUtil
5153import com.intellij.refactoring.rename.RenameProcessor
52- import com.intellij.ui.DocumentAdapter
5354import com.intellij.ui.EnumComboBoxModel
5455import com.intellij.ui.components.JBLabel
55- import com.intellij.ui.components.JBTextField
5656import com.intellij.ui.dsl.builder.COLUMNS_SHORT
57- import com.intellij.ui.dsl.builder.Cell
5857import com.intellij.ui.dsl.builder.RowLayout
5958import com.intellij.ui.dsl.builder.columns
6059import com.intellij.ui.dsl.builder.panel
61- import com.intellij.ui.layout.ValidationInfoBuilder
62- import com.intellij.util.xmlb.Converter
6360import com.intellij.util.xmlb.annotations.Attribute
64- import java.util.function.Supplier
65- import java.util.regex.Pattern
66- import java.util.regex.PatternSyntaxException
6761import javax.swing.JComponent
68- import javax.swing.event.DocumentEvent
69- import kotlin.reflect.KMutableProperty0
70- import org.intellij.lang.annotations.Language
7162
7263class AddedMembersNameFormatInspection : AbstractAddedMembersInspection () {
7364 @Attribute(converter = RegexConverter ::class )
@@ -91,7 +82,7 @@ class AddedMembersNameFormatInspection : AbstractAddedMembersInspection() {
9182 override fun getStaticDescription () = " Reports added members not matching the correct name format"
9283
9384 override fun visitAddedField (holder : ProblemsHolder , field : PsiField ) {
94- if (reportFields.shouldReport(field.findModule())) {
85+ if (field !is PsiEnumConstant && reportFields.shouldReport(field.findModule())) {
9586 visitAdded(holder, field)
9687 }
9788 }
@@ -131,7 +122,7 @@ class AddedMembersNameFormatInspection : AbstractAddedMembersInspection() {
131122 val fixed = try {
132123 validNameFixSearch.replace(name, validNameFixReplace)
133124 .replace(" MOD_ID" , getAppropriatePrefix(holder.project))
134- } catch (e : RuntimeException ) {
125+ } catch (_ : RuntimeException ) {
135126 null
136127 }
137128
@@ -219,67 +210,6 @@ class AddedMembersNameFormatInspection : AbstractAddedMembersInspection() {
219210 }
220211}
221212
222- private fun String.toRegexOrDefault (@Language(" RegExp" ) default : String ): Regex {
223- return try {
224- this .toRegex()
225- } catch (e: PatternSyntaxException ) {
226- default.toRegex()
227- }
228- }
229-
230- private fun Cell<JBTextField>.doBindText (property : KMutableProperty0 <String >): Cell <JBTextField > {
231- return doBindText(property.getter, property.setter)
232- }
233-
234- private fun Cell<JBTextField>.doBindText (getter : () -> String , setter : (String ) -> Unit ): Cell <JBTextField > {
235- component.text = getter()
236- component.document.addDocumentListener(object : DocumentAdapter () {
237- override fun textChanged (e : DocumentEvent ) {
238- setter(component.text)
239- }
240- })
241- return this
242- }
243-
244- private fun <T > Cell<ComboBox<T>>.doBindItem (property : KMutableProperty0 <T >): Cell <ComboBox <T >> {
245- component.selectedItem = property.get()
246- component.addActionListener {
247- @Suppress(" UNCHECKED_CAST" )
248- val selectedItem = component.selectedItem as T ?
249- if (selectedItem != null ) {
250- property.set(selectedItem)
251- }
252- }
253- return this
254- }
255-
256- private fun Cell<JBTextField>.regexValidator (): Cell <JBTextField > {
257- var hasRegisteredValidator = false
258- component.onShown {
259- if (! hasRegisteredValidator) {
260- hasRegisteredValidator = true
261- val disposable = DialogWrapper .findInstance(component)?.disposable ? : return @onShown
262- ComponentValidator (disposable).withValidator(
263- Supplier {
264- try {
265- Pattern .compile(component.text)
266- null
267- } catch (e: PatternSyntaxException ) {
268- ValidationInfoBuilder (component).error(" Invalid regex" )
269- }
270- }
271- ).andRegisterOnDocumentListener(component).installOn(component)
272- }
273- }
274- return this
275- }
276-
277- private class RegexConverter : Converter <Regex >() {
278- override fun toString (value : Regex ) = value.pattern
279-
280- override fun fromString (value : String ) = runCatching { value.toRegex() }.getOrNull()
281- }
282-
283213private class RenameWithInheritanceFix (
284214 element : PsiNamedElement ,
285215 private val newName : String
0 commit comments