From 8e25048884d6fe2b667237f725adbc7006dbd176 Mon Sep 17 00:00:00 2001 From: hduelme <46139144+hduelme@users.noreply.github.com> Date: Sat, 16 Mar 2024 12:44:24 +0100 Subject: [PATCH] #155 Refactor RemoveMappersFix to use SafeDeleteFix --- .../WrongUsageOfMappersFactoryInspection.java | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/mapstruct/intellij/inspection/WrongUsageOfMappersFactoryInspection.java b/src/main/java/org/mapstruct/intellij/inspection/WrongUsageOfMappersFactoryInspection.java index 7fa60124..b53d438d 100644 --- a/src/main/java/org/mapstruct/intellij/inspection/WrongUsageOfMappersFactoryInspection.java +++ b/src/main/java/org/mapstruct/intellij/inspection/WrongUsageOfMappersFactoryInspection.java @@ -6,10 +6,9 @@ package org.mapstruct.intellij.inspection; import com.intellij.codeInsight.AnnotationUtil; -import com.intellij.codeInsight.daemon.impl.quickfix.RemoveUnusedVariableFix; +import com.intellij.codeInsight.daemon.impl.quickfix.SafeDeleteFix; import com.intellij.codeInsight.intention.AddAnnotationPsiFix; import com.intellij.codeInsight.intention.QuickFixFactory; -import com.intellij.codeInspection.IntentionWrapper; import com.intellij.codeInspection.LocalQuickFix; import com.intellij.codeInspection.ProblemsHolder; import com.intellij.psi.CommonClassNames; @@ -32,6 +31,9 @@ import org.mapstruct.intellij.MapStructBundle; import org.mapstruct.intellij.util.MapstructUtil; +import java.util.ArrayList; +import java.util.List; + /** * Inspection that checks that Mappers factory is correctly used * @@ -82,15 +84,20 @@ public void visitMethodCallExpression(PsiMethodCallExpression expression) { PsiClass mapperClass = (PsiClass) mapperElement; PsiAnnotation mapperAnnotation = mapperClass.getAnnotation( MapstructUtil.MAPPER_ANNOTATION_FQN ); if ( mapperAnnotation == null ) { - problemsHolder.registerProblem( - expression, - MapStructBundle.message( "inspection.wrong.usage.mappers.factory.non.mapstruct" ), - new AddAnnotationPsiFix( + List fixes = new ArrayList<>(2); + fixes.add( new AddAnnotationPsiFix( MapstructUtil.MAPPER_ANNOTATION_FQN, mapperClass, PsiNameValuePair.EMPTY_ARRAY - ), - createRemoveMappersFix( expression ) + ) ); + LocalQuickFix removeMappersFix = createRemoveMappersFix( expression ); + if ( removeMappersFix != null ) { + fixes.add( removeMappersFix ); + } + problemsHolder.registerProblem( + expression, + MapStructBundle.message( "inspection.wrong.usage.mappers.factory.non.mapstruct" ), + fixes.toArray( LocalQuickFix[]::new ) ); } else { @@ -105,11 +112,16 @@ public void visitMethodCallExpression(PsiMethodCallExpression expression) { null : AnnotationUtil.getStringAttributeValue( memberValue ); if ( componentModel != null && !componentModel.equals( "default" ) ) { + List fixes = new ArrayList<>(2); + fixes.add( createRemoveComponentModelFix( componentModelAttribute, mapperClass ) ); + LocalQuickFix removeMappersFix = createRemoveMappersFix( expression ); + if ( removeMappersFix != null ) { + fixes.add( removeMappersFix ); + } problemsHolder.registerProblem( expression, MapStructBundle.message( "inspection.wrong.usage.mappers.factory.non.default" ), - createRemoveComponentModelFix( componentModelAttribute, mapperClass ), - createRemoveMappersFix( expression ) + fixes.toArray( LocalQuickFix[]::new ) ); } } @@ -117,7 +129,7 @@ public void visitMethodCallExpression(PsiMethodCallExpression expression) { } } - private static class RemoveMappersFix extends RemoveUnusedVariableFix { + private static class RemoveMappersFix extends SafeDeleteFix { private final String myText; private final String myFamilyName; @@ -145,10 +157,8 @@ public String getFamilyName() { private static LocalQuickFix createRemoveMappersFix(@NotNull PsiMethodCallExpression methodCallExpression) { PsiElement parent = methodCallExpression.getParent(); if ( parent instanceof PsiVariable ) { - return IntentionWrapper.wrapToQuickFix( - new RemoveMappersFix( (PsiVariable) parent ), - methodCallExpression.getContainingFile() - ); + + return new RemoveMappersFix( (PsiVariable) parent ); } return null;