User: kyle wood
Date: 27 Nov 26 07:00
Revision: 852ad26352818dd4aa6af81b62065e52db1ecc52
Summary:
Index unpick constant conflicts and cache per-file summaries
Add conflict keys to UnpickGroupIndex, one per group name, type and scope,
so DuplicateUnpickConstantInspection only looks at files whose definitions
can actually conflict. Each file caches its evaluated constants by conflict
key and value. The cache ignores edits to other unpick files, so editing one
file doesn't re-evaluate all the others.
Definitions now conflict when they have any scope in common instead of only
when their scope sets match exactly. Group name keys are now prefixed, and
the index version is bumped to 2.
TeamCity URL: http://ci.mcdev.io:80/viewModification.html?tab=vcsModificationFiles&modId=10825&personal=false
Index: src/main/kotlin/platform/minecraft/unpick/UnpickConstantConflicts.kt
===================================================================
--- src/main/kotlin/platform/minecraft/unpick/UnpickConstantConflicts.kt (revision 852ad26352818dd4aa6af81b62065e52db1ecc52)
+++ src/main/kotlin/platform/minecraft/unpick/UnpickConstantConflicts.kt (revision 852ad26352818dd4aa6af81b62065e52db1ecc52)
@@ -0,0 +1,118 @@
+/*
+ * 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.platform.minecraft.unpick.eval.UnpickExpressionEvaluator
+import com.demonwav.mcdev.platform.minecraft.unpick.eval.UnpickExpressionEvaluator.EvaluatedConstant
+import com.demonwav.mcdev.platform.minecraft.unpick.eval.UnpickValue
+import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickGroupDefinition
+import com.demonwav.mcdev.platform.minecraft.unpick.index.UnpickGroupIndex
+import com.demonwav.mcdev.util.equivalentTo
+import com.intellij.lang.Language
+import com.intellij.psi.util.CachedValueProvider
+import com.intellij.psi.util.CachedValuesManager
+import com.intellij.psi.util.PsiModificationTracker
+
+/**
+ * Finds group constants that evaluate to the same value as another constant in the same group, where both can apply.
+ */
+object UnpickConstantConflicts {
+
+ /**
+ * [constant] and [other] both evaluate to [value], which is converted to the group type.
+ */
+ class Conflict(val constant: EvaluatedConstant, val value: UnpickValue, val other: EvaluatedConstant)
+
+ /**
+ * Returns a conflict for each constant of [definition] whose value is shared by another constant of the same group
+ * and type. Definitions only conflict when both are unscoped or they have a scope in common. [Conflict.other] is
+ * from the same file when possible.
+ */
+ fun findConflicts(definition: UnpickGroupDefinition): List {
+ val groupType = groupType(definition) ?: return emptyList()
+ val keys = UnpickGroupIndex.Util.conflictKeys(definition)
+ val summaries = UnpickGroups.findFiles(definition, keys).map(::summary)
+
+ val result = mutableListOf()
+ for (constant in definition.groupConstantList) {
+ for (evaluated in UnpickExpressionEvaluator.evaluate(constant).values) {
+ val value = groupValue(evaluated, groupType) ?: continue
+ val other = summaries.firstNotNullOfOrNull { summary ->
+ keys.firstNotNullOfOrNull { key ->
+ summary[key]?.get(value)?.firstOrNull { !it.isSameAs(evaluated) }
+ }
+ } ?: continue
+ result += Conflict(evaluated, value, other)
+ }
+ }
+ return result
+ }
+
+ /**
+ * The constants of a file, grouped by conflict key and then by value.
+ */
+ private fun summary(file: UnpickFile): Map>> {
+ return CachedValuesManager.getCachedValue(file) {
+ // Constants only depend on their own file and on Java. Every PSI change counts towards Language.ANY, so it
+ // has to be excluded for edits to other unpick files to keep this cached.
+ val otherLanguages = PsiModificationTracker.getInstance(file.project)
+ .forLanguages { it != Language.ANY && !it.isKindOf(UnpickLanguage) }
+ CachedValueProvider.Result.create(buildSummary(file), file, otherLanguages)
+ }
+ }
+
+ private fun buildSummary(file: UnpickFile): Map>> {
+ val summary = HashMap>>()
+ for (definition in file.groupDefinitions) {
+ val groupType = groupType(definition) ?: continue
+ val keys = UnpickGroupIndex.Util.conflictKeys(definition)
+ for (constant in definition.groupConstantList) {
+ for (evaluated in UnpickExpressionEvaluator.evaluate(constant).values) {
+ val value = groupValue(evaluated, groupType) ?: continue
+ for (key in keys) {
+ summary.getOrPut(key, ::HashMap).getOrPut(value, ::ArrayList) += evaluated
+ }
+ }
+ }
+ }
+ return summary
+ }
+
+ private fun groupType(definition: UnpickGroupDefinition): DataType? {
+ return definition.dataType?.dataType?.takeIf { it.isValidGroupType }
+ }
+
+ /**
+ * Returns the value of the constant converted to the group type, or `null` if it's unknown or of the wrong type.
+ */
+ private fun groupValue(evaluated: EvaluatedConstant, groupType: DataType): UnpickValue? {
+ // Constants of the wrong type are reported by UnpickExpressionTypeInspection
+ if (!evaluated.value.type.canWidenTo(groupType)) {
+ return null
+ }
+ return evaluated.value.convertTo(groupType).takeIf { it.isKnown }
+ }
+
+ private fun EvaluatedConstant.isSameAs(other: EvaluatedConstant): Boolean {
+ return constant equivalentTo other.constant && wildcardField == other.wildcardField
+ }
+}
Index: src/main/kotlin/platform/minecraft/unpick/UnpickGroups.kt
===================================================================
--- src/main/kotlin/platform/minecraft/unpick/UnpickGroups.kt (revision e5ff5ed95e8881d95b7cb8f99b4a58ddcc0085fa)
+++ src/main/kotlin/platform/minecraft/unpick/UnpickGroups.kt (revision 852ad26352818dd4aa6af81b62065e52db1ecc52)
@@ -23,6 +23,7 @@
import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickGroupDefinition
import com.demonwav.mcdev.platform.minecraft.unpick.index.UnpickGroupIndex
import com.intellij.openapi.project.DumbService
+import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiManager
import com.intellij.psi.search.GlobalSearchScope
@@ -31,10 +32,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 named group.
*/
- 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, listOf(UnpickGroupIndex.Util.nameKey(name))).flatMap { file ->
file.groupDefinitions.filter { it.name == name }
}
}
@@ -54,9 +55,10 @@
index.processAllKeys(
UnpickGroupIndex.Util.NAME,
{ key ->
- if (key != UnpickGroupIndex.Util.DEFAULT_GROUP_KEY && key !in result) {
+ val name = UnpickGroupIndex.Util.nameFromKey(key)
+ if (name != null && name !in result) {
if (index.getContainingFiles(UnpickGroupIndex.Util.NAME, key, scope).isNotEmpty()) {
- result += key
+ result += name
}
}
true
@@ -68,19 +70,30 @@
return result
}
- private fun findFiles(context: PsiElement, key: String): List {
+ /**
+ * Finds the files visible from [context] that have any of the given [UnpickGroupIndex] keys. The file containing
+ * [context] is always first. In dumb mode, only that file is returned.
+ */
+ fun findFiles(context: PsiElement, keys: Collection): List {
val project = context.project
val ownFile = context.containingFile?.originalFile as? UnpickFile
if (DumbService.isDumb(project)) {
return listOfNotNull(ownFile)
}
+ val index = FileBasedIndex.getInstance()
+ val scope = searchScope(context)
+ val virtualFiles = LinkedHashSet()
+ for (key in keys) {
+ virtualFiles += index.getContainingFiles(UnpickGroupIndex.Util.NAME, key, scope)
+ }
val psiManager = PsiManager.getInstance(project)
- val files = FileBasedIndex.getInstance()
- .getContainingFiles(UnpickGroupIndex.Util.NAME, key, searchScope(context))
- val result = files.mapNotNull { psiManager.findFile(it) as? UnpickFile }.toMutableList()
- if (ownFile != null && ownFile !in result) {
- result += ownFile
+ val result = listOfNotNull(ownFile).toMutableList()
+ for (file in virtualFiles) {
+ val psiFile = psiManager.findFile(file) as? UnpickFile ?: continue
+ if (psiFile != ownFile) {
+ result += psiFile
- }
+ }
+ }
return result
}
Index: src/main/kotlin/platform/minecraft/unpick/index/UnpickGroupIndex.kt
===================================================================
--- src/main/kotlin/platform/minecraft/unpick/index/UnpickGroupIndex.kt (revision e5ff5ed95e8881d95b7cb8f99b4a58ddcc0085fa)
+++ src/main/kotlin/platform/minecraft/unpick/index/UnpickGroupIndex.kt (revision 852ad26352818dd4aa6af81b62065e52db1ecc52)
@@ -22,6 +22,7 @@
import com.demonwav.mcdev.platform.minecraft.unpick.UnpickFile
import com.demonwav.mcdev.platform.minecraft.unpick.UnpickFileType
+import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickGroupDefinition
import com.intellij.util.indexing.DataIndexer
import com.intellij.util.indexing.DefaultFileTypeSpecificInputFilter
import com.intellij.util.indexing.FileBasedIndex
@@ -34,7 +35,7 @@
class UnpickGroupIndex : ScalarIndexExtension() {
override fun getName() = Util.NAME
- override fun getVersion() = 1
+ override fun getVersion() = 2
override fun dependsOnFileContent() = true
@@ -47,13 +48,48 @@
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 }
+ val keys = HashMap()
+ for (definition in file.groupDefinitions) {
+ definition.name?.let { keys[Util.nameKey(it)] = null }
+ for (key in Util.conflictKeys(definition)) {
+ keys[key] = null
- }
- }
+ }
+ }
+ return keys
+ }
+ }
object Util {
val NAME = ID.create("mcdev.UnpickGroupIndex")
- const val DEFAULT_GROUP_KEY = ""
+ private const val NAME_PREFIX = "n:"
+ private const val CONFLICT_PREFIX = "c:"
+ private val WHITESPACE = Regex("\\s+")
+
+ /**
+ * The key of files that define the named group.
+ */
+ fun nameKey(name: String) = NAME_PREFIX + name
+
+ /**
+ * Returns the group name of a key from [nameKey], or `null` for any other key.
+ */
+ fun nameFromKey(key: String): String? = key.takeIf { it.startsWith(NAME_PREFIX) }?.removePrefix(NAME_PREFIX)
+
+ /**
+ * Returns the keys of [definition] for finding constants it can conflict with. Two definitions share a key when
+ * they belong to the same group, have the same type, and are both unscoped or have a scope in common.
+ *
+ * Returns an empty list if the definition doesn't have a valid group type.
+ */
+ fun conflictKeys(definition: UnpickGroupDefinition): List {
+ val type = definition.dataType?.dataType?.takeIf { it.isValidGroupType } ?: return emptyList()
+ val prefix = "$CONFLICT_PREFIX${definition.name.orEmpty()}:${type.keyword}:"
+ val scopes = definition.scopes
+ if (scopes.isEmpty()) {
+ return listOf(prefix)
- }
+ }
+ return scopes.map { prefix + it.text.split(WHITESPACE).joinToString(" ") }.distinct()
-}
+ }
+ }
+}
Index: src/main/kotlin/platform/minecraft/unpick/inspections/DuplicateUnpickConstantInspection.kt
===================================================================
--- src/main/kotlin/platform/minecraft/unpick/inspections/DuplicateUnpickConstantInspection.kt (revision e5ff5ed95e8881d95b7cb8f99b4a58ddcc0085fa)
+++ src/main/kotlin/platform/minecraft/unpick/inspections/DuplicateUnpickConstantInspection.kt (revision 852ad26352818dd4aa6af81b62065e52db1ecc52)
@@ -21,15 +21,12 @@
package com.demonwav.mcdev.platform.minecraft.unpick.inspections
import com.demonwav.mcdev.asset.MCDevBundle
-import com.demonwav.mcdev.platform.minecraft.unpick.UnpickGroups
-import com.demonwav.mcdev.platform.minecraft.unpick.eval.UnpickExpressionEvaluator
+import com.demonwav.mcdev.platform.minecraft.unpick.UnpickConstantConflicts
import com.demonwav.mcdev.platform.minecraft.unpick.eval.UnpickExpressionEvaluator.EvaluatedConstant
-import com.demonwav.mcdev.platform.minecraft.unpick.eval.UnpickValue
import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickFieldExpression
import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickGroupDefinition
import com.demonwav.mcdev.platform.minecraft.unpick.gen.psi.UnpickVisitor
import com.demonwav.mcdev.platform.minecraft.unpick.psi.UnpickElementFactory
-import com.demonwav.mcdev.util.equivalentTo
import com.intellij.codeInspection.LocalInspectionTool
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.PsiElementVisitor
@@ -39,63 +36,28 @@
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor =
object : UnpickVisitor() {
override fun visitGroupDefinition(element: UnpickGroupDefinition) {
- 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)
- .filter { it.dataType?.dataType == groupType && scopeKey(it) == scopes }
- .ifEmpty { listOf(element) }
-
- val byValue = HashMap>()
- for (definition in definitions) {
- for (constant in definition.groupConstantList) {
- for (evaluated in UnpickExpressionEvaluator.evaluate(constant).values) {
- // Constants of the wrong type are reported by UnpickExpressionTypeInspection
- if (!evaluated.value.type.canWidenTo(groupType)) {
- continue
- }
- val value = evaluated.value.convertTo(groupType)
- if (value.isKnown) {
- byValue.getOrPut(value) { mutableListOf() } += evaluated
- }
- }
- }
- }
-
- for (constant in element.groupConstantList) {
- for (evaluated in UnpickExpressionEvaluator.evaluate(constant).values) {
- if (!evaluated.value.type.canWidenTo(groupType)) {
- continue
- }
- val value = evaluated.value.convertTo(groupType)
- val other = byValue[value]?.firstOrNull { !it.isSameAs(evaluated) } ?: continue
- val wildcardField = evaluated.wildcardField
+ for (conflict in UnpickConstantConflicts.findConflicts(element)) {
+ val wildcardField = conflict.constant.wildcardField
- holder.registerProblem(
+ holder.registerProblem(
- constant,
+ conflict.constant.constant,
- if (wildcardField == null) {
- MCDevBundle(
- "inspection.duplicate_unpick_constant.message",
+ if (wildcardField == null) {
+ MCDevBundle(
+ "inspection.duplicate_unpick_constant.message",
- value.toDisplayString(),
- other.expandedText(),
+ conflict.value.toDisplayString(),
+ conflict.other.expandedText(),
- )
- } else {
- MCDevBundle(
- "inspection.duplicate_unpick_constant.wildcard_message",
+ )
+ } else {
+ MCDevBundle(
+ "inspection.duplicate_unpick_constant.wildcard_message",
- value.toDisplayString(),
+ conflict.value.toDisplayString(),
- "${wildcardField.containingClass?.name}.${wildcardField.name}",
+ "${wildcardField.containingClass?.name}.${wildcardField.name}",
- other.expandedText(),
+ conflict.other.expandedText(),
- )
- },
- )
- }
- }
+ )
+ },
+ )
+ }
+ }
- }
- private fun EvaluatedConstant.isSameAs(other: EvaluatedConstant): Boolean {
- return constant equivalentTo other.constant && wildcardField == other.wildcardField
- }
-
/**
* Returns the text of the constant, with the wildcard replaced by the field it was expanded to.
*/
@@ -108,9 +70,5 @@
wildcard.replace(UnpickElementFactory.createFieldName(constant.project, field.name))
return copy.text
}
-
- private fun scopeKey(definition: UnpickGroupDefinition): Set {
- return definition.scopes.mapTo(HashSet()) { scope -> scope.text.split(Regex("\\s+")).joinToString(" ") }
- }
- }
+ }
+}
-}