-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial insight for Bukkit-like plugin.yml
- Loading branch information
Showing
24 changed files
with
1,186 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Minecraft Development for IntelliJ | ||
* | ||
* https://mcdev.io/ | ||
* | ||
* Copyright (C) 2024 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.demonwav.mcdev.yaml | ||
|
||
import com.demonwav.mcdev.facet.MinecraftFacet | ||
import com.demonwav.mcdev.platform.bukkit.PaperModuleType | ||
import com.demonwav.mcdev.platform.bukkit.SpigotModuleType | ||
import com.demonwav.mcdev.platform.bukkit.util.BukkitConstants | ||
import com.demonwav.mcdev.platform.bungeecord.BungeeCordModuleType | ||
import com.demonwav.mcdev.platform.bungeecord.util.BungeeCordConstants | ||
import com.demonwav.mcdev.util.findModule | ||
import com.intellij.codeInspection.LocalInspectionTool | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.psi.JavaPsiFacade | ||
import com.intellij.psi.PsiClass | ||
import com.intellij.psi.PsiElementVisitor | ||
import org.jetbrains.annotations.Nls | ||
import org.jetbrains.yaml.psi.YAMLKeyValue | ||
import org.jetbrains.yaml.psi.YAMLScalar | ||
import org.jetbrains.yaml.psi.YamlPsiElementVisitor | ||
|
||
class PluginYmlInspection : LocalInspectionTool() { | ||
|
||
override fun getStaticDescription(): @Nls String? = "Reports issues in Bukkit-like plugin.yml files" | ||
|
||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { | ||
val module = holder.file.findModule() ?: return PsiElementVisitor.EMPTY_VISITOR | ||
val virtualFile = holder.file.virtualFile ?: return PsiElementVisitor.EMPTY_VISITOR | ||
val pluginClassFqn = when { | ||
MinecraftFacet.getInstance(module, SpigotModuleType, PaperModuleType)?.pluginYml == virtualFile -> | ||
BukkitConstants.PLUGIN | ||
MinecraftFacet.getInstance(module, BungeeCordModuleType)?.pluginYml == virtualFile -> | ||
BungeeCordConstants.PLUGIN | ||
else -> return PsiElementVisitor.EMPTY_VISITOR | ||
} | ||
|
||
return Visitor(holder, pluginClassFqn) | ||
} | ||
|
||
private class Visitor(val holder: ProblemsHolder, val pluginClassFqn: String) : YamlPsiElementVisitor() { | ||
|
||
override fun visitScalar(scalar: YAMLScalar) { | ||
super.visitScalar(scalar) | ||
|
||
if ((scalar.parent as? YAMLKeyValue)?.keyText != "main") { | ||
return | ||
} | ||
|
||
val resolved = scalar.references.firstNotNullOfOrNull { it.resolve() as? PsiClass } | ||
if (resolved == null) { | ||
holder.registerProblem(scalar, "Unresolved reference") | ||
return | ||
} | ||
|
||
val pluginClass = JavaPsiFacade.getInstance(holder.project).findClass(pluginClassFqn, scalar.resolveScope) | ||
?: return | ||
if (!resolved.isInheritor(pluginClass, true)) { | ||
holder.registerProblem(scalar, "Class must implement $pluginClassFqn") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Minecraft Development for IntelliJ | ||
* | ||
* https://mcdev.io/ | ||
* | ||
* Copyright (C) 2024 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.demonwav.mcdev.yaml | ||
|
||
import com.demonwav.mcdev.facet.MinecraftFacet | ||
import com.demonwav.mcdev.platform.bukkit.PaperModuleType | ||
import com.demonwav.mcdev.platform.bukkit.SpigotModuleType | ||
import com.demonwav.mcdev.platform.bukkit.util.BukkitConstants | ||
import com.demonwav.mcdev.platform.bungeecord.BungeeCordModuleType | ||
import com.demonwav.mcdev.platform.bungeecord.util.BungeeCordConstants | ||
import com.demonwav.mcdev.util.findModule | ||
import com.intellij.codeInsight.completion.JavaLookupElementBuilder | ||
import com.intellij.lang.jvm.JvmModifier | ||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.patterns.ObjectPattern | ||
import com.intellij.patterns.PatternCondition | ||
import com.intellij.patterns.PlatformPatterns | ||
import com.intellij.psi.JavaPsiFacade | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiReference | ||
import com.intellij.psi.PsiReferenceContributor | ||
import com.intellij.psi.PsiReferenceRegistrar | ||
import com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference | ||
import com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceProvider | ||
import com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceSet | ||
import com.intellij.psi.search.searches.ClassInheritorsSearch | ||
import com.intellij.util.ArrayUtilRt | ||
import com.intellij.util.ProcessingContext | ||
import org.jetbrains.yaml.psi.YAMLKeyValue | ||
import org.jetbrains.yaml.psi.YAMLScalar | ||
|
||
private fun <P : ObjectPattern<out PsiElement, P>> P.inSpigotOrPaperPluginYml(): P = with( | ||
object : PatternCondition<PsiElement>("") { | ||
override fun accepts(t: PsiElement, context: ProcessingContext): Boolean { | ||
val module = t.findModule() ?: return false | ||
val instance = MinecraftFacet.getInstance(module, SpigotModuleType, PaperModuleType) ?: return false | ||
return instance.pluginYml == t.containingFile.originalFile.virtualFile | ||
} | ||
} | ||
) | ||
|
||
private fun <P : ObjectPattern<out PsiElement, P>> P.inBungeePluginYml(): P = with( | ||
object : PatternCondition<PsiElement>("") { | ||
override fun accepts(t: PsiElement, context: ProcessingContext): Boolean { | ||
val module = t.findModule() ?: return false | ||
val instance = MinecraftFacet.getInstance(module, BungeeCordModuleType) ?: return false | ||
return instance.pluginYml == t.containingFile.originalFile.virtualFile | ||
} | ||
} | ||
) | ||
|
||
class PluginYmlReferenceContributor : PsiReferenceContributor() { | ||
|
||
override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) { | ||
registrar.registerReferenceProvider( | ||
PlatformPatterns.psiElement(YAMLScalar::class.java) | ||
.withParent(PlatformPatterns.psiElement(YAMLKeyValue::class.java).withName("main")) | ||
.inSpigotOrPaperPluginYml(), | ||
PluginYmlClassReferenceProvider(BukkitConstants.PLUGIN) | ||
) | ||
registrar.registerReferenceProvider( | ||
PlatformPatterns.psiElement(YAMLScalar::class.java) | ||
.withParent(PlatformPatterns.psiElement(YAMLKeyValue::class.java).withName("main")) | ||
.inBungeePluginYml(), | ||
PluginYmlClassReferenceProvider(BungeeCordConstants.PLUGIN) | ||
) | ||
} | ||
} | ||
|
||
class PluginYmlClassReferenceProvider(val superClass: String) : JavaClassReferenceProvider() { | ||
|
||
init { | ||
setOption(ALLOW_DOLLAR_NAMES, true) | ||
setOption(JVM_FORMAT, true) | ||
setOption(CONCRETE, true) | ||
setOption(INSTANTIATABLE, true) | ||
setOption(SUPER_CLASSES, listOf(superClass)) | ||
setOption(ADVANCED_RESOLVE, true) | ||
} | ||
|
||
override fun getReferencesByString( | ||
str: String, | ||
position: PsiElement, | ||
offsetInPosition: Int | ||
): Array<out PsiReference?> { | ||
return object : JavaClassReferenceSet(str, position, offsetInPosition, true, this) { | ||
override fun isAllowDollarInNames(): Boolean = true | ||
|
||
override fun isAllowSpaces(): Boolean = false | ||
|
||
override fun createReference( | ||
referenceIndex: Int, | ||
referenceText: String, | ||
textRange: TextRange, | ||
staticImport: Boolean | ||
): JavaClassReference { | ||
return PluginYmlClassReference(this, referenceIndex, referenceText, textRange, staticImport, superClass) | ||
} | ||
}.allReferences | ||
} | ||
} | ||
|
||
class PluginYmlClassReference( | ||
referenceSet: JavaClassReferenceSet, | ||
referenceIndex: Int, | ||
referenceText: String, | ||
textRange: TextRange, | ||
staticImport: Boolean, | ||
val superClass: String | ||
) : JavaClassReference(referenceSet, textRange, referenceIndex, referenceText, staticImport) { | ||
|
||
override fun getVariants(): Array<out Any?> { | ||
val pluginClass = | ||
JavaPsiFacade.getInstance(element.project).findClass(superClass, element.resolveScope) | ||
?: return ArrayUtilRt.EMPTY_OBJECT_ARRAY | ||
val candidates = | ||
ClassInheritorsSearch.search(pluginClass, element.resolveScope, true) | ||
.filter { !it.hasModifier(JvmModifier.ABSTRACT) } | ||
.map { JavaLookupElementBuilder.forClass(it, it.qualifiedName) } | ||
.toTypedArray() | ||
return candidates | ||
} | ||
} |
Oops, something went wrong.