IDEA-245312 Exception on removing throws clause from record constructor

GitOrigin-RevId: 03cfe1f332222795cc78522dea6b160498f31471
This commit is contained in:
Tagir Valeev
2020-07-07 04:46:37 +00:00
committed by intellij-monorepo-bot
parent eb701312fd
commit a9d4eed68b
9 changed files with 92 additions and 2 deletions
@@ -384,6 +384,9 @@ public abstract class QuickFixFactory {
@NotNull
public abstract IntentionAction createEnableOptimizeImportsOnTheFlyFix();
@NotNull
public abstract IntentionAction createDeleteFix(@NotNull PsiElement @NotNull ... elements);
@NotNull
public abstract LocalQuickFixAndIntentionActionOnPsiElement createDeleteFix(@NotNull PsiElement element);
@@ -2028,7 +2028,7 @@ public final class HighlightMethodUtil {
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeParameterList)
.descriptionAndTooltip(JavaErrorBundle.message("record.special.method.type.parameters", methodTitle))
.create();
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createDeleteFix(typeParameterList));
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createDeleteFix(typeParameterList.getTypeParameters()));
return info;
}
if (method.isConstructor() && PsiUtil.getLanguageLevel(method) != LanguageLevel.JDK_14_PREVIEW) {
@@ -2057,7 +2057,7 @@ public final class HighlightMethodUtil {
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(throwsList)
.descriptionAndTooltip(JavaErrorBundle.message("record.special.method.throws", methodTitle))
.create();
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createDeleteFix(throwsList));
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createDeleteFix(throwsList.getReferenceElements()));
return info;
}
return null;
@@ -15,14 +15,20 @@
*/
package com.intellij.codeInsight.daemon.impl.quickfix;
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.util.IntentionName;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.JavaElementKind;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import com.siyeh.ig.psiutils.CommentTracker;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -63,4 +69,44 @@ public class DeleteElementFix extends LocalQuickFixAndIntentionActionOnPsiElemen
@NotNull PsiElement endElement) {
new CommentTracker().deleteAndRestoreComments(startElement);
}
public static final class DeleteMultiFix implements IntentionAction {
private final @NotNull PsiElement @NotNull [] myElements;
public DeleteMultiFix(@NotNull PsiElement @NotNull ... elements) {
myElements = elements;
}
@Override
public @IntentionName @NotNull String getText() {
return getFamilyName();
}
@Override
public @NotNull String getFamilyName() {
return CommonQuickFixBundle.message("fix.remove.title", JavaElementKind.UNKNOWN.object());
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return true;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
for (PsiElement element : myElements) {
new CommentTracker().deleteAndRestoreComments(element);
}
}
@Override
public boolean startInWriteAction() {
return true;
}
@Override
public @NotNull FileModifier getFileModifierForPreview(@NotNull PsiFile target) {
return new DeleteMultiFix(ContainerUtil.map2Array(myElements, PsiElement.class, e -> PsiTreeUtil.findSameElementInCopy(e, target)));
}
}
}
@@ -700,6 +700,11 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
return new DeleteElementFix(element);
}
@Override
public @NotNull IntentionAction createDeleteFix(@NotNull PsiElement @NotNull ... elements) {
return new DeleteElementFix.DeleteMultiFix(elements);
}
@NotNull
@Override
public LocalQuickFixAndIntentionActionOnPsiElement createDeleteFix(@NotNull PsiElement element, @Nls @NotNull String text) {
@@ -0,0 +1,4 @@
// "Remove element" "true"
record R() {
R() {}
}
@@ -0,0 +1,4 @@
// "Remove element" "true"
record R() {
R() {}
}
@@ -0,0 +1,4 @@
// "Remove element" "true"
record R() {
R() throws<caret> Exception, Error {}
}
@@ -0,0 +1,4 @@
// "Remove element" "true"
record R() {
<T, <caret>U> R() {}
}
@@ -0,0 +1,20 @@
// 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.
package com.intellij.java.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.testFramework.LightProjectDescriptor;
import org.jetbrains.annotations.NotNull;
import static com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase.JAVA_15;
public class DeleteInvalidElementFixTest extends LightQuickFixParameterizedTestCase {
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return JAVA_15;
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/deleteInvalidElement";
}
}