|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2024 minecraft-dev |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published |
| 10 | + * by the Free Software Foundation, version 3.0 only. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.demonwav.mcdev.yaml |
| 22 | + |
| 23 | +import com.demonwav.mcdev.facet.MinecraftFacet |
| 24 | +import com.demonwav.mcdev.platform.bukkit.PaperModuleType |
| 25 | +import com.demonwav.mcdev.platform.bukkit.SpigotModuleType |
| 26 | +import com.demonwav.mcdev.platform.bukkit.util.BukkitConstants |
| 27 | +import com.demonwav.mcdev.platform.bungeecord.BungeeCordModuleType |
| 28 | +import com.demonwav.mcdev.platform.bungeecord.util.BungeeCordConstants |
| 29 | +import com.demonwav.mcdev.util.findModule |
| 30 | +import com.intellij.codeInsight.completion.JavaLookupElementBuilder |
| 31 | +import com.intellij.lang.jvm.JvmModifier |
| 32 | +import com.intellij.openapi.util.TextRange |
| 33 | +import com.intellij.patterns.ObjectPattern |
| 34 | +import com.intellij.patterns.PatternCondition |
| 35 | +import com.intellij.patterns.PlatformPatterns |
| 36 | +import com.intellij.psi.JavaPsiFacade |
| 37 | +import com.intellij.psi.PsiElement |
| 38 | +import com.intellij.psi.PsiReference |
| 39 | +import com.intellij.psi.PsiReferenceContributor |
| 40 | +import com.intellij.psi.PsiReferenceRegistrar |
| 41 | +import com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference |
| 42 | +import com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceProvider |
| 43 | +import com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceSet |
| 44 | +import com.intellij.psi.search.searches.ClassInheritorsSearch |
| 45 | +import com.intellij.util.ArrayUtilRt |
| 46 | +import com.intellij.util.ProcessingContext |
| 47 | +import org.jetbrains.yaml.psi.YAMLKeyValue |
| 48 | +import org.jetbrains.yaml.psi.YAMLScalar |
| 49 | + |
| 50 | +private fun <P : ObjectPattern<out PsiElement, P>> P.inSpigotOrPaperPluginYml(): P = with( |
| 51 | + object : PatternCondition<PsiElement>("") { |
| 52 | + override fun accepts(t: PsiElement, context: ProcessingContext): Boolean { |
| 53 | + val module = t.findModule() ?: return false |
| 54 | + val instance = MinecraftFacet.getInstance(module, SpigotModuleType, PaperModuleType) ?: return false |
| 55 | + return instance.pluginYml == t.containingFile.originalFile.virtualFile |
| 56 | + } |
| 57 | + } |
| 58 | +) |
| 59 | + |
| 60 | +private fun <P : ObjectPattern<out PsiElement, P>> P.inBungeePluginYml(): P = with( |
| 61 | + object : PatternCondition<PsiElement>("") { |
| 62 | + override fun accepts(t: PsiElement, context: ProcessingContext): Boolean { |
| 63 | + val module = t.findModule() ?: return false |
| 64 | + val instance = MinecraftFacet.getInstance(module, BungeeCordModuleType) ?: return false |
| 65 | + return instance.pluginYml == t.containingFile.originalFile.virtualFile |
| 66 | + } |
| 67 | + } |
| 68 | +) |
| 69 | + |
| 70 | +class PluginYmlReferenceContributor : PsiReferenceContributor() { |
| 71 | + |
| 72 | + override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) { |
| 73 | + registrar.registerReferenceProvider( |
| 74 | + PlatformPatterns.psiElement(YAMLScalar::class.java) |
| 75 | + .withParent(PlatformPatterns.psiElement(YAMLKeyValue::class.java).withName("main")) |
| 76 | + .inSpigotOrPaperPluginYml(), |
| 77 | + PluginYmlClassReferenceProvider(BukkitConstants.PLUGIN) |
| 78 | + ) |
| 79 | + registrar.registerReferenceProvider( |
| 80 | + PlatformPatterns.psiElement(YAMLScalar::class.java) |
| 81 | + .withParent(PlatformPatterns.psiElement(YAMLKeyValue::class.java).withName("main")) |
| 82 | + .inBungeePluginYml(), |
| 83 | + PluginYmlClassReferenceProvider(BungeeCordConstants.PLUGIN) |
| 84 | + ) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +class PluginYmlClassReferenceProvider(val superClass: String) : JavaClassReferenceProvider() { |
| 89 | + |
| 90 | + init { |
| 91 | + setOption(ALLOW_DOLLAR_NAMES, true) |
| 92 | + setOption(JVM_FORMAT, true) |
| 93 | + setOption(CONCRETE, true) |
| 94 | + setOption(INSTANTIATABLE, true) |
| 95 | + setOption(SUPER_CLASSES, listOf(superClass)) |
| 96 | + setOption(ADVANCED_RESOLVE, true) |
| 97 | + } |
| 98 | + |
| 99 | + override fun getReferencesByString( |
| 100 | + str: String, |
| 101 | + position: PsiElement, |
| 102 | + offsetInPosition: Int |
| 103 | + ): Array<out PsiReference?> { |
| 104 | + return object : JavaClassReferenceSet(str, position, offsetInPosition, true, this) { |
| 105 | + override fun isAllowDollarInNames(): Boolean = true |
| 106 | + |
| 107 | + override fun isAllowSpaces(): Boolean = false |
| 108 | + |
| 109 | + override fun createReference( |
| 110 | + referenceIndex: Int, |
| 111 | + referenceText: String, |
| 112 | + textRange: TextRange, |
| 113 | + staticImport: Boolean |
| 114 | + ): JavaClassReference { |
| 115 | + return PluginYmlClassReference(this, referenceIndex, referenceText, textRange, staticImport, superClass) |
| 116 | + } |
| 117 | + }.allReferences |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +class PluginYmlClassReference( |
| 122 | + referenceSet: JavaClassReferenceSet, |
| 123 | + referenceIndex: Int, |
| 124 | + referenceText: String, |
| 125 | + textRange: TextRange, |
| 126 | + staticImport: Boolean, |
| 127 | + val superClass: String |
| 128 | +) : JavaClassReference(referenceSet, textRange, referenceIndex, referenceText, staticImport) { |
| 129 | + |
| 130 | + override fun getVariants(): Array<out Any?> { |
| 131 | + val pluginClass = |
| 132 | + JavaPsiFacade.getInstance(element.project).findClass(superClass, element.resolveScope) |
| 133 | + ?: return ArrayUtilRt.EMPTY_OBJECT_ARRAY |
| 134 | + val candidates = |
| 135 | + ClassInheritorsSearch.search(pluginClass, element.resolveScope, true) |
| 136 | + .filter { !it.hasModifier(JvmModifier.ABSTRACT) } |
| 137 | + .map { JavaLookupElementBuilder.forClass(it, it.qualifiedName) } |
| 138 | + .toTypedArray() |
| 139 | + return candidates |
| 140 | + } |
| 141 | +} |
0 commit comments