ReplaceCallFix instead of two ReplaceDotCallWithSafeCall & ReplaceSafeCallWithDotCall

This commit is contained in:
svtk
2011-12-27 19:21:33 +04:00
parent a5d8ff4669
commit 4272dafc36
6 changed files with 73 additions and 126 deletions
@@ -280,7 +280,7 @@ public interface Errors {
ParameterizedDiagnosticFactory1<JetType> UNSAFE_CALL = ParameterizedDiagnosticFactory1.create(ERROR, "Only safe calls (?.) are allowed on a nullable receiver of type {0}");
SimpleDiagnosticFactory AMBIGUOUS_LABEL = SimpleDiagnosticFactory.create(ERROR, "Ambiguous label");
ParameterizedDiagnosticFactory1<String> UNSUPPORTED = ParameterizedDiagnosticFactory1.create(ERROR, "Unsupported [{0}]");
PsiElementOnlyDiagnosticFactory1<JetElement, JetType> UNNECESSARY_SAFE_CALL = PsiElementOnlyDiagnosticFactory1.create(WARNING, "Unnecessary safe call on a non-null receiver of type {0}");
ParameterizedDiagnosticFactory1<JetType> UNNECESSARY_SAFE_CALL = ParameterizedDiagnosticFactory1.create(WARNING, "Unnecessary safe call on a non-null receiver of type {0}");
ParameterizedDiagnosticFactory2<JetTypeConstraint, JetTypeParameterListOwner> NAME_IN_CONSTRAINT_IS_NOT_A_TYPE_PARAMETER = new ParameterizedDiagnosticFactory2<JetTypeConstraint, JetTypeParameterListOwner>(ERROR, "{0} does not refer to a type parameter of {1}") {
@Override
protected String makeMessageForA(@NotNull JetTypeConstraint jetTypeConstraint) {
@@ -87,7 +87,6 @@ public class QuickFixes {
add(Errors.USELESS_ELVIS, RemoveRightPartOfBinaryExpressionFix.createRemoveElvisOperatorFactory());
add(Errors.UNNECESSARY_SAFE_CALL, ReplaceSafeCallWithDotCall.createFactory());
JetIntentionActionFactory<JetModifierList> removeRedundantModifierFactory = RemoveModifierFix.createRemoveModifierFromListFactory(true);
add(Errors.REDUNDANT_MODIFIER, removeRedundantModifierFactory);
@@ -109,7 +108,7 @@ public class QuickFixes {
add(Errors.GETTER_VISIBILITY_DIFFERS_FROM_PROPERTY_VISIBILITY, removeModifierFactory);
add(Errors.REDUNDANT_MODIFIER_IN_GETTER, removeRedundantModifierFactory);
add(Errors.ILLEGAL_MODIFIER, removeModifierFactory);
add(Errors.PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE, AddReturnTypeFix.createFactory());
JetIntentionActionFactory<JetSimpleNameExpression> changeToBackingFieldFactory = ChangeToBackingFieldFix.createFactory();
@@ -127,6 +126,7 @@ public class QuickFixes {
actionMap.put(Errors.VAL_WITH_SETTER, changeVariableMutabilityFix);
actionMap.put(Errors.VAL_REASSIGNMENT, changeVariableMutabilityFix);
actionMap.put(Errors.UNSAFE_CALL, new ReplaceDotCallWithSafeCall());
actionMap.put(Errors.UNNECESSARY_SAFE_CALL, new ReplaceCallFix(false));
actionMap.put(Errors.UNSAFE_CALL, new ReplaceCallFix(true));
}
}
@@ -0,0 +1,68 @@
package org.jetbrains.jet.plugin.quickfix;
import com.intellij.codeInsight.intention.IntentionAction;
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.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.plugin.JetBundle;
/**
* @author svtk
*/
public class ReplaceCallFix implements IntentionAction {
private final boolean toSafe;
public ReplaceCallFix(boolean toSafe) {
this.toSafe = toSafe;
}
@NotNull
@Override
public String getText() {
return toSafe ? JetBundle.message("replace.with.safe.call") : JetBundle.message("replace.with.dot.call");
}
@NotNull
@Override
public String getFamilyName() {
return toSafe ? JetBundle.message("replace.with.safe.call") : JetBundle.message("replace.with.dot.call");
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (file instanceof JetFile) {
return getCallExpression(editor, (JetFile) file) != null;
}
return false;
}
private JetQualifiedExpression getCallExpression(@NotNull Editor editor, @NotNull JetFile file) {
final PsiElement elementAtCaret = file.findElementAt(editor.getCaretModel().getOffset());
return PsiTreeUtil.getParentOfType(elementAtCaret, toSafe ? JetDotQualifiedExpression.class : JetSafeQualifiedExpression.class);
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
JetQualifiedExpression callExpression = getCallExpression(editor, (JetFile) file);
assert callExpression != null;
JetExpression selector = callExpression.getSelectorExpression();
if (selector != null) {
JetQualifiedExpression newElement = (JetQualifiedExpression) JetPsiFactory.createExpression(
project, callExpression.getReceiverExpression().getText() + (toSafe ? "?." : ".") + selector.getText());
callExpression.replace(newElement);
}
}
@Override
public boolean startInWriteAction() {
return true;
}
}
@@ -1,65 +0,0 @@
package org.jetbrains.jet.plugin.quickfix;
import com.intellij.codeInsight.intention.IntentionAction;
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.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.plugin.JetBundle;
/**
* @author ignatov
*/
@SuppressWarnings("IntentionDescriptionNotFoundInspection")
public class ReplaceDotCallWithSafeCall implements IntentionAction {
public ReplaceDotCallWithSafeCall() {
}
@NotNull
@Override
public String getText() {
return JetBundle.message("replace.with.safe.call");
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("replace.with.safe.call");
}
@Override
public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
if (file instanceof JetFile) {
return getDotCallExpression(editor, (JetFile) file) != null;
}
return false;
}
private static JetDotQualifiedExpression getDotCallExpression(@NotNull Editor editor, @NotNull JetFile file) {
final PsiElement elementAtCaret = file.findElementAt(editor.getCaretModel().getOffset());
return PsiTreeUtil.getParentOfType(elementAtCaret, JetDotQualifiedExpression.class);
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
JetDotQualifiedExpression dotCallExpression = getDotCallExpression(editor, (JetFile) file);
assert dotCallExpression != null;
JetExpression selector = dotCallExpression.getSelectorExpression();
if (selector != null) {
JetSafeQualifiedExpression newElement = (JetSafeQualifiedExpression) JetPsiFactory.createExpression(
project, dotCallExpression.getReceiverExpression().getText() + "?." + selector.getText());
dotCallExpression.replace(newElement);
}
}
@Override
public boolean startInWriteAction() {
return true;
}
}
@@ -1,56 +0,0 @@
package org.jetbrains.jet.plugin.quickfix;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.source.codeStyle.CodeEditUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.diagnostics.DiagnosticWithPsiElement;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.plugin.JetBundle;
/**
* @author svtk
*/
public class ReplaceSafeCallWithDotCall extends JetIntentionAction<JetElement> {
public ReplaceSafeCallWithDotCall(@NotNull JetElement element) {
super(element);
}
@NotNull
@Override
public String getText() {
return JetBundle.message("replace.with.dot.call");
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("replace.with.dot.call");
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
if (element instanceof JetSafeQualifiedExpression) {
JetSafeQualifiedExpression safeQualifiedExpression = (JetSafeQualifiedExpression) element;
JetDotQualifiedExpression newElement = (JetDotQualifiedExpression) JetPsiFactory.createExpression(project, "x.foo");
CodeEditUtil.replaceChild(newElement.getNode(), newElement.getSelectorExpression().getNode(), safeQualifiedExpression.getSelectorExpression().getNode());
CodeEditUtil.replaceChild(newElement.getNode(), newElement.getReceiverExpression().getNode(), safeQualifiedExpression.getReceiverExpression().getNode());
element.replace(newElement);
}
}
public static JetIntentionActionFactory<JetElement> createFactory() {
return new JetIntentionActionFactory<JetElement>() {
@Override
public JetIntentionAction<JetElement> createAction(DiagnosticWithPsiElement diagnostic) {
assert diagnostic.getPsiElement() instanceof JetElement;
return new ReplaceSafeCallWithDotCall((JetElement) diagnostic.getPsiElement());
}
};
}
}
@@ -1,4 +1,4 @@
// "Replace with dot call" "true"
fun foo(a: Any) {
<caret>a.equals(0)
a<caret>.equals(0)
}