mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-30 10:20:15 +07:00
IDEA-216258 Provide fixes for the receiver should be the first parameter
GitOrigin-RevId: ff4c5fc6fa17d2d78c9ac785ce09ceb5860d29bc
This commit is contained in:
committed by
intellij-monorepo-bot
parent
a6fe1409b9
commit
3e579cda88
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Move 'this' to the begin of the list" "true"
|
||||
class X {
|
||||
void foo(X this, X x) {}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Move 'this' to the begin of the list" "true"
|
||||
class X {
|
||||
void foo(X this, X x, double d) {}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Move 'this' to the begin of the list" "true"
|
||||
class X {
|
||||
void foo(X x, X this<caret>) {}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Move 'this' to the begin of the list" "true"
|
||||
class X {
|
||||
void foo(X x, double d, X this<caret>) {}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Remove receiver parameter" "true"
|
||||
class X {
|
||||
void foo(X x) {}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Remove receiver parameter" "true"
|
||||
class X {
|
||||
void foo(X x, X this<caret>) {}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user