From 51c5999d0beef19bff882cd489193adbfaea408b Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 28 Jun 2023 12:03:39 +0200 Subject: [PATCH] [java-inspections] DeleteElementFix: ModCommand GitOrigin-RevId: aefb109889b898b31067f369577f14433075e4e9 --- ...MeaninglessRecordAnnotationInspection.java | 4 ++-- .../RedundantExplicitCloseInspection.java | 6 +++-- .../RedundantRecordConstructorInspection.java | 5 ++--- .../impl/quickfix/DeleteElementFix.java | 22 +++++++------------ .../impl/config/QuickFixFactoryImpl.java | 4 ++-- .../javaDoc/JavadocDeclarationInspection.java | 8 +++---- .../LightAnnotatorHighlightingTest.java | 12 ++++------ .../ig/bugs/EmptyStatementBodyInspection.java | 2 +- ...ntOperationOnEmptyContainerInspection.java | 13 +++++------ .../RedundantStringOperationInspection.java | 6 ++--- 10 files changed, 36 insertions(+), 46 deletions(-) diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/MeaninglessRecordAnnotationInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/MeaninglessRecordAnnotationInspection.java index 486cb0235f30..e9df4366b05c 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/MeaninglessRecordAnnotationInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/MeaninglessRecordAnnotationInspection.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInspection; import com.intellij.codeInsight.AnnotationTargetUtil; @@ -75,7 +75,7 @@ public class MeaninglessRecordAnnotationInspection extends AbstractBaseJavaLocal message = JavaBundle.message("inspection.meaningless.record.annotation.message.parameter"); } else return; - holder.registerProblem(annotation, message, new DeleteElementFix(annotation)); + holder.problem(annotation, message).fix(new DeleteElementFix(annotation)).register(); } }; } diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/RedundantExplicitCloseInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/RedundantExplicitCloseInspection.java index 6d96e4232bec..1f9621151735 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/RedundantExplicitCloseInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/RedundantExplicitCloseInspection.java @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInspection; import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix; @@ -50,7 +50,9 @@ public class RedundantExplicitCloseInspection extends AbstractBaseJavaLocalInspe EquivalenceChecker.getCanonicalPsiEquivalence() .expressionsAreEquivalent(reference, ((PsiResourceExpression)element).getExpression())); if(!isReferenceToResourceVariable) return; - holder.registerProblem(last, JavaBundle.message("inspection.redundant.explicit.close"), new DeleteElementFix(last, CommonQuickFixBundle.message("fix.remove.redundant", "close()"))); + holder.problem(last, JavaBundle.message("inspection.redundant.explicit.close")) + .fix(new DeleteElementFix(last, CommonQuickFixBundle.message("fix.remove.redundant", "close()"))) + .register(); } } }; diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/RedundantRecordConstructorInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/RedundantRecordConstructorInspection.java index bebb8b1170c7..904d60edc0eb 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/RedundantRecordConstructorInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/RedundantRecordConstructorInspection.java @@ -75,9 +75,8 @@ public class RedundantRecordConstructorInspection extends AbstractBaseJavaLocalI int count = getAssignedComponentsCount(components, parameters, statements); if (count < statements.length) { for (int i = statements.length - count; i < statements.length; i++) { - holder.registerProblem(statements[i], - JavaBundle.message("inspection.redundant.record.constructor.statement.message"), - ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new DeleteElementFix(statements[i])); + holder.problem(statements[i], JavaBundle.message("inspection.redundant.record.constructor.statement.message")) + .fix(new DeleteElementFix(statements[i])).register(); } return; } diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteElementFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteElementFix.java index fcaaa868116c..6c57fdd07967 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteElementFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteElementFix.java @@ -1,12 +1,12 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.daemon.impl.quickfix; -import com.intellij.codeInsight.daemon.impl.actions.IntentionActionWithFixAllOption; import com.intellij.codeInsight.intention.FileModifier; import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.codeInspection.CommonQuickFixBundle; -import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement; +import com.intellij.codeInspection.PsiUpdateModCommandAction; import com.intellij.codeInspection.util.IntentionName; +import com.intellij.modcommand.ModPsiUpdater; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; @@ -20,7 +20,7 @@ import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class DeleteElementFix extends LocalQuickFixAndIntentionActionOnPsiElement implements IntentionActionWithFixAllOption { +public class DeleteElementFix extends PsiUpdateModCommandAction { private final @Nls String myText; public DeleteElementFix(@NotNull PsiElement element) { @@ -33,11 +33,9 @@ public class DeleteElementFix extends LocalQuickFixAndIntentionActionOnPsiElemen myText = text; } - @Nls - @NotNull @Override - public String getText() { - return myText == null ? getFamilyName() : myText; + protected @Nullable Presentation getPresentation(@NotNull ActionContext context, @NotNull PsiElement element) { + return Presentation.of(myText == null ? getFamilyName() : myText).withFixAllOption(this); } @Nls @@ -48,12 +46,8 @@ public class DeleteElementFix extends LocalQuickFixAndIntentionActionOnPsiElemen } @Override - public void invoke(@NotNull Project project, - @NotNull PsiFile file, - @Nullable Editor editor, - @NotNull PsiElement startElement, - @NotNull PsiElement endElement) { - new CommentTracker().deleteAndRestoreComments(startElement); + protected void invoke(@NotNull ActionContext context, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { + new CommentTracker().deleteAndRestoreComments(element); } public static final class DeleteMultiFix implements IntentionAction { diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java index 7a6f41502383..1e22beae98ab 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java @@ -775,7 +775,7 @@ public final class QuickFixFactoryImpl extends QuickFixFactory { @NotNull @Override public LocalQuickFixAndIntentionActionOnPsiElement createDeleteFix(@NotNull PsiElement element) { - return new DeleteElementFix(element); + return LocalQuickFixAndIntentionActionOnPsiElement.from(new DeleteElementFix(element), element); } @Override @@ -786,7 +786,7 @@ public final class QuickFixFactoryImpl extends QuickFixFactory { @NotNull @Override public LocalQuickFixAndIntentionActionOnPsiElement createDeleteFix(@NotNull PsiElement element, @Nls @NotNull String text) { - return new DeleteElementFix(element, text); + return LocalQuickFixAndIntentionActionOnPsiElement.from(new DeleteElementFix(element, text), element); } @NotNull diff --git a/java/java-impl/src/com/intellij/codeInspection/javaDoc/JavadocDeclarationInspection.java b/java/java-impl/src/com/intellij/codeInspection/javaDoc/JavadocDeclarationInspection.java index 52bf02b27c93..40c3a9d4be06 100644 --- a/java/java-impl/src/com/intellij/codeInspection/javaDoc/JavadocDeclarationInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/javaDoc/JavadocDeclarationInspection.java @@ -482,10 +482,10 @@ public class JavadocDeclarationInspection extends LocalInspectionTool { String inlineRegion = region != null && markup.getRegionStart(region) == null ? null : region; String inlineRendered = renderText(markup, inlineRegion); if (!externalRendered.equals(inlineRendered)) { - holder.registerProblem(nameElement, - JavaBundle.message("inspection.message.external.snippet.differs.from.inline.snippet"), - new SynchronizeInlineMarkupFix(externalRendered), - new DeleteElementFix(body)); + holder.problem(nameElement, JavaBundle.message("inspection.message.external.snippet.differs.from.inline.snippet")) + .fix(new SynchronizeInlineMarkupFix(externalRendered)) + .fix(new DeleteElementFix(body)) + .register(); } } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAnnotatorHighlightingTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAnnotatorHighlightingTest.java index 95c40ac8f444..1c1d4a3ab237 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAnnotatorHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAnnotatorHighlightingTest.java @@ -1,4 +1,4 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.java.codeInsight.daemon; import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; @@ -565,14 +565,10 @@ public class LightAnnotatorHighlightingTest extends LightDaemonAnalyzerTestCase holder.newAnnotation(HighlightSeverity.ERROR, "i hate it") .newFix(new DeleteElementFix(element) { @Override - public boolean isAvailable(@NotNull Project project, - @NotNull PsiFile file, - @Nullable Editor editor, - @NotNull PsiElement startElement, - @NotNull PsiElement endElement) { - return FIX_ENABLED; + protected @Nullable Presentation getPresentation(@NotNull ActionContext context, @NotNull PsiElement element) { + return FIX_ENABLED ? super.getPresentation(context, element) : null; } - }).registerFix().create(); + }.asIntention()).registerFix().create(); iDidIt(); } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/EmptyStatementBodyInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/EmptyStatementBodyInspection.java index d06e1369cdc7..e1b04121cb16 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/EmptyStatementBodyInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/EmptyStatementBodyInspection.java @@ -162,7 +162,7 @@ public class EmptyStatementBodyInspection extends BaseInspection { @NotNull private static LocalQuickFix createFix(@NotNull PsiStatement statement, PsiExpression expression) { if (expression == null) { - return new DeleteElementFix(statement); + return new DeleteElementFix(statement).asQuickFix(); } return new DeleteSideEffectsAwareFix(statement, expression).asQuickFix(); } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantOperationOnEmptyContainerInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantOperationOnEmptyContainerInspection.java index 259410383e30..7128dd68ab99 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantOperationOnEmptyContainerInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantOperationOnEmptyContainerInspection.java @@ -1,4 +1,4 @@ -// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.siyeh.ig.redundancy; import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix; @@ -12,6 +12,7 @@ import com.intellij.codeInspection.dataFlow.jvm.SpecialField; import com.intellij.codeInspection.dataFlow.types.DfType; import com.intellij.codeInspection.util.InspectionMessage; import com.intellij.java.JavaBundle; +import com.intellij.modcommand.ModCommandAction; import com.intellij.psi.*; import com.intellij.psi.util.InheritanceUtil; import com.intellij.psi.util.PsiUtil; @@ -68,11 +69,11 @@ public class RedundantOperationOnEmptyContainerInspection extends AbstractBaseJa if (container != null) { String msg = getProblemMessage(container); if (msg == null) return; - LocalQuickFix fix = null; + ModCommandAction fix = null; if (ExpressionUtils.isVoidContext(call)) { fix = new DeleteElementFix(call, InspectionGadgetsBundle.message("remove.call.fix.family.name")); } - holder.registerProblem(container, msg, LocalQuickFix.notNullElements(fix, getFindCauseFix(container))); + holder.problem(container, msg).maybeFix(fix).fix(getFindCauseFix(container)).register(); } } @@ -82,12 +83,10 @@ public class RedundantOperationOnEmptyContainerInspection extends AbstractBaseJa if (value == null) return; String msg = getProblemMessage(value); if (msg == null) return; - holder.registerProblem(value, msg, - new DeleteElementFix(statement, InspectionGadgetsBundle.message("remove.loop.fix.family.name")), - getFindCauseFix(value)); + holder.problem(value, msg).fix(new DeleteElementFix(statement, InspectionGadgetsBundle.message("remove.loop.fix.family.name"))).fix(getFindCauseFix(value)).register(); } - private @NotNull LocalQuickFix getFindCauseFix(@NotNull PsiExpression value) { + private static @NotNull LocalQuickFix getFindCauseFix(@NotNull PsiExpression value) { PsiType type = value.getType(); SpecialField field; if (type instanceof PsiArrayType) { diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java index ae4527f06c1c..23e8a4993ae3 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java @@ -518,7 +518,7 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns @NotNull @PropertyKey(resourceBundle = BUNDLE) String key) { if (argument == null) return null; LocalQuickFix fix = - new DeleteElementFix(argument, InspectionGadgetsBundle.message("inspection.redundant.string.remove.argument.fix.name")); + new DeleteElementFix(argument, InspectionGadgetsBundle.message("inspection.redundant.string.remove.argument.fix.name")).asQuickFix(); return myManager.createProblemDescriptor(argument, InspectionGadgetsBundle.message(key), myIsOnTheFly, @@ -556,8 +556,8 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns if (ExpressionUtils.isZero(args[0])) { return getProblem(call, "inspection.redundant.string.call.message"); } - DeleteElementFix fix = - new DeleteElementFix(args[1], InspectionGadgetsBundle.message("inspection.redundant.string.remove.argument.fix.name")); + LocalQuickFix fix = + new DeleteElementFix(args[1], InspectionGadgetsBundle.message("inspection.redundant.string.remove.argument.fix.name")).asQuickFix(); return myManager.createProblemDescriptor(args[1], InspectionGadgetsBundle.message("inspection.redundant.string.length.argument.message"), fix, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myIsOnTheFly);