User: joe Date: 22 Nov 26 07:35 Revision: 9868154fcedfab64c3a53f247e18073499fecec8 Summary: Review round 2 TeamCity URL: http://ci.mcdev.io:80/viewModification.html?tab=vcsModificationFiles&modId=10816&personal=false Index: src/main/kotlin/platform/minecraft/unpick/UnpickAnnotator.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/UnpickAnnotator.kt (revision 1a3346016a9eba14b618a26093b35aeed7419d68) +++ src/main/kotlin/platform/minecraft/unpick/UnpickAnnotator.kt (revision 9868154fcedfab64c3a53f247e18073499fecec8) @@ -42,6 +42,8 @@ import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickTargetMethod import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickTypes import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickUnknownAttribute +import com.demonwav.mcdev.util.isValidFieldDescriptor +import com.demonwav.mcdev.util.isValidMethodDescriptor import com.intellij.lang.annotation.AnnotationHolder import com.intellij.lang.annotation.Annotator import com.intellij.lang.annotation.HighlightSeverity @@ -116,10 +118,10 @@ UnpickTypes.KEYWORD -> if (element.parent !is UnpickGroupAttribute) { holder.highlight(element, UnpickSyntaxHighlighter.KEYWORD) } - UnpickTypes.METHOD_DESCRIPTOR -> if (!UnpickDescriptors.isValidMethod(element.text)) { + UnpickTypes.METHOD_DESCRIPTOR -> if (!isValidMethodDescriptor(element.text)) { holder.error(MCDevBundle("minecraft.unpick.descriptor.invalid_method"), element) } - UnpickTypes.FIELD_DESCRIPTOR -> if (!UnpickDescriptors.isValidField(element.text)) { + UnpickTypes.FIELD_DESCRIPTOR -> if (!isValidFieldDescriptor(element.text)) { holder.error(MCDevBundle("minecraft.unpick.descriptor.invalid_field"), element) } } Index: src/main/kotlin/platform/minecraft/unpick/UnpickCompletionContributor.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/UnpickCompletionContributor.kt (revision 1a3346016a9eba14b618a26093b35aeed7419d68) +++ src/main/kotlin/platform/minecraft/unpick/UnpickCompletionContributor.kt (revision 9868154fcedfab64c3a53f247e18073499fecec8) @@ -21,6 +21,7 @@ package com.demonwav.mcdev.platform.minecraft.unpick import com.demonwav.mcdev.asset.PlatformAssets +import com.demonwav.mcdev.platform.minecraft.unpick.UnpickParserDefinition.TokenSets import com.demonwav.mcdev.util.descriptor import com.demonwav.mcdev.util.findMethodsByInternalName import com.demonwav.mcdev.util.findQualifiedClass @@ -55,13 +56,15 @@ return } val file = parameters.originalFile as? UnpickFile ?: return + // Nothing can be completed inside comments and literals + val tokenType = parameters.position.node.elementType + if (tokenType in TokenSets.COMMENTS || tokenType in TokenSets.STRINGS) { + return + } val text = file.viewProvider.contents val offset = parameters.offset.coerceAtMost(text.length) val lineStart = text.lastIndexOf('\n', offset - 1) + 1 val line = text.substring(lineStart, offset) - if ('#' in line) { - return - } if (lineStart == 0) { if (file.header == null && Const.HEADER_PREFIX.matches(line)) { Index: src/main/kotlin/platform/minecraft/unpick/UnpickDescriptors.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/UnpickDescriptors.kt (revision 1a3346016a9eba14b618a26093b35aeed7419d68) +++ src/main/kotlin/platform/minecraft/unpick/UnpickDescriptors.kt (revision 1a3346016a9eba14b618a26093b35aeed7419d68) @@ -1,35 +0,0 @@ -/* - * 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 - -/** - * Validation of JVM descriptors appearing in unpick files. Valid descriptors can be parsed with ASM's - * [org.objectweb.asm.Type]. - */ -object UnpickDescriptors { - private const val FIELD_DESCRIPTOR = "\\[*(?:[BCDFIJSZ]|L[^.;\\[()]+;)" - private val FIELD_DESCRIPTOR_REGEX = Regex(FIELD_DESCRIPTOR) - private val METHOD_DESCRIPTOR_REGEX = Regex("\\((?:$FIELD_DESCRIPTOR)*\\)(?:V|$FIELD_DESCRIPTOR)") - - fun isValidField(descriptor: String) = FIELD_DESCRIPTOR_REGEX.matches(descriptor) - - fun isValidMethod(descriptor: String) = METHOD_DESCRIPTOR_REGEX.matches(descriptor) -} Index: src/main/kotlin/platform/minecraft/unpick/eval/UnpickExpressionEvaluator.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/eval/UnpickExpressionEvaluator.kt (revision 1a3346016a9eba14b618a26093b35aeed7419d68) +++ src/main/kotlin/platform/minecraft/unpick/eval/UnpickExpressionEvaluator.kt (revision 9868154fcedfab64c3a53f247e18073499fecec8) @@ -50,6 +50,7 @@ import com.intellij.psi.util.PsiModificationTracker import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiUtil +import com.intellij.psi.util.TypeConversionUtil import com.intellij.psi.util.elementType /** @@ -143,38 +144,30 @@ } else { groupType != null && fieldType.canWidenTo(groupType) } - typeMatches && constantValue(field, fieldType) != null + typeMatches && constantValue(field) != null } } /** - * Returns the value of a constant field of the given type, or `null` if the field isn't a constant. A constant - * field is final and initialized to a compile-time constant (JLS 15.29), `null`, a class literal or another - * constant field. + * Returns the value of a constant field, or `null` if the field isn't a constant or its type isn't a [DataType]. + * A constant field is final and initialized to a compile-time constant (JLS 15.29), `null`, a class literal or + * another constant field. * * If the field is compiled and has no source, `null` and `Class` constants can't be distinguished from * non-constant fields, so an unknown value is returned for them. */ - fun constantValue(field: PsiField, type: DataType): UnpickValue? = constantValue(field, type, 0) + fun constantValue(field: PsiField): UnpickValue? = constantValue(field, 0) - private fun constantValue(field: PsiField, type: DataType, depth: Int): UnpickValue? { + private fun constantValue(field: PsiField, depth: Int): UnpickValue? { + val type = DataType.fromPsiType(field.type) ?: return null if (!field.hasModifierProperty(PsiModifier.FINAL)) { return null } if (type.isPrimitive) { - val value = field.computeConstantValue() - val valueType = when (value) { - is Byte -> DataType.BYTE - is Short -> DataType.SHORT - is Char -> DataType.CHAR - is Int -> DataType.INT - is Long -> DataType.LONG - is Float -> DataType.FLOAT - is Double -> DataType.DOUBLE - else -> return null + // Java's implementations return a value of the field's type, but this isn't guaranteed for all languages + val value = TypeConversionUtil.computeCastTo(field.computeConstantValue(), field.type) ?: return null + return UnpickValue(type, value) - } + } - return UnpickValue(valueType, value).convertTo(type) - } if (type == DataType.STRING) { (field.computeConstantValue() as? String)?.let { return UnpickValue(DataType.STRING, it) } } else { @@ -199,7 +192,7 @@ is PsiReferenceExpression -> { val referenced = initializer.resolve() as? PsiField if (referenced != null && depth < MAX_FIELD_DEPTH && DataType.fromPsiType(referenced.type) == type) { - constantValue(referenced, type, depth + 1) + constantValue(referenced, depth + 1) } else { null } @@ -462,7 +455,7 @@ ) } - val value = constantValue(field, fieldType) + val value = constantValue(field) if (value == null) { fieldProblem(nameElement, "minecraft.unpick.eval.field_not_constant", name) return UnpickValue.unknown(fieldType) Index: src/main/kotlin/platform/minecraft/unpick/inspections/UnpickTargetMismatchInspection.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/inspections/UnpickTargetMismatchInspection.kt (revision 1a3346016a9eba14b618a26093b35aeed7419d68) +++ src/main/kotlin/platform/minecraft/unpick/inspections/UnpickTargetMismatchInspection.kt (revision 9868154fcedfab64c3a53f247e18073499fecec8) @@ -22,7 +22,6 @@ import com.demonwav.mcdev.asset.MCDevBundle import com.demonwav.mcdev.platform.minecraft.unpick.DataType -import com.demonwav.mcdev.platform.minecraft.unpick.UnpickDescriptors import com.demonwav.mcdev.platform.minecraft.unpick.UnpickLiterals import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickGroupRef import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickTargetAnnotation @@ -30,6 +29,8 @@ import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickTargetMethod import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickVisitor import com.demonwav.mcdev.util.descriptor +import com.demonwav.mcdev.util.isValidFieldDescriptor +import com.demonwav.mcdev.util.isValidMethodDescriptor import com.intellij.codeInspection.LocalInspectionTool import com.intellij.codeInspection.ProblemsHolder import com.intellij.psi.PsiElementVisitor @@ -39,7 +40,7 @@ override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = object : UnpickVisitor() { override fun visitTargetMethod(element: UnpickTargetMethod) { - val descriptor = element.methodDescriptor?.text?.takeIf(UnpickDescriptors::isValidMethod) ?: return + val descriptor = element.methodDescriptor?.text?.takeIf(::isValidMethodDescriptor) ?: return val parameters = Type.getArgumentTypes(descriptor) val returnType = Type.getReturnType(descriptor) @@ -91,7 +92,7 @@ override fun visitTargetField(element: UnpickTargetField) { val descriptorElement = element.fieldDescriptor ?: return val descriptor = descriptorElement.text - if (!UnpickDescriptors.isValidField(descriptor)) { + if (!isValidFieldDescriptor(descriptor)) { return } val type = Type.getType(descriptor) Index: src/main/kotlin/platform/minecraft/unpick/psi/UnpickClassReference.kt =================================================================== --- src/main/kotlin/platform/minecraft/unpick/psi/UnpickClassReference.kt (revision 1a3346016a9eba14b618a26093b35aeed7419d68) +++ src/main/kotlin/platform/minecraft/unpick/psi/UnpickClassReference.kt (revision 9868154fcedfab64c3a53f247e18073499fecec8) @@ -22,9 +22,8 @@ import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickClassName import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickTypes -import com.demonwav.mcdev.util.anonymousClasses import com.demonwav.mcdev.util.findQualifiedClass -import com.demonwav.mcdev.util.localClasses +import com.demonwav.mcdev.util.namedAndUnnamedInnerClasses import com.intellij.codeInsight.lookup.LookupElementBuilder import com.intellij.openapi.util.TextRange import com.intellij.psi.JavaPsiFacade @@ -68,7 +67,8 @@ val isInnerClass = targetName.lastIndexOf('$') > targetName.lastIndexOf('.') if (isInnerClass) { val outer = findQualifiedClass(parentName, element) ?: return ArrayUtil.EMPTY_OBJECT_ARRAY - return innerClassNames(outer).map { (name, psiClass) -> LookupElementBuilder.create(psiClass, name) } + return outer.namedAndUnnamedInnerClasses + .map { (name, psiClass) -> LookupElementBuilder.create(psiClass, name) } .toTypedArray() } val parentPackage = facade.findPackage(parentName) ?: return ArrayUtil.EMPTY_OBJECT_ARRAY @@ -82,22 +82,6 @@ return (packages + classes).toTypedArray() } - /** - * Returns the binary names of the classes directly inside [outer] relative to it, including local and anonymous - * classes, together with the classes they name. - */ - private fun innerClassNames(outer: PsiClass): List> { - val named = outer.innerClasses.mapNotNull { innerClass -> innerClass.name?.let { it to innerClass } } - val localIndices = HashMap() - val local = outer.localClasses.mapNotNull { localClass -> - val name = localClass.name ?: return@mapNotNull null - val index = localIndices.merge(name, 1, Int::plus) - "$index$name" to localClass - } - val anonymous = outer.anonymousClasses.mapIndexed { index, anonymousClass -> "${index + 1}" to anonymousClass } - return named + local + anonymous - } - override fun handleElementRename(newElementName: String): PsiElement { val identifier = element.node.getChildren(null).firstOrNull { it.elementType == UnpickTypes.IDENTIFIER && Index: src/main/kotlin/util/bytecode-utils.kt =================================================================== --- src/main/kotlin/util/bytecode-utils.kt (revision 1a3346016a9eba14b618a26093b35aeed7419d68) +++ src/main/kotlin/util/bytecode-utils.kt (revision 9868154fcedfab64c3a53f247e18073499fecec8) @@ -3,7 +3,7 @@ * * https://mcdev.io/ * - * Copyright (C) 2025 minecraft-dev + * 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 @@ -101,6 +101,22 @@ } } +private const val FIELD_DESCRIPTOR = "\\[*(?:[BCDFIJSZ]|L[^.;\\[()]+;)" +private val FIELD_DESCRIPTOR_REGEX = Regex(FIELD_DESCRIPTOR) +private val METHOD_DESCRIPTOR_REGEX = Regex("\\((?:$FIELD_DESCRIPTOR)*\\)(?:V|$FIELD_DESCRIPTOR)") + +/** + * Whether [descriptor] is a structurally valid JVM field descriptor (JVMS 4.3.2), which can be parsed with ASM's + * [org.objectweb.asm.Type]. + */ +fun isValidFieldDescriptor(descriptor: String) = FIELD_DESCRIPTOR_REGEX.matches(descriptor) + +/** + * Whether [descriptor] is a structurally valid JVM method descriptor (JVMS 4.3.3), which can be parsed with ASM's + * [org.objectweb.asm.Type]. + */ +fun isValidMethodDescriptor(descriptor: String) = METHOD_DESCRIPTOR_REGEX.matches(descriptor) + fun parseClassDescriptor(descriptor: String): String { val internalName = descriptor.substring(1, descriptor.length - 1) return internalName.replace('/', '.') Index: src/main/kotlin/util/class-utils.kt =================================================================== --- src/main/kotlin/util/class-utils.kt (revision 1a3346016a9eba14b618a26093b35aeed7419d68) +++ src/main/kotlin/util/class-utils.kt (revision 9868154fcedfab64c3a53f247e18073499fecec8) @@ -3,7 +3,7 @@ * * https://mcdev.io/ * - * Copyright (C) 2025 minecraft-dev + * 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 @@ -261,6 +261,24 @@ return list } +/** + * The classes directly inside this class, including local and anonymous classes, paired with their binary names + * relative to this class (the part after the `$`). For example, the first local class named `Foo` is `1Foo`, and the + * second anonymous class is `2`. + */ +val PsiClass.namedAndUnnamedInnerClasses: List> + get() { + val named = innerClasses.mapNotNull { innerClass -> innerClass.name?.let { it to innerClass } } + val localIndices = HashMap() + val local = localClasses.mapNotNull { localClass -> + val name = localClass.name ?: return@mapNotNull null + val index = localIndices.merge(name, 1, Int::plus) + "$index$name" to localClass + } + val anonymous = anonymousClasses.mapIndexed { index, anonymousClass -> "${index + 1}" to anonymousClass } + return named + local + anonymous + } + private val INNER_INDEX_AND_NAME_REGEX = "(\\d+)(\\w*)".toRegex() fun innerIndexAndName(className: String): Pair? { val (indexStr, name) = INNER_INDEX_AND_NAME_REGEX.matchEntire(className)?.destructured ?: return null Index: src/test/kotlin/platform/minecraft/unpick/UnpickClassReferenceTest.kt =================================================================== --- src/test/kotlin/platform/minecraft/unpick/UnpickClassReferenceTest.kt (revision 1a3346016a9eba14b618a26093b35aeed7419d68) +++ src/test/kotlin/platform/minecraft/unpick/UnpickClassReferenceTest.kt (revision 9868154fcedfab64c3a53f247e18073499fecec8) @@ -190,4 +190,31 @@ fixture.editor.document.text, ) } + + @Test + @DisplayName("Completion After Hash In String") + fun completionAfterHashInString() { + complete("unpick v4\ngroup String\n \"a#b\" + Inne\n") + if (!fixture.editor.document.text.contains("com.example.Outer\$Middle\$Inner")) { + fixture.lookup?.let { lookup -> + fixture.lookup.currentItem = lookup.items.first { it.lookupString.endsWith("Inner") } + fixture.finishLookup('\n') -} + } + } + Assertions.assertTrue( + fixture.editor.document.text.contains("\"a#b\" + com.example.Outer\$Middle\$Inner"), + fixture.editor.document.text, + ) + } + + @Test + @DisplayName("No Completion In Comments And Literals") + fun noCompletionInCommentsAndLiterals() { + for (line in listOf("1 # Inne", "\"Inne\"")) { + val text = "unpick v4\ngroup String\n $line\n" + val lookups = complete(text) + Assertions.assertEquals(emptyList(), lookups) + Assertions.assertEquals(text.replace("", ""), fixture.editor.document.text) + } + } +} Index: unpick-review.md =================================================================== --- unpick-review.md (revision 1a3346016a9eba14b618a26093b35aeed7419d68) +++ unpick-review.md (revision 1a3346016a9eba14b618a26093b35aeed7419d68) @@ -1,4 +0,0 @@ -- In `UnpickCompletionContributor`, it won't complete if `#` is in the line, even if that `#` is inside a string or char token. -- In `UnpickExpressionEvaluator.constantValue`, check if `field.computeConstantValue` is contractually guaranteed to return a value of the field's type, or `null`. If it is, the conversion below it can be elided. If not, you can still use `TypeConversionUtil.computeCastTo` to simplify this code and avoid the `when` statement. -- `UnpickDescriptors` doesn't contain anything specific to unpick. The stuff inside could be moved to generic ASM utilities. -- `UnpickClassReference.innerClassNames` could maybe also be a more generic utility, next to the other related utilities. \ No newline at end of file