66package org .mapstruct .intellij .inspection ;
77
88import com .intellij .codeInsight .AnnotationUtil ;
9- import com .intellij .codeInsight .daemon .impl .quickfix .RemoveUnusedVariableFix ;
9+ import com .intellij .codeInsight .daemon .impl .quickfix .SafeDeleteFix ;
1010import com .intellij .codeInsight .intention .AddAnnotationPsiFix ;
1111import com .intellij .codeInsight .intention .QuickFixFactory ;
12- import com .intellij .codeInspection .IntentionWrapper ;
1312import com .intellij .codeInspection .LocalQuickFix ;
1413import com .intellij .codeInspection .ProblemsHolder ;
1514import com .intellij .psi .CommonClassNames ;
3231import org .mapstruct .intellij .MapStructBundle ;
3332import org .mapstruct .intellij .util .MapstructUtil ;
3433
34+ import java .util .ArrayList ;
35+ import java .util .List ;
36+
3537/**
3638 * Inspection that checks that Mappers factory is correctly used
3739 *
@@ -82,15 +84,20 @@ public void visitMethodCallExpression(PsiMethodCallExpression expression) {
8284 PsiClass mapperClass = (PsiClass ) mapperElement ;
8385 PsiAnnotation mapperAnnotation = mapperClass .getAnnotation ( MapstructUtil .MAPPER_ANNOTATION_FQN );
8486 if ( mapperAnnotation == null ) {
85- problemsHolder .registerProblem (
86- expression ,
87- MapStructBundle .message ( "inspection.wrong.usage.mappers.factory.non.mapstruct" ),
88- new AddAnnotationPsiFix (
87+ List <LocalQuickFix > fixes = new ArrayList <>(2 );
88+ fixes .add ( new AddAnnotationPsiFix (
8989 MapstructUtil .MAPPER_ANNOTATION_FQN ,
9090 mapperClass ,
9191 PsiNameValuePair .EMPTY_ARRAY
92- ),
93- createRemoveMappersFix ( expression )
92+ ) );
93+ LocalQuickFix removeMappersFix = createRemoveMappersFix ( expression );
94+ if ( removeMappersFix != null ) {
95+ fixes .add ( removeMappersFix );
96+ }
97+ problemsHolder .registerProblem (
98+ expression ,
99+ MapStructBundle .message ( "inspection.wrong.usage.mappers.factory.non.mapstruct" ),
100+ fixes .toArray ( LocalQuickFix []::new )
94101 );
95102 }
96103 else {
@@ -105,19 +112,24 @@ public void visitMethodCallExpression(PsiMethodCallExpression expression) {
105112 null :
106113 AnnotationUtil .getStringAttributeValue ( memberValue );
107114 if ( componentModel != null && !componentModel .equals ( "default" ) ) {
115+ List <LocalQuickFix > fixes = new ArrayList <>(2 );
116+ fixes .add ( createRemoveComponentModelFix ( componentModelAttribute , mapperClass ) );
117+ LocalQuickFix removeMappersFix = createRemoveMappersFix ( expression );
118+ if ( removeMappersFix != null ) {
119+ fixes .add ( removeMappersFix );
120+ }
108121 problemsHolder .registerProblem (
109122 expression ,
110123 MapStructBundle .message ( "inspection.wrong.usage.mappers.factory.non.default" ),
111- createRemoveComponentModelFix ( componentModelAttribute , mapperClass ),
112- createRemoveMappersFix ( expression )
124+ fixes .toArray ( LocalQuickFix []::new )
113125 );
114126 }
115127 }
116128 }
117129 }
118130 }
119131
120- private static class RemoveMappersFix extends RemoveUnusedVariableFix {
132+ private static class RemoveMappersFix extends SafeDeleteFix {
121133
122134 private final String myText ;
123135 private final String myFamilyName ;
@@ -145,10 +157,8 @@ public String getFamilyName() {
145157 private static LocalQuickFix createRemoveMappersFix (@ NotNull PsiMethodCallExpression methodCallExpression ) {
146158 PsiElement parent = methodCallExpression .getParent ();
147159 if ( parent instanceof PsiVariable ) {
148- return IntentionWrapper .wrapToQuickFix (
149- new RemoveMappersFix ( (PsiVariable ) parent ),
150- methodCallExpression .getContainingFile ()
151- );
160+
161+ return new RemoveMappersFix ( (PsiVariable ) parent );
152162 }
153163
154164 return null ;
0 commit comments