From d5827f30881403eb75ba736b7c89cdbd493d49a2 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 16 Jan 2021 13:13:45 +0800 Subject: [PATCH] Support PluginMainServiceNotConfiguredInspection for Java --- .../resources/META-INF/plugin.xml | 6 +++ .../projects/test-project/build.gradle.kts | 3 +- .../gradle/wrapper/gradle-wrapper.properties | 2 +- ...luginMainServiceNotConfiguredInspection.kt | 50 +++++++++++++++---- .../fix/ConfigurePluginMainServiceFix.kt | 10 ++-- 5 files changed, 55 insertions(+), 16 deletions(-) diff --git a/tools/intellij-plugin/resources/META-INF/plugin.xml b/tools/intellij-plugin/resources/META-INF/plugin.xml index 05f8eb827..2c86f20a1 100644 --- a/tools/intellij-plugin/resources/META-INF/plugin.xml +++ b/tools/intellij-plugin/resources/META-INF/plugin.xml @@ -38,6 +38,12 @@ groupKey="group.names.plugin.service.issues" enabledByDefault="true" level="WARNING" implementationClass="net.mamoe.mirai.console.intellij.diagnostics.PluginMainServiceNotConfiguredInspection"/> + + + val ktVisitor = classOrObjectVisitor visitor@{ element -> if (element !is KtObjectDeclaration) return@visitor if (element.allSuperNames.none { it == PLUGIN_FQ_NAME }) return@visitor val fqName = element.fqName?.asString() ?: return@visitor @@ -51,26 +54,53 @@ class PluginMainServiceNotConfiguredInspection : AbstractKotlinInspection() { || isServiceConfiguredWithResource(element, fqName) if (!found) { - holder.registerProblem( - element.nameIdentifier ?: element.identifyingElement ?: element, - "插件主类服务未配置", - ProblemHighlightType.WARNING, - ConfigurePluginMainServiceFix(element) - ) + registerProblemImpl(holder, element, fqName) + } + } + + val javaVisitor = object : PsiElementVisitor() { + override fun visitElement(element: PsiElement) { + if (element !is PsiClass) return + if (element.allSuperNames.none { it == PLUGIN_FQ_NAME }) return + + if (element.hasAnnotation(AUTO_SERVICE.asString())) return + val fqName = element.qualifiedName ?: return + if (isServiceConfiguredWithResource(element, fqName)) return + + registerProblemImpl(holder, element, fqName) + } + } + + return object : PsiElementVisitor() { + override fun visitElement(element: PsiElement) { + super.visitElement(element) + if (element is KtClassOrObject) ktVisitor.visitClassOrObject(element) + else javaVisitor.visitElement(element) } } } + private fun registerProblemImpl(holder: ProblemsHolder, element: PsiNameIdentifierOwner, fqName: String) { + holder.registerProblem( + element.nameIdentifier ?: element.identifyingElement ?: element, + @Suppress("DialogTitleCapitalization") "插件主类服务未配置", + ProblemHighlightType.WARNING, + ConfigurePluginMainServiceFix(element, fqName) + ) + } + private fun isServiceConfiguredWithAutoService( ktClass: KtClassOrObject, - ): Boolean = ktClass.hasAnnotation(AUTO_SERVICE) + ): Boolean { + return ktClass.hasAnnotation(AUTO_SERVICE) + } private fun isServiceConfiguredWithResource( - ktClass: KtClassOrObject, + psiOrKtClass: PsiElement, fqName: String, ): Boolean { return runWithCancellationCheck { - val sourceRoots = ktClass.module?.rootManager?.sourceRoots ?: return@runWithCancellationCheck false + val sourceRoots = psiOrKtClass.module?.rootManager?.sourceRoots ?: return@runWithCancellationCheck false val services = sourceRoots.asSequence().flatMap { file -> SERVICE_FILE_NAMES.asSequence().mapNotNull { serviceFileName -> file.findFileByRelativePath("META-INF/services/$serviceFileName") diff --git a/tools/intellij-plugin/src/diagnostics/fix/ConfigurePluginMainServiceFix.kt b/tools/intellij-plugin/src/diagnostics/fix/ConfigurePluginMainServiceFix.kt index bf5535845..df8d910fc 100644 --- a/tools/intellij-plugin/src/diagnostics/fix/ConfigurePluginMainServiceFix.kt +++ b/tools/intellij-plugin/src/diagnostics/fix/ConfigurePluginMainServiceFix.kt @@ -13,6 +13,7 @@ import com.intellij.codeInspection.LocalQuickFix import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import com.intellij.openapi.project.rootManager +import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.testFramework.writeChild import org.jetbrains.kotlin.idea.inspections.KotlinUniversalQuickFix @@ -23,14 +24,15 @@ import org.jetbrains.kotlin.psi.KtClassOrObject class ConfigurePluginMainServiceFix( - element: KtClassOrObject, -) : KotlinCrossLanguageQuickFixAction(element), KotlinUniversalQuickFix, LocalQuickFix { + element: PsiElement, + private val fqName: String, +) : KotlinCrossLanguageQuickFixAction(element), KotlinUniversalQuickFix, LocalQuickFix { override fun getFamilyName(): String = "Mirai Console" override fun getText(): String = "配置插件主类服务" override fun invokeImpl(project: Project, editor: Editor?, file: PsiFile) { - val elementFqName = element?.fqName ?: return + val elementFqName = fqName val sourceRoots = file.module?.rootManager?.sourceRoots ?: return val sourceRoot = sourceRoots.find { it.name.endsWith("resources") } @@ -38,7 +40,7 @@ class ConfigurePluginMainServiceFix( ?: sourceRoots.last() project.executeWriteCommand(name) { - sourceRoot.writeChild("META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin", elementFqName.asString().toByteArray()) + sourceRoot.writeChild("META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin", elementFqName.toByteArray()) } } }