IDEA-216258 Provide fixes for the receiver should be the first parameter

GitOrigin-RevId: ff4c5fc6fa17d2d78c9ac785ce09ceb5860d29bc
This commit is contained in:
Andrey.Cherkasov
2021-02-16 01:53:12 +03:00
committed by intellij-monorepo-bot
parent a6fe1409b9
commit 3e579cda88
11 changed files with 112 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ import com.intellij.codeInsight.daemon.JavaErrorBundle;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
import com.intellij.codeInsight.daemon.impl.quickfix.MakeReceiverParameterFirstFix;
import com.intellij.codeInsight.daemon.impl.quickfix.MoveAnnotationOnStaticMemberQualifyingTypeFix;
import com.intellij.codeInsight.daemon.impl.quickfix.QuickFixAction;
import com.intellij.codeInsight.daemon.impl.quickfix.ReplaceVarWithExplicitTypeFix;
@@ -715,7 +716,13 @@ public final class AnnotationsHighlightUtil {
PsiElement leftNeighbour = PsiTreeUtil.skipWhitespacesAndCommentsBackward(parameter);
if (leftNeighbour != null && !PsiUtil.isJavaToken(leftNeighbour, JavaTokenType.LPARENTH)) {
String text = JavaErrorBundle.message("receiver.wrong.position");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parameter.getIdentifier()).descriptionAndTooltip(text).create();
HighlightInfo info =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parameter.getIdentifier()).descriptionAndTooltip(text).create();
QuickFixAction.registerQuickFixAction(info, new MakeReceiverParameterFirstFix(parameter));
QuickFixAction.registerQuickFixAction(
info,
QUICK_FIX_FACTORY.createDeleteFix(parameter, QuickFixBundle.message("remove.receiver.parameter")));
return info;
}
return null;

View File

@@ -0,0 +1,56 @@
// Copyright 2000-2021 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.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.codeInspection.util.IntentionFamilyName;
import com.intellij.codeInspection.util.IntentionName;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiParameterList;
import com.intellij.psi.PsiReceiverParameter;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
public class MakeReceiverParameterFirstFix implements IntentionAction {
@NotNull final PsiReceiverParameter myParameter;
public MakeReceiverParameterFirstFix(@NotNull PsiReceiverParameter parameter) {
myParameter = parameter;
}
@Override
public @IntentionName @NotNull String getText() {
return QuickFixBundle.message("make.receiver.parameter.first.text");
}
@Override
public @NotNull @IntentionFamilyName String getFamilyName() {
return QuickFixBundle.message("make.receiver.parameter.first.family");
}
@Override
public boolean isAvailable(@NotNull Project project,
Editor editor,
PsiFile file) {
if (!myParameter.isValid() || !BaseIntentionAction.canModify(myParameter)) return false;
final PsiParameterList parameterList = ObjectUtils.tryCast(myParameter.getParent(), PsiParameterList.class);
return parameterList != null && !parameterList.isEmpty();
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final PsiParameterList parameterList = ObjectUtils.tryCast(myParameter.getParent(), PsiParameterList.class);
if (parameterList == null || parameterList.isEmpty()) return;
parameterList.addBefore(myParameter, parameterList.getParameter(0));
myParameter.delete();
}
@Override
public boolean startInWriteAction() {
return true;
}
}

View File

@@ -133,6 +133,8 @@ make.class.an.interface.text=Make ''{0}'' an interface
make.interface.an.class.text=Make ''{0}'' a class
make.vararg.parameter.last.family=Make vararg parameter last
make.vararg.parameter.last.text=Move ''{0}'' to the end of the list
make.receiver.parameter.first.family=Make receiver parameter first
make.receiver.parameter.first.text=Move 'this' to the begin of the list
remove.receiver.parameter=Remove receiver parameter
fix.parameter.type.family=Fix Parameter Type
fix.parameter.type.text=Make ''{0}'' take parameter of type ''{1}'' here

View File

@@ -0,0 +1,4 @@
// "Move 'this' to the begin of the list" "true"
class X {
void foo(X this, X x) {}
}

View File

@@ -0,0 +1,4 @@
// "Move 'this' to the begin of the list" "true"
class X {
void foo(X this, X x, double d) {}
}

View File

@@ -0,0 +1,4 @@
// "Move 'this' to the begin of the list" "true"
class X {
void foo(X x, X this<caret>) {}
}

View File

@@ -0,0 +1,4 @@
// "Move 'this' to the begin of the list" "true"
class X {
void foo(X x, double d, X this<caret>) {}
}

View File

@@ -0,0 +1,4 @@
// "Remove receiver parameter" "true"
class X {
void foo(X x) {}
}

View File

@@ -0,0 +1,4 @@
// "Remove receiver parameter" "true"
class X {
void foo(X x, X this<caret>) {}
}

View File

@@ -0,0 +1,11 @@
// Copyright 2000-2021 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;
public class MakeReceiverParameterFirstTest extends LightQuickFixParameterizedTestCase {
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/makeReceiverParameterFirst";
}
}

View File

@@ -0,0 +1,11 @@
// Copyright 2000-2021 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;
public class RemoveReceiverParameterTest extends LightQuickFixParameterizedTestCase {
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/removeReceiverParameter";
}
}