Skip to content

Commit

Permalink
Add json5 support to mixin config jsons (#2375)
Browse files Browse the repository at this point in the history
* add json5 support to mixin config jsons

* remove debug line

* use or pattern
  • Loading branch information
Bawnorton authored Sep 23, 2024
1 parent 816437a commit 56b3764
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
25 changes: 19 additions & 6 deletions src/main/kotlin/platform/mixin/config/MixinConfigFileType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ import com.intellij.psi.PsiReferenceContributor
import com.intellij.psi.PsiReferenceRegistrar

class MixinConfigReferenceContributor : PsiReferenceContributor() {

override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
val anyMixinConfigFileType = StandardPatterns.or(
StandardPatterns.`object`(MixinConfigFileType.Json),
StandardPatterns.`object`(MixinConfigFileType.Json5)
)

val pattern = PlatformPatterns.psiElement(JsonStringLiteral::class.java)
.inFile(PlatformPatterns.psiFile().withFileType(StandardPatterns.`object`(MixinConfigFileType)))
.inFile(PlatformPatterns.psiFile().withFileType(anyMixinConfigFileType))

registrar.registerReferenceProvider(pattern.isPropertyKey(), ConfigProperty)
registrar.registerReferenceProvider(pattern.isPropertyValue("package"), MixinPackage)
Expand All @@ -47,3 +51,4 @@ class MixinConfigReferenceContributor : PsiReferenceContributor() {
registrar.registerReferenceProvider(pattern.withParent(mixinList.isPropertyValue("client")), MixinClass)
}
}

12 changes: 10 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -720,14 +720,22 @@
id="Find Mixins"/>

<!-- Mixin configuration -->
<fileType name="Mixin Configuration" language="JSON"
implementationClass="com.demonwav.mcdev.platform.mixin.config.MixinConfigFileType"
<fileType name="Mixin Json Configuration" language="JSON"
implementationClass="com.demonwav.mcdev.platform.mixin.config.MixinConfigFileType$Json"
fieldName="INSTANCE"/>
<psi.referenceContributor language="JSON"
implementation="com.demonwav.mcdev.platform.mixin.config.reference.MixinConfigReferenceContributor"/>
<lang.importOptimizer language="JSON"
implementationClass="com.demonwav.mcdev.platform.mixin.config.MixinConfigImportOptimizer"/>

<fileType name="Mixin Json5 Configuration" language="JSON5"
implementationClass="com.demonwav.mcdev.platform.mixin.config.MixinConfigFileType$Json5"
fieldName="INSTANCE"/>
<psi.referenceContributor language="JSON5"
implementation="com.demonwav.mcdev.platform.mixin.config.reference.MixinConfigReferenceContributor"/>
<lang.importOptimizer language="JSON5"
implementationClass="com.demonwav.mcdev.platform.mixin.config.MixinConfigImportOptimizer"/>

<!-- Mixin Line Marker Providers -->
<codeInsight.lineMarkerProvider language="JAVA"
implementationClass="com.demonwav.mcdev.platform.mixin.insight.MixinLineMarkerProvider"/>
Expand Down

0 comments on commit 56b3764

Please sign in to comment.