From d97b135ed1d781ea76f155e49567befea9a5c470 Mon Sep 17 00:00:00 2001 From: Bawnorton Date: Tue, 17 Sep 2024 22:19:34 -0700 Subject: [PATCH 1/3] add json5 support to mixin config jsons --- .../mixin/config/MixinConfigFileType.kt | 25 ++++++++++++++----- .../config/MixinConfigImportOptimizer.kt | 2 +- .../inspection/MixinConfigInspection.kt | 2 +- .../MixinConfigReferenceContributor.kt | 15 ++++++++--- src/main/resources/META-INF/plugin.xml | 12 +++++++-- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/platform/mixin/config/MixinConfigFileType.kt b/src/main/kotlin/platform/mixin/config/MixinConfigFileType.kt index bd37ac5dd..a6cfb647c 100644 --- a/src/main/kotlin/platform/mixin/config/MixinConfigFileType.kt +++ b/src/main/kotlin/platform/mixin/config/MixinConfigFileType.kt @@ -22,22 +22,35 @@ package com.demonwav.mcdev.platform.mixin.config import com.demonwav.mcdev.asset.PlatformAssets import com.intellij.json.JsonLanguage +import com.intellij.json.json5.Json5Language import com.intellij.openapi.fileTypes.LanguageFileType import com.intellij.openapi.fileTypes.ex.FileTypeIdentifiableByVirtualFile import com.intellij.openapi.vfs.VirtualFile -object MixinConfigFileType : LanguageFileType(JsonLanguage.INSTANCE), FileTypeIdentifiableByVirtualFile { - - private val filenameRegex = "(^|\\.)mixins?(\\.[^.]+)*\\.json\$".toRegex() +interface MixinConfigFileType : FileTypeIdentifiableByVirtualFile { + fun getFilenameRegex() : Regex // Dynamic file type detection is sadly needed as we're overriding the built-in json file type. // Simply using an extension pattern is not sufficient as there is no way to bump the version to tell // the cache that the pattern has changed, as it now has, without changing the file type name. // See https://www.plugin-dev.com/intellij/custom-language/file-type-detection/#guidelines - override fun isMyFileType(file: VirtualFile) = file.name.contains(filenameRegex) + override fun isMyFileType(file: VirtualFile) = file.name.contains(getFilenameRegex()) - override fun getName() = "Mixin Configuration" override fun getDescription() = "Mixin configuration" override fun getDefaultExtension() = "" override fun getIcon() = PlatformAssets.MIXIN_ICON -} + + object Json : LanguageFileType(JsonLanguage.INSTANCE), MixinConfigFileType { + private val filenameRegex = "(^|\\.)mixins?(\\.[^.]+)*\\.json\$".toRegex() + + override fun getFilenameRegex() : Regex = filenameRegex + override fun getName() = "Mixin Json Configuration" + } + + object Json5 : LanguageFileType(Json5Language.INSTANCE), MixinConfigFileType { + private var filenameRegex = "(^|\\.)mixins?(\\.[^.]+)*\\.json5\$".toRegex() + + override fun getFilenameRegex() : Regex = filenameRegex + override fun getName() = "Mixin Json5 Configuration" + } +} \ No newline at end of file diff --git a/src/main/kotlin/platform/mixin/config/MixinConfigImportOptimizer.kt b/src/main/kotlin/platform/mixin/config/MixinConfigImportOptimizer.kt index 5bb35f5a0..c7f1ca12f 100644 --- a/src/main/kotlin/platform/mixin/config/MixinConfigImportOptimizer.kt +++ b/src/main/kotlin/platform/mixin/config/MixinConfigImportOptimizer.kt @@ -81,7 +81,7 @@ class MixinConfigImportOptimizer : ImportOptimizer { } } - override fun supports(file: PsiFile) = file is JsonFile && file.fileType == MixinConfigFileType + override fun supports(file: PsiFile) = file is JsonFile && file.fileType is MixinConfigFileType override fun processFile(file: PsiFile): Runnable { if (file !is JsonFile) { diff --git a/src/main/kotlin/platform/mixin/config/inspection/MixinConfigInspection.kt b/src/main/kotlin/platform/mixin/config/inspection/MixinConfigInspection.kt index bdeac9bd1..37b4cdd4c 100644 --- a/src/main/kotlin/platform/mixin/config/inspection/MixinConfigInspection.kt +++ b/src/main/kotlin/platform/mixin/config/inspection/MixinConfigInspection.kt @@ -34,7 +34,7 @@ abstract class MixinConfigInspection : LocalInspectionTool() { protected abstract fun buildVisitor(holder: ProblemsHolder): PsiElementVisitor private fun checkFile(file: PsiFile): Boolean { - return file.fileType === MixinConfigFileType && MixinModuleType.isInModule(file) + return file.fileType is MixinConfigFileType && MixinModuleType.isInModule(file) } final override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { diff --git a/src/main/kotlin/platform/mixin/config/reference/MixinConfigReferenceContributor.kt b/src/main/kotlin/platform/mixin/config/reference/MixinConfigReferenceContributor.kt index 5ffffbdc8..6e51475fb 100644 --- a/src/main/kotlin/platform/mixin/config/reference/MixinConfigReferenceContributor.kt +++ b/src/main/kotlin/platform/mixin/config/reference/MixinConfigReferenceContributor.kt @@ -26,16 +26,24 @@ import com.demonwav.mcdev.util.isPropertyValue import com.intellij.json.psi.JsonArray import com.intellij.json.psi.JsonStringLiteral import com.intellij.patterns.PlatformPatterns +import com.intellij.patterns.PsiElementPattern import com.intellij.patterns.StandardPatterns import com.intellij.psi.PsiReferenceContributor import com.intellij.psi.PsiReferenceRegistrar class MixinConfigReferenceContributor : PsiReferenceContributor() { - override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) { - val pattern = PlatformPatterns.psiElement(JsonStringLiteral::class.java) - .inFile(PlatformPatterns.psiFile().withFileType(StandardPatterns.`object`(MixinConfigFileType))) + println(this::class.qualifiedName) + val jsonPattern = PlatformPatterns.psiElement(JsonStringLiteral::class.java) + .inFile(PlatformPatterns.psiFile().withFileType(StandardPatterns.`object`(MixinConfigFileType.Json))) + val json5Pattern = PlatformPatterns.psiElement(JsonStringLiteral::class.java) + .inFile(PlatformPatterns.psiFile().withFileType(StandardPatterns.`object`(MixinConfigFileType.Json5))) + + registerWithPattern(registrar, jsonPattern) + registerWithPattern(registrar, json5Pattern) + } + private fun registerWithPattern(registrar: PsiReferenceRegistrar, pattern: PsiElementPattern.Capture) { registrar.registerReferenceProvider(pattern.isPropertyKey(), ConfigProperty) registrar.registerReferenceProvider(pattern.isPropertyValue("package"), MixinPackage) registrar.registerReferenceProvider(pattern.isPropertyValue("plugin"), MixinPlugin) @@ -47,3 +55,4 @@ class MixinConfigReferenceContributor : PsiReferenceContributor() { registrar.registerReferenceProvider(pattern.withParent(mixinList.isPropertyValue("client")), MixinClass) } } + diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 58b44c1bd..f01c10a05 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -720,14 +720,22 @@ id="Find Mixins"/> - + + + + From a09e18f306759fb1106acc76b35f74bb50320946 Mon Sep 17 00:00:00 2001 From: Bawnorton Date: Tue, 17 Sep 2024 22:25:59 -0700 Subject: [PATCH 2/3] remove debug line --- .../mixin/config/reference/MixinConfigReferenceContributor.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/kotlin/platform/mixin/config/reference/MixinConfigReferenceContributor.kt b/src/main/kotlin/platform/mixin/config/reference/MixinConfigReferenceContributor.kt index 6e51475fb..f4ac0fc49 100644 --- a/src/main/kotlin/platform/mixin/config/reference/MixinConfigReferenceContributor.kt +++ b/src/main/kotlin/platform/mixin/config/reference/MixinConfigReferenceContributor.kt @@ -33,7 +33,6 @@ import com.intellij.psi.PsiReferenceRegistrar class MixinConfigReferenceContributor : PsiReferenceContributor() { override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) { - println(this::class.qualifiedName) val jsonPattern = PlatformPatterns.psiElement(JsonStringLiteral::class.java) .inFile(PlatformPatterns.psiFile().withFileType(StandardPatterns.`object`(MixinConfigFileType.Json))) val json5Pattern = PlatformPatterns.psiElement(JsonStringLiteral::class.java) From b9ffd52feae28af1d66e7d88c53b54b419a0ed45 Mon Sep 17 00:00:00 2001 From: Bawnorton Date: Mon, 23 Sep 2024 09:09:51 -0700 Subject: [PATCH 3/3] use or pattern --- .../reference/MixinConfigReferenceContributor.kt | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/platform/mixin/config/reference/MixinConfigReferenceContributor.kt b/src/main/kotlin/platform/mixin/config/reference/MixinConfigReferenceContributor.kt index f4ac0fc49..433df566d 100644 --- a/src/main/kotlin/platform/mixin/config/reference/MixinConfigReferenceContributor.kt +++ b/src/main/kotlin/platform/mixin/config/reference/MixinConfigReferenceContributor.kt @@ -26,23 +26,20 @@ import com.demonwav.mcdev.util.isPropertyValue import com.intellij.json.psi.JsonArray import com.intellij.json.psi.JsonStringLiteral import com.intellij.patterns.PlatformPatterns -import com.intellij.patterns.PsiElementPattern import com.intellij.patterns.StandardPatterns import com.intellij.psi.PsiReferenceContributor import com.intellij.psi.PsiReferenceRegistrar class MixinConfigReferenceContributor : PsiReferenceContributor() { override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) { - val jsonPattern = PlatformPatterns.psiElement(JsonStringLiteral::class.java) - .inFile(PlatformPatterns.psiFile().withFileType(StandardPatterns.`object`(MixinConfigFileType.Json))) - val json5Pattern = PlatformPatterns.psiElement(JsonStringLiteral::class.java) - .inFile(PlatformPatterns.psiFile().withFileType(StandardPatterns.`object`(MixinConfigFileType.Json5))) + val anyMixinConfigFileType = StandardPatterns.or( + StandardPatterns.`object`(MixinConfigFileType.Json), + StandardPatterns.`object`(MixinConfigFileType.Json5) + ) - registerWithPattern(registrar, jsonPattern) - registerWithPattern(registrar, json5Pattern) - } + val pattern = PlatformPatterns.psiElement(JsonStringLiteral::class.java) + .inFile(PlatformPatterns.psiFile().withFileType(anyMixinConfigFileType)) - private fun registerWithPattern(registrar: PsiReferenceRegistrar, pattern: PsiElementPattern.Capture) { registrar.registerReferenceProvider(pattern.isPropertyKey(), ConfigProperty) registrar.registerReferenceProvider(pattern.isPropertyValue("package"), MixinPackage) registrar.registerReferenceProvider(pattern.isPropertyValue("plugin"), MixinPlugin)