split UnimplementInterfaceAction to two fixes with immutable text for clarity (boolean parameter considered harmful) and for stability

GitOrigin-RevId: 3942585b2071cb81733080dee122913cdac001f1
This commit is contained in:
Alexey Kudravtsev
2022-08-03 09:43:32 +02:00
committed by intellij-monorepo-bot
parent 5224aa124a
commit 5145671442
28 changed files with 59 additions and 38 deletions

View File

@@ -4,7 +4,6 @@ package com.intellij.codeInsight.intention;
import com.intellij.codeInsight.daemon.QuickFixActionRegistrar;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.codeInspection.LocalQuickFixOnPsiElement;
import com.intellij.codeInspection.util.IntentionName;
import com.intellij.lang.jvm.actions.JvmElementActionsFactory;
import com.intellij.openapi.application.ApplicationManager;
@@ -530,7 +529,7 @@ public abstract class QuickFixFactory {
public abstract @NotNull IntentionAction createSealClassFromPermitsListFix(@NotNull PsiClass classFromPermitsList);
public abstract @NotNull IntentionAction createUnimplementInterfaceAction(@NotNull String className, boolean isDuplicates);
public abstract @NotNull IntentionAction createRemoveDuplicateExtendsAction(@NotNull String className);
public abstract @NotNull IntentionAction createMoveMemberIntoClassFix(@NotNull PsiErrorElement errorElement);

View File

@@ -714,7 +714,7 @@ public final class HighlightClassUtil {
String description = JavaErrorBundle.message("duplicate.class", name);
HighlightInfo info =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createUnimplementInterfaceAction(name, true));
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createRemoveDuplicateExtendsAction(name));
return info;
}
return null;

View File

@@ -1044,8 +1044,8 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
}
@Override
public @NotNull IntentionAction createUnimplementInterfaceAction(@NotNull String className, boolean isDuplicates) {
return new UnimplementInterfaceAction(className, isDuplicates);
public @NotNull IntentionAction createRemoveDuplicateExtendsAction(@NotNull String className) {
return new UnimplementInterfaceAction.RemoveDuplicateExtendFix(className);
}
@Override

View File

@@ -18,24 +18,19 @@ import java.util.*;
public class UnimplementInterfaceAction extends BaseElementAtCaretIntentionAction {
private String myName = "Interface";
private final String myClassName;
private final boolean myIsDuplicates;
final String myClassName;
public UnimplementInterfaceAction() {
this(null, false);
this(null);
}
public UnimplementInterfaceAction(String className, boolean isDuplicates) {
public UnimplementInterfaceAction(String className) {
myClassName = className;
myIsDuplicates = isDuplicates;
}
@Override
@NotNull
public String getText() {
if (myIsDuplicates) {
return JavaBundle.message("intention.text.implements.list.remove.others", myClassName);
}
return JavaBundle.message("intention.text.unimplement.0", myName);
}
@@ -45,6 +40,9 @@ public class UnimplementInterfaceAction extends BaseElementAtCaretIntentionActio
return JavaBundle.message("intention.family.unimplement.interface.class");
}
boolean isRemoveDuplicateExtends() {
return false;
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
final PsiReferenceList referenceList = PsiTreeUtil.getParentOfType(element, PsiReferenceList.class);
@@ -61,7 +59,7 @@ public class UnimplementInterfaceAction extends BaseElementAtCaretIntentionActio
final PsiClass targetClass = ObjectUtils.tryCast(topLevelRef.resolve(), PsiClass.class);
if (targetClass == null) return false;
if (myIsDuplicates) return true;
if (isRemoveDuplicateExtends()) return true;
for (PsiJavaCodeReferenceElement refElement : referenceList.getReferenceElements()) {
if (isDuplicate(topLevelRef, refElement, targetClass)) return false;
@@ -98,7 +96,7 @@ public class UnimplementInterfaceAction extends BaseElementAtCaretIntentionActio
final PsiClass targetClass = ObjectUtils.tryCast(target, PsiClass.class);
if (targetClass == null) return;
if (myIsDuplicates) {
if (isRemoveDuplicateExtends()) {
for (PsiJavaCodeReferenceElement refElement : referenceList.getReferenceElements()) {
if (isDuplicate(topLevelRef, refElement, targetClass)) {
refElement.delete();
@@ -144,4 +142,21 @@ public class UnimplementInterfaceAction extends BaseElementAtCaretIntentionActio
final PsiManager manager = aClass.getManager();
return !manager.areElementsEquivalent(otherElement, element) && manager.areElementsEquivalent(otherElement.resolve(), aClass);
}
public static class RemoveDuplicateExtendFix extends UnimplementInterfaceAction {
public RemoveDuplicateExtendFix(String className) {
super(className);
}
@Override
@NotNull
public String getText() {
return JavaBundle.message("intention.text.implements.list.remove.others", myClassName);
}
@Override
boolean isRemoveDuplicateExtends() {
return true;
}
}
}

View File

@@ -1,4 +1,4 @@
// "Unimplement Interface" "true-preview"
// "Unimplement" "true-preview"
class A {
public String toString() {
return super.toString();

View File

@@ -1,4 +1,4 @@
// "Unimplement Interface" "true-preview"
// "Unimplement" "true-preview"
class A {
public String toString() {
return super.toString();

View File

@@ -1,4 +1,4 @@
// "Unimplement Interface" "true-preview"
// "Unimplement" "true-preview"
class A {
public String toString() {
return super.toString();

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
public sealed interface A permits B {}
class C {}

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
class A {
}

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
class A {}
final class B {}

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
sealed class A permits C {}
class B {}

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
class A {}
class B {}

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
class A {}
sealed class B permits C {}

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
class A {
public String toString() {
return super.toString();

View File

@@ -1,3 +1,3 @@
// "Unimplement Interface" "true-preview"
// "Unimplement" "true-preview"
class X {
}

View File

@@ -1,4 +1,4 @@
// "Unimplement Interface" "true-preview"
// "Unimplement" "true-preview"
class A implements I<caret>I {
public String toString() {
return super.toString();

View File

@@ -1,4 +1,4 @@
// "Unimplement Interface" "true-preview"
// "Unimplement" "true-preview"
class A implements II<S<caret>tring> {
public String toString() {
return super.toString();

View File

@@ -1,4 +1,4 @@
// "Unimplement Interface" "true-preview"
// "Unimplement" "true-preview"
class A implements II<caret> {
public String toString() {
return super.toString();

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
public sealed interface A permits B {}
sealed class C permits B {}

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
sealed class A permits B {
}

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
sealed class A permits B {}
final class B extends <caret>A {}

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
sealed class A permits B, C {}
non-sealed class B extends A<caret> {}

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
sealed class A permits B {}
non-sealed class B extends <caret>A {}

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
sealed class A permits B {}
sealed class B extends <caret>A permits C {}

View File

@@ -1,4 +1,4 @@
// "Unimplement Class" "true-preview"
// "Unimplement" "true-preview"
class A implements A<caret> {
public String toString() {
return super.toString();

View File

@@ -1,4 +1,4 @@
// "Unimplement Interface" "true-preview"
// "Unimplement" "true-preview"
class X implements Comparable<String<caret>> {
@Override
public int compareTo(String o) {

View File

@@ -1,2 +1,2 @@
// "Unimplement Interface" "false"
// "Unimplement" "false"
class X implements Cloneable<caret>, Cloneable {}

View File

@@ -1,7 +1,9 @@
// Copyright 2000-2022 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.quickFix;
import com.intellij.codeInsight.daemon.quickFix.ActionHint;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
@@ -17,5 +19,10 @@ public class UnimplementIntentionTest extends LightQuickFixParameterizedTestCase
return "/codeInsight/daemonCodeAnalyzer/quickFix/unimplement";
}
@Override
protected ActionHint parseActionHintImpl(@NotNull PsiFile file, @NotNull String contents) {
return ActionHint.parse(file, contents, false);
}
}