[java-intentions] RedundantArrayForVarargsCallFix, RemoveRedundantParameterTypesFix: ModCommand migration

GitOrigin-RevId: 1233736a6935608aa42afd4d0df0c6855a02e8a1
This commit is contained in:
Tagir Valeev
2023-06-16 20:29:01 +00:00
committed by intellij-monorepo-bot
parent 91522732c3
commit 55e85c1cf0
8 changed files with 22 additions and 33 deletions
@@ -23,8 +23,8 @@ public class RedundantLambdaParameterTypeInspection extends AbstractBaseJavaLoca
RemoveRedundantParameterTypesFix.isApplicable(parameterList)) {
for (PsiParameter parameter : parameterList.getParameters()) {
if (parameter.getTypeElement() != null) {
holder.registerProblem(parameter.getTypeElement(), JavaBundle.message("inspection.message.lambda.parameter.type.is.redundant"),
new RemoveRedundantParameterTypesFix((PsiLambdaExpression)parameterList.getParent()));
holder.problem(parameter.getTypeElement(), JavaBundle.message("inspection.message.lambda.parameter.type.is.redundant"))
.fix(new RemoveRedundantParameterTypesFix((PsiLambdaExpression)parameterList.getParent())).register();
}
}
}
@@ -90,12 +90,11 @@ public class RedundantArrayForVarargsCallInspection extends AbstractBaseJavaLoca
}
private static final class RedundantArrayForVarargsCallFix implements LocalQuickFix {
private static final class RedundantArrayForVarargsCallFix extends PsiUpdateModCommandQuickFix {
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement arrayCreation = descriptor.getPsiElement();
if (!(arrayCreation instanceof PsiNewExpression)) return;
CommonJavaRefactoringUtil.inlineArrayCreationForVarargs((PsiNewExpression)arrayCreation);
protected void applyFix(@NotNull Project project, @NotNull PsiElement arrayCreation, @NotNull EditorUpdater updater) {
if (!(arrayCreation instanceof PsiNewExpression newExpression)) return;
CommonJavaRefactoringUtil.inlineArrayCreationForVarargs(newExpression);
}
@Override
@@ -2,45 +2,32 @@
package com.intellij.codeInsight.intention.impl;
import com.intellij.codeInspection.AnonymousCanBeLambdaInspection;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.codeInspection.EditorUpdater;
import com.intellij.codeInspection.PsiUpdateModCommandAction;
import com.intellij.java.JavaBundle;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.containers.ContainerUtil;
import com.siyeh.ig.psiutils.CommentTracker;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.Objects;
public class RemoveRedundantParameterTypesFix extends LocalQuickFixAndIntentionActionOnPsiElement {
public class RemoveRedundantParameterTypesFix extends PsiUpdateModCommandAction<PsiLambdaExpression> {
public RemoveRedundantParameterTypesFix(@NotNull PsiLambdaExpression lambdaExpression) {
super(lambdaExpression);
}
@Nls
@Override
public @NotNull String getText() {
public @NotNull String getFamilyName() {
return JavaBundle.message("quickfix.family.remove.redundant.parameter.types");
}
@Override
public @NotNull String getFamilyName() {
return getText();
}
@Override
public void invoke(@NotNull Project project,
@NotNull PsiFile file,
@Nullable Editor editor,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
removeLambdaParameterTypesIfPossible((PsiLambdaExpression)startElement);
protected void invoke(@NotNull ActionContext context, @NotNull PsiLambdaExpression lambda, @NotNull EditorUpdater updater) {
removeLambdaParameterTypesIfPossible(lambda);
}
public static boolean isApplicable(@NotNull PsiParameterList parameterList) {
@@ -1167,7 +1167,7 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
@Override
public @NotNull IntentionAction createRemoveRedundantLambdaParameterTypesFix(@NotNull PsiLambdaExpression lambdaExpression,
@IntentionName String message) {
return new RemoveRedundantLambdaParameterTypesFix(lambdaExpression, message);
return new RemoveRedundantLambdaParameterTypesFix(lambdaExpression, message).asIntention();
}
@Override
@@ -1183,10 +1183,9 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
myMessage = message;
}
@Nls
@Override
public @NotNull String getText() {
return myMessage;
protected @NotNull Presentation getPresentation(@NotNull ActionContext context, @NotNull PsiLambdaExpression element) {
return Presentation.of(myMessage);
}
}
@@ -2,6 +2,6 @@ class X {
void varargFunc(String s, Object... ss) {}
{
varargFunc("hello", new Object[]{<caret>/* 1 */ "World" });
varargFunc("hello", new Object[]<caret>{/* 1 */ "World" });
}
}
@@ -1,7 +1,7 @@
public class Main {
public static void main(String[] args) {
display(//c1
<caret>"hi");
"hi"<caret>);
}
private static void display(String... messages) { }
@@ -1,9 +1,10 @@
// 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.java.codeInspection;
import com.intellij.JavaTestUtil;
import com.intellij.codeInspection.miscGenerics.RedundantArrayForVarargsCallInspection;
import com.intellij.java.JavaBundle;
import com.intellij.openapi.application.impl.NonBlockingReadActionImpl;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
/**
@@ -30,6 +31,7 @@ public class RedundantArray4VarargsCallInspectionTest extends LightJavaCodeInsig
String name = getTestName(false);
myFixture.configureByFile(name + ".java");
myFixture.launchAction(myFixture.findSingleIntention(JavaBundle.message("inspection.redundant.array.creation.quickfix")));
NonBlockingReadActionImpl.waitForAsyncTaskCompletion();
myFixture.checkResultByFile(name + "_after.java");
}
}
@@ -4,6 +4,7 @@ package com.intellij.java.codeInspection;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.lambda.RedundantLambdaParameterTypeInspection;
import com.intellij.openapi.application.impl.NonBlockingReadActionImpl;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
@@ -86,6 +87,7 @@ public class RedundantLambdaParameterTypeFixTest extends LightJavaCodeInsightFix
myFixture.configureByFiles(getTestName(false) + ".java");
final IntentionAction singleIntention = myFixture.findSingleIntention(ourIntentionName);
myFixture.launchAction(singleIntention);
NonBlockingReadActionImpl.waitForAsyncTaskCompletion();
myFixture.checkResultByFile(getTestName(false) + ".java", getTestName(false) + "_after.java", true);
}