diff --git a/tools/compiler-common/src/main/java/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrors.java b/tools/compiler-common/src/main/java/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrors.java index 325bd8263..e4e2e21da 100644 --- a/tools/compiler-common/src/main/java/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrors.java +++ b/tools/compiler-common/src/main/java/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrors.java @@ -10,6 +10,7 @@ package net.mamoe.mirai.console.compiler.common.diagnostics; import com.intellij.psi.PsiElement; +import org.jetbrains.kotlin.descriptors.ClassDescriptor; import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1; import org.jetbrains.kotlin.diagnostics.DiagnosticFactory2; import org.jetbrains.kotlin.diagnostics.Errors; @@ -19,7 +20,7 @@ import static org.jetbrains.kotlin.diagnostics.Severity.ERROR; public interface MiraiConsoleErrors { DiagnosticFactory1 ILLEGAL_PLUGIN_DESCRIPTION = DiagnosticFactory1.create(ERROR); DiagnosticFactory1 NOT_CONSTRUCTABLE_TYPE = DiagnosticFactory1.create(ERROR); - DiagnosticFactory1 UNSERIALIZABLE_TYPE = DiagnosticFactory1.create(ERROR); + DiagnosticFactory1 UNSERIALIZABLE_TYPE = DiagnosticFactory1.create(ERROR); DiagnosticFactory2 ILLEGAL_COMMAND_NAME = DiagnosticFactory2.create(ERROR); DiagnosticFactory2 ILLEGAL_PERMISSION_NAME = DiagnosticFactory2.create(ERROR); DiagnosticFactory2 ILLEGAL_PERMISSION_ID = DiagnosticFactory2.create(ERROR); diff --git a/tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrorsRendering.kt b/tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrorsRendering.kt index 6140aed62..e3e7b7b6a 100644 --- a/tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrorsRendering.kt +++ b/tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/diagnostics/MiraiConsoleErrorsRendering.kt @@ -31,7 +31,7 @@ object MiraiConsoleErrorsRendering : DefaultErrorMessages.Extension { put( UNSERIALIZABLE_TYPE, "类型 ''{0}'' 无法被自动序列化, 需要添加序列化器", - Renderers.STRING, + Renderers.FQ_NAMES_IN_TYPES, ) put( diff --git a/tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/resolve/MiraiConsoleTypes.kt b/tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/resolve/resolveTypes.kt similarity index 92% rename from tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/resolve/MiraiConsoleTypes.kt rename to tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/resolve/resolveTypes.kt index 478656c70..463321b14 100644 --- a/tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/resolve/MiraiConsoleTypes.kt +++ b/tools/compiler-common/src/main/kotlin/net/mamoe/mirai/console/compiler/common/resolve/resolveTypes.kt @@ -15,6 +15,13 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotated import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.resolve.constants.EnumValue +/////////////////////////////////////////////////////////////////////////// +// Serializer +/////////////////////////////////////////////////////////////////////////// + +val SERIALIZABLE_FQ_NAME = FqName("kotlinx.serialization.Serializable") + + /////////////////////////////////////////////////////////////////////////// // Command /////////////////////////////////////////////////////////////////////////// diff --git a/tools/intellij-plugin/src/main/kotlin/net/mamoe/mirai/console/intellij/QuickFixRegistrar.kt b/tools/intellij-plugin/src/main/kotlin/net/mamoe/mirai/console/intellij/QuickFixRegistrar.kt new file mode 100644 index 000000000..6e338736c --- /dev/null +++ b/tools/intellij-plugin/src/main/kotlin/net/mamoe/mirai/console/intellij/QuickFixRegistrar.kt @@ -0,0 +1,23 @@ +package net.mamoe.mirai.console.intellij + +import com.intellij.codeInsight.intention.IntentionAction +import net.mamoe.mirai.console.compiler.common.diagnostics.MiraiConsoleErrors +import net.mamoe.mirai.console.intellij.diagnostics.fix.AddSerializerFix +import org.jetbrains.kotlin.diagnostics.DiagnosticFactory +import org.jetbrains.kotlin.idea.quickfix.KotlinIntentionActionsFactory +import org.jetbrains.kotlin.idea.quickfix.QuickFixContributor +import org.jetbrains.kotlin.idea.quickfix.QuickFixes + +class QuickFixRegistrar : QuickFixContributor { + override fun registerQuickFixes(quickFixes: QuickFixes) { + fun DiagnosticFactory<*>.registerFactory(vararg factory: KotlinIntentionActionsFactory) { + quickFixes.register(this, *factory) + } + + fun DiagnosticFactory<*>.registerActions(vararg action: IntentionAction) { + quickFixes.register(this, *action) + } + + MiraiConsoleErrors.UNSERIALIZABLE_TYPE.registerFactory(AddSerializerFix) + } +} diff --git a/tools/intellij-plugin/src/main/kotlin/net/mamoe/mirai/console/intellij/diagnostics/PluginDataValuesChecker.kt b/tools/intellij-plugin/src/main/kotlin/net/mamoe/mirai/console/intellij/diagnostics/PluginDataValuesChecker.kt index 16e6e2cf8..4130b6ce3 100644 --- a/tools/intellij-plugin/src/main/kotlin/net/mamoe/mirai/console/intellij/diagnostics/PluginDataValuesChecker.kt +++ b/tools/intellij-plugin/src/main/kotlin/net/mamoe/mirai/console/intellij/diagnostics/PluginDataValuesChecker.kt @@ -56,8 +56,8 @@ class PluginDataValuesChecker : DeclarationChecker { if (!classDescriptor.hasAnnotation(SERIALIZABLE_FQ_NAME)) // TODO: 2020/9/18 external serializers return@forEach context.report(MiraiConsoleErrors.UNSERIALIZABLE_TYPE.on( inspectionTarget, - type.fqName?.asString().toString()) - ) + classDescriptor + )) } } } \ No newline at end of file diff --git a/tools/intellij-plugin/src/main/kotlin/net/mamoe/mirai/console/intellij/diagnostics/fix/AddSerializerFix.kt b/tools/intellij-plugin/src/main/kotlin/net/mamoe/mirai/console/intellij/diagnostics/fix/AddSerializerFix.kt new file mode 100644 index 000000000..c276b3c8c --- /dev/null +++ b/tools/intellij-plugin/src/main/kotlin/net/mamoe/mirai/console/intellij/diagnostics/fix/AddSerializerFix.kt @@ -0,0 +1,44 @@ +package net.mamoe.mirai.console.intellij.diagnostics.fix + +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiFile +import net.mamoe.mirai.console.compiler.common.SERIALIZABLE_FQ_NAME +import net.mamoe.mirai.console.compiler.common.castOrNull +import net.mamoe.mirai.console.compiler.common.diagnostics.MiraiConsoleErrors +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.diagnostics.DiagnosticWithParameters1 +import org.jetbrains.kotlin.idea.inspections.KotlinUniversalQuickFix +import org.jetbrains.kotlin.idea.quickfix.KotlinCrossLanguageQuickFixAction +import org.jetbrains.kotlin.idea.quickfix.KotlinSingleIntentionActionFactory +import org.jetbrains.kotlin.idea.util.addAnnotation +import org.jetbrains.kotlin.js.resolve.diagnostics.findPsi +import org.jetbrains.kotlin.psi.KtClassOrObject +import org.jetbrains.kotlin.psi.KtModifierListOwner + +/** + * @see MiraiConsoleErrors.UNSERIALIZABLE_TYPE + */ +class AddSerializerFix( + element: KtClassOrObject, +) : KotlinCrossLanguageQuickFixAction(element), KotlinUniversalQuickFix { + + override fun getFamilyName(): String = "添加注解" + override fun getText(): String = "添加 @Serializable" + + override fun invokeImpl(project: Project, editor: Editor?, file: PsiFile) { + element?.addAnnotation(SERIALIZABLE_FQ_NAME) ?: return + } + + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): IntentionAction? { + val classDescriptor = diagnostic.castOrNull>()?.a?.castOrNull() ?: return null + val ktClassOrObject = classDescriptor.findPsi()?.castOrNull() ?: return null + return AddSerializerFix(ktClassOrObject) + } + + override fun isApplicableForCodeFragment(): Boolean = false + } +} diff --git a/tools/intellij-plugin/src/main/resources/META-INF/plugin.xml b/tools/intellij-plugin/src/main/resources/META-INF/plugin.xml index c7d524f96..19db4ac87 100644 --- a/tools/intellij-plugin/src/main/resources/META-INF/plugin.xml +++ b/tools/intellij-plugin/src/main/resources/META-INF/plugin.xml @@ -26,7 +26,7 @@ - +