mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-27 00:30:17 +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"
|
groupKey="group.names.plugin.service.issues" enabledByDefault="true" level="WARNING"
|
||||||
implementationClass="net.mamoe.mirai.console.intellij.diagnostics.PluginMainServiceNotConfiguredInspection"/>
|
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"
|
<localInspection groupPath="Mirai console" language="kotlin" shortName="UsingStringPlusMessage"
|
||||||
bundle="messages.InspectionGadgetsBundle"
|
bundle="messages.InspectionGadgetsBundle"
|
||||||
key="using.string.plus.message.display.name" groupBundle="messages.InspectionsBundle"
|
key="using.string.plus.message.display.name" groupBundle="messages.InspectionsBundle"
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
plugins {
|
plugins {
|
||||||
kotlin("jvm") version "1.4.20"
|
kotlin("jvm") version "1.4.20"
|
||||||
kotlin("plugin.serialization") 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"
|
group = "org.example"
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
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
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
@ -12,7 +12,10 @@ package net.mamoe.mirai.console.intellij.diagnostics
|
|||||||
import com.intellij.codeInspection.ProblemHighlightType
|
import com.intellij.codeInspection.ProblemHighlightType
|
||||||
import com.intellij.codeInspection.ProblemsHolder
|
import com.intellij.codeInspection.ProblemsHolder
|
||||||
import com.intellij.openapi.progress.impl.CancellationCheck.Companion.runWithCancellationCheck
|
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.PsiElementVisitor
|
||||||
|
import com.intellij.psi.PsiNameIdentifierOwner
|
||||||
import net.mamoe.mirai.console.compiler.common.resolve.AUTO_SERVICE
|
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.compiler.common.resolve.PLUGIN_FQ_NAME
|
||||||
import net.mamoe.mirai.console.intellij.diagnostics.fix.ConfigurePluginMainServiceFix
|
import net.mamoe.mirai.console.intellij.diagnostics.fix.ConfigurePluginMainServiceFix
|
||||||
@ -42,7 +45,7 @@ class PluginMainServiceNotConfiguredInspection : AbstractKotlinInspection() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
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 !is KtObjectDeclaration) return@visitor
|
||||||
if (element.allSuperNames.none { it == PLUGIN_FQ_NAME }) return@visitor
|
if (element.allSuperNames.none { it == PLUGIN_FQ_NAME }) return@visitor
|
||||||
val fqName = element.fqName?.asString() ?: return@visitor
|
val fqName = element.fqName?.asString() ?: return@visitor
|
||||||
@ -51,26 +54,53 @@ class PluginMainServiceNotConfiguredInspection : AbstractKotlinInspection() {
|
|||||||
|| isServiceConfiguredWithResource(element, fqName)
|
|| isServiceConfiguredWithResource(element, fqName)
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
holder.registerProblem(
|
registerProblemImpl(holder, element, fqName)
|
||||||
element.nameIdentifier ?: element.identifyingElement ?: element,
|
}
|
||||||
"插件主类服务未配置",
|
}
|
||||||
ProblemHighlightType.WARNING,
|
|
||||||
ConfigurePluginMainServiceFix(element)
|
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(
|
private fun isServiceConfiguredWithAutoService(
|
||||||
ktClass: KtClassOrObject,
|
ktClass: KtClassOrObject,
|
||||||
): Boolean = ktClass.hasAnnotation(AUTO_SERVICE)
|
): Boolean {
|
||||||
|
return ktClass.hasAnnotation(AUTO_SERVICE)
|
||||||
|
}
|
||||||
|
|
||||||
private fun isServiceConfiguredWithResource(
|
private fun isServiceConfiguredWithResource(
|
||||||
ktClass: KtClassOrObject,
|
psiOrKtClass: PsiElement,
|
||||||
fqName: String,
|
fqName: String,
|
||||||
): Boolean {
|
): Boolean {
|
||||||
return runWithCancellationCheck {
|
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 ->
|
val services = sourceRoots.asSequence().flatMap { file ->
|
||||||
SERVICE_FILE_NAMES.asSequence().mapNotNull { serviceFileName ->
|
SERVICE_FILE_NAMES.asSequence().mapNotNull { serviceFileName ->
|
||||||
file.findFileByRelativePath("META-INF/services/$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.editor.Editor
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.project.rootManager
|
import com.intellij.openapi.project.rootManager
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
import com.intellij.testFramework.writeChild
|
import com.intellij.testFramework.writeChild
|
||||||
import org.jetbrains.kotlin.idea.inspections.KotlinUniversalQuickFix
|
import org.jetbrains.kotlin.idea.inspections.KotlinUniversalQuickFix
|
||||||
@ -23,14 +24,15 @@ import org.jetbrains.kotlin.psi.KtClassOrObject
|
|||||||
|
|
||||||
|
|
||||||
class ConfigurePluginMainServiceFix(
|
class ConfigurePluginMainServiceFix(
|
||||||
element: KtClassOrObject,
|
element: PsiElement,
|
||||||
) : KotlinCrossLanguageQuickFixAction<KtClassOrObject>(element), KotlinUniversalQuickFix, LocalQuickFix {
|
private val fqName: String,
|
||||||
|
) : KotlinCrossLanguageQuickFixAction<PsiElement>(element), KotlinUniversalQuickFix, LocalQuickFix {
|
||||||
|
|
||||||
override fun getFamilyName(): String = "Mirai Console"
|
override fun getFamilyName(): String = "Mirai Console"
|
||||||
override fun getText(): String = "配置插件主类服务"
|
override fun getText(): String = "配置插件主类服务"
|
||||||
|
|
||||||
override fun invokeImpl(project: Project, editor: Editor?, file: PsiFile) {
|
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 sourceRoots = file.module?.rootManager?.sourceRoots ?: return
|
||||||
|
|
||||||
val sourceRoot = sourceRoots.find { it.name.endsWith("resources") }
|
val sourceRoot = sourceRoots.find { it.name.endsWith("resources") }
|
||||||
@ -38,7 +40,7 @@ class ConfigurePluginMainServiceFix(
|
|||||||
?: sourceRoots.last()
|
?: sourceRoots.last()
|
||||||
|
|
||||||
project.executeWriteCommand(name) {
|
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