diff options
author | CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> | 2024-06-06 09:16:51 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-06 09:16:51 +1000 |
commit | 4ef946f824817e91bdf9c2e0ae4f201e5c669f63 (patch) | |
tree | a28c85ee43236cbb12d736935f6e43d798ff1690 /.live-plugins/module/plugin.kts | |
parent | 93229bf2caa30de689664b665b58ee59949137fc (diff) | |
download | skyhanni-4ef946f824817e91bdf9c2e0ae4f201e5c669f63.tar.gz skyhanni-4ef946f824817e91bdf9c2e0ae4f201e5c669f63.tar.bz2 skyhanni-4ef946f824817e91bdf9c2e0ae4f201e5c669f63.zip |
Backend: Convert existing objects to use the annotation and add the plugin (#1974)
Co-authored-by: ThatGravyBoat <thatgravyboat@gmail.com>
Diffstat (limited to '.live-plugins/module/plugin.kts')
-rw-r--r-- | .live-plugins/module/plugin.kts | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/.live-plugins/module/plugin.kts b/.live-plugins/module/plugin.kts new file mode 100644 index 000000000..d20021217 --- /dev/null +++ b/.live-plugins/module/plugin.kts @@ -0,0 +1,100 @@ +import com.intellij.codeInspection.LocalQuickFix +import com.intellij.codeInspection.ProblemDescriptor +import com.intellij.codeInspection.ProblemHighlightType +import com.intellij.codeInspection.ProblemsHolder +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiElementVisitor +import liveplugin.registerInspection +import liveplugin.show +import org.jetbrains.kotlin.idea.base.utils.fqname.fqName +import org.jetbrains.kotlin.idea.codeinsight.api.classic.inspections.AbstractKotlinInspection +import org.jetbrains.kotlin.idea.util.AnnotationModificationHelper +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.nj2k.postProcessing.type +import org.jetbrains.kotlin.psi.* + +// depends-on-plugin org.jetbrains.kotlin + +val forgeEvent = "SubscribeEvent" +val handleEvent = "HandleEvent" +val skyHanniModule = "SkyHanniModule" + +val patternGroup = "at.hannibal2.skyhanni.utils.repopatterns.RepoPatternGroup" +val pattern = "java.util.regex.Pattern" + +registerInspection(ModuleInspectionKotlin()) + +fun isEvent(function: KtNamedFunction): Boolean { + return function.annotationEntries.any { + it.shortName!!.asString() == handleEvent || it.shortName!!.asString() == forgeEvent + } +} + +fun isRepoPattern(property: KtProperty): Boolean { + val type = property.type()?.fqName?.asString() ?: return false + if (type == patternGroup) return true + if (type == pattern && property.hasDelegate()) return true + return false +} + +class ModuleInspectionKotlin : AbstractKotlinInspection() { + override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { + + val visitor = object : KtVisitorVoid() { + + override fun visitClass(klass: KtClass) { + val hasAnnotation = klass.annotationEntries.any { it.shortName?.asString() == skyHanniModule } + + if (hasAnnotation) { + holder.registerProblem( + klass.nameIdentifier!!, + "@SkyHanniModule can only be applied to objects", + ProblemHighlightType.GENERIC_ERROR + ) + } + } + + override fun visitObjectDeclaration(declaration: KtObjectDeclaration) { + val hasAnnotation = declaration.annotationEntries.any { it.shortName?.asString() == skyHanniModule } + if (hasAnnotation) return + + val hasSkyHanniEvents = declaration.body!!.functions.any { function -> isEvent(function) } + val hasRepoPatterns = declaration.body!!.properties.any { property -> isRepoPattern(property) } + if (!hasSkyHanniEvents && !hasRepoPatterns) return + + holder.registerProblem( + declaration, + "Module should have a @SkyHanniModule annotation", + ModuleQuickFix() + ) + } + } + + return visitor + } + + override fun getDisplayName() = "Modules should have a @SkyHanniModule annotation" + override fun getShortName() = "SkyHanniModuleInspection" + override fun getGroupDisplayName() = "SkyHanni" + override fun isEnabledByDefault() = true +} + +class ModuleQuickFix : LocalQuickFix { + override fun applyFix(project: Project, descriptor: ProblemDescriptor) { + val obj = descriptor.psiElement as KtObjectDeclaration + AnnotationModificationHelper.addAnnotation( + obj, + FqName("at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule"), + null, + null, + { null }, + " ", + null + ) + show("Annotation applied, make sure SkyHanniMod isn't still loading this module") + } + + override fun getName() = "Annotate with @SkyHanniModule" + + override fun getFamilyName() = name +} |