Add QuickFix AddSerializerFix for UNSERIALIZABLE_TYPE

This commit is contained in:
Him188 2020-09-19 01:06:45 +08:00
parent 09e509849d
commit f76226f13c
7 changed files with 80 additions and 5 deletions

View File

@ -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<PsiElement, String> ILLEGAL_PLUGIN_DESCRIPTION = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> NOT_CONSTRUCTABLE_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> UNSERIALIZABLE_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, ClassDescriptor> UNSERIALIZABLE_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory2<PsiElement, String, String> ILLEGAL_COMMAND_NAME = DiagnosticFactory2.create(ERROR);
DiagnosticFactory2<PsiElement, String, String> ILLEGAL_PERMISSION_NAME = DiagnosticFactory2.create(ERROR);
DiagnosticFactory2<PsiElement, String, String> ILLEGAL_PERMISSION_ID = DiagnosticFactory2.create(ERROR);

View File

@ -31,7 +31,7 @@ object MiraiConsoleErrorsRendering : DefaultErrorMessages.Extension {
put(
UNSERIALIZABLE_TYPE,
"类型 ''{0}'' 无法被自动序列化, 需要添加序列化器",
Renderers.STRING,
Renderers.FQ_NAMES_IN_TYPES,
)
put(

View File

@ -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
///////////////////////////////////////////////////////////////////////////

View File

@ -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)
}
}

View File

@ -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
))
}
}
}

View File

@ -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<KtModifierListOwner>(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<DiagnosticWithParameters1<*, *>>()?.a?.castOrNull<ClassDescriptor>() ?: return null
val ktClassOrObject = classDescriptor.findPsi()?.castOrNull<KtClassOrObject>() ?: return null
return AddSerializerFix(ktClassOrObject)
}
override fun isApplicableForCodeFragment(): Boolean = false
}
}

View File

@ -26,7 +26,7 @@
<extensions defaultExtensionNs="org.jetbrains.kotlin">
<storageComponentContainerContributor
implementation="net.mamoe.mirai.console.intellij.IDEContainerContributor"/>
<!-- <quickFixContributor implementation="org.jetbrains.kotlinx.serialization.idea.quickfixes.SerializationQuickFixContributor"/> -->
<quickFixContributor implementation="net.mamoe.mirai.console.intellij.QuickFixRegistrar"/>
</extensions>
<idea-version since-build="193.*" until-build="203.*"/>