User: joe Date: 01 Dec 26 22:26 Revision: 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82 Summary: Address DenWav's review comments TeamCity URL: http://ci.mcdev.io:80/viewModification.html?tab=vcsModificationFiles&modId=10826&personal=false Index: src/main/kotlin/platform/minecraft/unpick/UnpickGroups.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/UnpickGroups.kt (revision 4386bb995c4ad6c9a9fe6ce5906a6f32fab8af7a) +++ src/main/kotlin/platform/minecraft/unpick/UnpickGroups.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -31,10 +31,10 @@ object UnpickGroups { /** - * Finds all definitions of the group with the given name, or of the default group if [name] is `null`. + * Finds all definitions of the group with the given name. */ - fun findDefinitions(context: PsiElement, name: String?): List { - return findFiles(context, name ?: UnpickGroupIndex.Util.DEFAULT_GROUP_KEY).flatMap { file -> + fun findDefinitions(context: PsiElement, name: String): List { + return findFiles(context, name).flatMap { file -> file.groupDefinitions.filter { it.name == name } } } @@ -54,7 +54,7 @@ index.processAllKeys( UnpickGroupIndex.Util.NAME, { key -> - if (key != UnpickGroupIndex.Util.DEFAULT_GROUP_KEY && key !in result) { + if (key !in result) { if (index.getContainingFiles(UnpickGroupIndex.Util.NAME, key, scope).isNotEmpty()) { result += key } Index: src/main/kotlin/platform/minecraft/unpick/UnpickIdentifiers.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/UnpickIdentifiers.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) +++ src/main/kotlin/platform/minecraft/unpick/UnpickIdentifiers.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -0,0 +1,30 @@ +/* + * Minecraft Development for IntelliJ + * + * https://mcdev.io/ + * + * Copyright (C) 2026 minecraft-dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, version 3.0 only. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.demonwav.mcdev.platform.minecraft.unpick + +object UnpickIdentifiers { + private val IDENTIFIER = Regex("[a-zA-Z_$][a-zA-Z0-9_$]*") + + /** + * Returns whether [name] is a valid unpick identifier. + */ + fun isValid(name: String) = IDENTIFIER.matches(name) +} Index: src/main/kotlin/platform/minecraft/unpick/UnpickRenameInputValidator.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/UnpickRenameInputValidator.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) +++ src/main/kotlin/platform/minecraft/unpick/UnpickRenameInputValidator.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -0,0 +1,45 @@ +/* + * Minecraft Development for IntelliJ + * + * https://mcdev.io/ + * + * Copyright (C) 2026 minecraft-dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, version 3.0 only. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.demonwav.mcdev.platform.minecraft.unpick + +import com.demonwav.mcdev.asset.MCDevBundle +import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickGroupDefinition +import com.intellij.openapi.project.Project +import com.intellij.patterns.ElementPattern +import com.intellij.patterns.PlatformPatterns +import com.intellij.psi.PsiElement +import com.intellij.refactoring.rename.RenameInputValidatorEx +import com.intellij.util.ProcessingContext + +class UnpickRenameInputValidator : RenameInputValidatorEx { + override fun isInputValid(newName: String, element: PsiElement, context: ProcessingContext) = + UnpickIdentifiers.isValid(newName) + + override fun getPattern(): ElementPattern = + PlatformPatterns.psiElement(UnpickGroupDefinition::class.java) + + override fun getErrorMessage(newName: String, project: Project): String? { + if (UnpickIdentifiers.isValid(newName)) { + return null + } + return MCDevBundle("minecraft.unpick.rename.invalid_identifier", newName) + } +} Index: src/main/kotlin/platform/minecraft/unpick/index/UnpickGroupIndex.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/index/UnpickGroupIndex.kt (revision 4386bb995c4ad6c9a9fe6ce5906a6f32fab8af7a) +++ src/main/kotlin/platform/minecraft/unpick/index/UnpickGroupIndex.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -47,13 +47,12 @@ private object Indexer : DataIndexer { override fun map(inputData: FileContent): Map { val file = inputData.psiFile as? UnpickFile ?: return emptyMap() - return file.groupDefinitions.associate { (it.name ?: Util.DEFAULT_GROUP_KEY) to null } + // The default group isn't indexed, since almost every file defines it + return file.groupDefinitions.mapNotNull { it.name }.associateWith { null } } } object Util { val NAME = ID.create("mcdev.UnpickGroupIndex") - - const val DEFAULT_GROUP_KEY = "" } } Index: src/main/kotlin/platform/minecraft/unpick/inspections/DuplicateUnpickConstantInspection.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/inspections/DuplicateUnpickConstantInspection.kt (revision 4386bb995c4ad6c9a9fe6ce5906a6f32fab8af7a) +++ src/main/kotlin/platform/minecraft/unpick/inspections/DuplicateUnpickConstantInspection.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -21,6 +21,7 @@ package com.demonwav.mcdev.platform.minecraft.unpick.inspections import com.demonwav.mcdev.asset.MCDevBundle +import com.demonwav.mcdev.platform.minecraft.unpick.UnpickFile import com.demonwav.mcdev.platform.minecraft.unpick.UnpickGroups import com.demonwav.mcdev.platform.minecraft.unpick.eval.UnpickExpressionEvaluator import com.demonwav.mcdev.platform.minecraft.unpick.eval.UnpickExpressionEvaluator.EvaluatedConstant @@ -42,8 +43,14 @@ val groupType = element.dataType?.dataType?.takeIf { it.isValidGroupType } ?: return val scopes = scopeKey(element) - // Constants only conflict with constants in the same group with the same scope - val definitions = UnpickGroups.findDefinitions(element, element.name) + // Constants only conflict with constants in the same group with the same scope. The default group isn't + // indexed, so only the definitions of it in the same file are compared. + val name = element.name + val definitions = if (name != null) { + UnpickGroups.findDefinitions(element, name) + } else { + (element.containingFile as? UnpickFile)?.groupDefinitions.orEmpty().filter { it.name == null } + } .filter { it.dataType?.dataType == groupType && scopeKey(it) == scopes } .ifEmpty { listOf(element) } Index: src/main/kotlin/platform/minecraft/unpick/psi/UnpickClassReference.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/psi/UnpickClassReference.kt (revision 4386bb995c4ad6c9a9fe6ce5906a6f32fab8af7a) +++ src/main/kotlin/platform/minecraft/unpick/psi/UnpickClassReference.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -20,7 +20,9 @@ package com.demonwav.mcdev.platform.minecraft.unpick.psi +import com.demonwav.mcdev.platform.minecraft.unpick.UnpickIdentifiers import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickClassName +import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickPackageName import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickTypes import com.demonwav.mcdev.util.findQualifiedClass import com.demonwav.mcdev.util.namedAndUnnamedInnerClasses @@ -89,20 +91,34 @@ } ?: throw IncorrectOperationException() val identifierRange = rangeInElement.shiftLeft(identifier.startOffsetInParent) val newText = identifierRange.replace(identifier.text, newElementName) + if (!UnpickIdentifiers.isValid(newText)) { + throw IncorrectOperationException() + } identifier.psi.replace(UnpickElementFactory.createIdentifier(element.project, newText)) return element } override fun bindToElement(element: PsiElement): PsiElement { - val className = this.element as? UnpickClassName - if (element is PsiClass && className != null && !isPackage) { - val binaryName = ClassUtil.getBinaryClassName(element) ?: throw IncorrectOperationException() - return className.replace(UnpickElementFactory.createClassName(element.project, binaryName)) + val newTargetName = when (element) { + is PsiClass if !isPackage -> ClassUtil.getBinaryClassName(element) + is PsiPackage if isPackage -> element.qualifiedName + else -> null + } ?: throw IncorrectOperationException() + + // Keep the part of the name after this reference, e.g. the nested classes when the outer class is moved + val suffix = qualifiedName(this.element).substring(targetName.length) + val newName = (newTargetName + suffix).removePrefix(".") + if (!newName.split('.').all(UnpickIdentifiers::isValid)) { + throw IncorrectOperationException() } - if (element is PsiPackage && isPackage) { - return handleElementRename(element.name ?: throw IncorrectOperationException()) + + val project = this.element.project + val replacement = when (this.element) { + is UnpickClassName -> UnpickElementFactory.createClassName(project, newName) + is UnpickPackageName -> UnpickElementFactory.createPackageName(project, newName) + else -> throw IncorrectOperationException() } - throw IncorrectOperationException() + return this.element.replace(replacement) } companion object { Index: src/main/kotlin/platform/minecraft/unpick/psi/UnpickElementFactory.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/psi/UnpickElementFactory.kt (revision 4386bb995c4ad6c9a9fe6ce5906a6f32fab8af7a) +++ src/main/kotlin/platform/minecraft/unpick/psi/UnpickElementFactory.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -24,6 +24,7 @@ import com.demonwav.mcdev.platform.minecraft.unpick.UnpickFileType import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickClassName import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickFieldName +import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickPackageName import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickTargetAnnotation import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickTargetField import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickTypes @@ -54,6 +55,13 @@ return annotation.className!! } + fun createPackageName(project: Project, name: String): UnpickPackageName { + return PsiTreeUtil.findChildOfType( + createFile(project, "unpick v4\ngroup int\n\t@scope package $name\n"), + UnpickPackageName::class.java, + )!! + } + fun createFieldName(project: Project, name: String): UnpickFieldName { val targetField = PsiTreeUtil.findChildOfType( createFile(project, "unpick v4\ntarget_field a.A $name I G"), Index: src/main/kotlin/platform/minecraft/unpick/psi/mixins/impl/UnpickFieldNameImplMixin.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/psi/mixins/impl/UnpickFieldNameImplMixin.kt (revision 4386bb995c4ad6c9a9fe6ce5906a6f32fab8af7a) +++ src/main/kotlin/platform/minecraft/unpick/psi/mixins/impl/UnpickFieldNameImplMixin.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -20,6 +20,7 @@ package com.demonwav.mcdev.platform.minecraft.unpick.psi.mixins.impl +import com.demonwav.mcdev.platform.minecraft.unpick.UnpickIdentifiers import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickClassName import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickFieldExpression import com.demonwav.mcdev.platform.minecraft.unpick.psi.UnpickElementFactory @@ -37,6 +38,7 @@ import com.intellij.psi.util.PsiModificationTracker import com.intellij.psi.util.PsiTreeUtil import com.intellij.util.ArrayUtil +import com.intellij.util.IncorrectOperationException abstract class UnpickFieldNameImplMixin(node: ASTNode) : ASTWrapperPsiElement(node), UnpickFieldNameMixin { @@ -76,6 +78,9 @@ override fun getCanonicalText(): String = text override fun handleElementRename(newElementName: String): PsiElement { + if (!UnpickIdentifiers.isValid(newElementName)) { + throw IncorrectOperationException() + } firstChild.replace(UnpickElementFactory.createIdentifier(project, newElementName)) return this } Index: src/main/kotlin/platform/minecraft/unpick/psi/mixins/impl/UnpickMethodNameImplMixin.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/psi/mixins/impl/UnpickMethodNameImplMixin.kt (revision 4386bb995c4ad6c9a9fe6ce5906a6f32fab8af7a) +++ src/main/kotlin/platform/minecraft/unpick/psi/mixins/impl/UnpickMethodNameImplMixin.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -20,6 +20,7 @@ package com.demonwav.mcdev.platform.minecraft.unpick.psi.mixins.impl +import com.demonwav.mcdev.platform.minecraft.unpick.UnpickIdentifiers import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickClassName import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickTypes import com.demonwav.mcdev.platform.minecraft.unpick.psi.UnpickElementFactory @@ -94,6 +95,9 @@ if (isConstructor || isStaticInitializer) { return this } + if (!UnpickIdentifiers.isValid(newElementName)) { + throw IncorrectOperationException() + } val identifier = node.findChildByType(UnpickTypes.IDENTIFIER) ?: throw IncorrectOperationException() identifier.psi.replace(UnpickElementFactory.createIdentifier(project, newElementName)) return this Index: src/main/resources/META-INF/plugin.xml =================================================================== --- src/main/resources/META-INF/plugin.xml (revision 4386bb995c4ad6c9a9fe6ce5906a6f32fab8af7a) +++ src/main/resources/META-INF/plugin.xml (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -623,6 +623,7 @@ + Index: src/main/resources/messages/MinecraftDevelopmentBundle.properties =================================================================== --- src/main/resources/messages/MinecraftDevelopmentBundle.properties (revision 4386bb995c4ad6c9a9fe6ce5906a6f32fab8af7a) +++ src/main/resources/messages/MinecraftDevelopmentBundle.properties (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -996,6 +996,7 @@ minecraft.unpick.literal.malformed_number=Malformed number minecraft.unpick.literal.unterminated_char=Unterminated character literal minecraft.unpick.literal.unterminated_string=Unterminated string literal +minecraft.unpick.rename.invalid_identifier=''{0}'' is not a valid unpick identifier minecraft.unscrambler.srg=Remap SRG names minecraft.unscrambler.mappings=Mappings\: minecraft.unscrambler.tiny=Remap Tiny names Index: src/test/kotlin/platform/minecraft/unpick/UnpickClassReferenceTest.kt =================================================================== --- src/test/kotlin/platform/minecraft/unpick/UnpickClassReferenceTest.kt (revision 4386bb995c4ad6c9a9fe6ce5906a6f32fab8af7a) +++ src/test/kotlin/platform/minecraft/unpick/UnpickClassReferenceTest.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -22,9 +22,13 @@ import com.demonwav.mcdev.framework.BaseMinecraftTest import com.demonwav.mcdev.framework.EdtInterceptor +import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickClassName +import com.intellij.openapi.command.WriteCommandAction import com.intellij.psi.JavaPsiFacade +import com.intellij.psi.PsiElement import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.search.searches.ReferencesSearch +import com.intellij.psi.util.PsiTreeUtil import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName @@ -65,6 +69,17 @@ configure = false, allowAst = true, ) + java( + "com/other/Target.java", + """ + package com.other; + + public class Target { + } + """, + configure = false, + allowAst = true, + ) unpick( "usages.unpick", """ @@ -120,6 +135,35 @@ Assertions.assertEquals(2, text.split("Outer\$Middle\$Renamed").size - 1, text) } + private fun usagesFile() = + fixture.psiManager.findFile(fixture.tempDirFixture.getFile("usages.unpick")!!)!! + + private fun bindFirstReference(targetName: String, newTarget: PsiElement) { + val reference = PsiTreeUtil.findChildrenOfType(usagesFile(), UnpickClassName::class.java) + .flatMap { it.references.asList() } + .first { it.canonicalText == targetName } + WriteCommandAction.runWriteCommandAction(project) { + reference.bindToElement(newTarget) + } + } + + @Test + @DisplayName("Bind Outer Class Keeps Nested Classes") + fun bindOuterClassKeepsNestedClasses() { + bindFirstReference("com.example.Outer", findClass("com.other.Target")) + val text = usagesFile().text + Assertions.assertTrue(text.contains("com.other.Target\$Middle\$Inner.INNER_VALUE"), text) + } + + @Test + @DisplayName("Bind Package Keeps Class Name") + fun bindPackageKeepsClassName() { + val newPackage = JavaPsiFacade.getInstance(project).findPackage("com.other")!! + bindFirstReference("com.example", newPackage) + val text = usagesFile().text + Assertions.assertTrue(text.contains("com.other.Outer\$Middle\$Inner.INNER_VALUE"), text) + } + private fun complete(text: String): List { fixture.configureByText("complete.unpick", text) return fixture.completeBasic()?.map { it.lookupString }.orEmpty() Index: src/test/kotlin/platform/minecraft/unpick/UnpickGroupNavigationTest.kt =================================================================== --- src/test/kotlin/platform/minecraft/unpick/UnpickGroupNavigationTest.kt (revision 4386bb995c4ad6c9a9fe6ce5906a6f32fab8af7a) +++ src/test/kotlin/platform/minecraft/unpick/UnpickGroupNavigationTest.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -26,6 +26,7 @@ import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickGroupRef import com.intellij.psi.PsiManager import com.intellij.psi.util.PsiTreeUtil +import com.intellij.refactoring.rename.RenameInputValidatorRegistry import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName @@ -114,6 +115,17 @@ } @Test + @DisplayName("Rename Input Validation") + fun renameInputValidation() { + val validator = RenameInputValidatorRegistry.getInputValidator(definitions().first())!! + Assertions.assertTrue(validator.value("Color_2\$")) + Assertions.assertFalse(validator.value("Color 2")) + Assertions.assertFalse(validator.value("2Color")) + Assertions.assertFalse(validator.value("Color-2")) + Assertions.assertFalse(validator.value("Coloré")) + } + + @Test @DisplayName("Documentation") fun documentation() { val doc = UnpickDocumentationProvider().generateDoc(definitions().first(), null)!! Index: src/test/kotlin/platform/minecraft/unpick/UnpickInspectionsTest.kt =================================================================== --- src/test/kotlin/platform/minecraft/unpick/UnpickInspectionsTest.kt (revision 4386bb995c4ad6c9a9fe6ce5906a6f32fab8af7a) +++ src/test/kotlin/platform/minecraft/unpick/UnpickInspectionsTest.kt (revision 4a6c6d74ed36ca4a828969b7f3cd510d7efbed82) @@ -30,9 +30,12 @@ import com.demonwav.mcdev.platform.minecraft.unpick.inspections.UnpickGroupTypeConflictInspection import com.demonwav.mcdev.platform.minecraft.unpick.inspections.UnpickTargetMismatchInspection import com.demonwav.mcdev.platform.minecraft.unpick.inspections.UnresolvedUnpickReferenceInspection +import com.demonwav.mcdev.platform.minecraft.unpick.index.UnpickGroupIndex import com.intellij.codeInspection.LocalInspectionTool +import com.intellij.util.indexing.FileBasedIndex import kotlin.reflect.KClass import org.intellij.lang.annotations.Language +import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test @@ -242,6 +245,39 @@ ) @Test + @DisplayName("Default Groups Are Not Indexed") + fun defaultGroupsAreNotIndexed() { + buildProject { + unpick( + "other.unpick", + """ + unpick v4 + group int + 1 + group int G + com.example.Constants.TWO + """, + configure = false, + ) + } + val keys = FileBasedIndex.getInstance().getAllKeys(UnpickGroupIndex.Util.NAME, project) + Assertions.assertFalse("" in keys, keys.toString()) + Assertions.assertTrue("G" in keys, keys.toString()) + + // Default groups in other files aren't compared, named groups are + doTest( + DuplicateUnpickConstantInspection::class, + """ + unpick v4 + group int + 1 + group int G + 2 + """, + ) + } + + @Test @DisplayName("Target Mismatches") fun targetMismatches() = doTest( UnpickTargetMismatchInspection::class,