mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-25 15:40:28 +08:00
Support PluginMainServiceNotConfiguredInspection for Java
This commit is contained in:
parent
2947a7c162
commit
d5827f3088
@ -38,6 +38,12 @@
|
||||
groupKey="group.names.plugin.service.issues" enabledByDefault="true" level="WARNING"
|
||||
implementationClass="net.mamoe.mirai.console.intellij.diagnostics.PluginMainServiceNotConfiguredInspection"/>
|
||||
|
||||
<localInspection groupPath="Mirai console" language="JAVA" shortName="PluginMainServiceNotConfigured"
|
||||
bundle="messages.InspectionGadgetsBundle"
|
||||
key="plugin.service.not.configured.display.name" groupBundle="messages.InspectionsBundle"
|
||||
groupKey="group.names.plugin.service.issues" enabledByDefault="true" level="WARNING"
|
||||
implementationClass="net.mamoe.mirai.console.intellij.diagnostics.PluginMainServiceNotConfiguredInspection"/>
|
||||
|
||||
<localInspection groupPath="Mirai console" language="kotlin" shortName="UsingStringPlusMessage"
|
||||
bundle="messages.InspectionGadgetsBundle"
|
||||
key="using.string.plus.message.display.name" groupBundle="messages.InspectionsBundle"
|
||||
|
@ -1,7 +1,8 @@
|
||||
plugins {
|
||||
kotlin("jvm") version "1.4.20"
|
||||
kotlin("plugin.serialization") version "1.4.20"
|
||||
id("net.mamoe.mirai-console") version "1.1.0-dev-36"
|
||||
id("net.mamoe.mirai-console") version "2.0.0"
|
||||
java
|
||||
}
|
||||
|
||||
group = "org.example"
|
||||
|
@ -1,5 +1,5 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
@ -12,7 +12,10 @@ package net.mamoe.mirai.console.intellij.diagnostics
|
||||
import com.intellij.codeInspection.ProblemHighlightType
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.openapi.progress.impl.CancellationCheck.Companion.runWithCancellationCheck
|
||||
import com.intellij.psi.PsiClass
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import com.intellij.psi.PsiNameIdentifierOwner
|
||||
import net.mamoe.mirai.console.compiler.common.resolve.AUTO_SERVICE
|
||||
import net.mamoe.mirai.console.compiler.common.resolve.PLUGIN_FQ_NAME
|
||||
import net.mamoe.mirai.console.intellij.diagnostics.fix.ConfigurePluginMainServiceFix
|
||||
@ -42,7 +45,7 @@ class PluginMainServiceNotConfiguredInspection : AbstractKotlinInspection() {
|
||||
}
|
||||
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return classOrObjectVisitor visitor@{ element ->
|
||||
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")
|
||||
|
@ -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<KtClassOrObject>(element), KotlinUniversalQuickFix, LocalQuickFix {
|
||||
element: PsiElement,
|
||||
private val fqName: String,
|
||||
) : KotlinCrossLanguageQuickFixAction<PsiElement>(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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user