From 4272dafc364e7d53bb2f083baeb6a1eb0750b149 Mon Sep 17 00:00:00 2001 From: svtk Date: Tue, 27 Dec 2011 19:21:33 +0400 Subject: [PATCH] ReplaceCallFix instead of two ReplaceDotCallWithSafeCall & ReplaceSafeCallWithDotCall --- .../jet/lang/diagnostics/Errors.java | 2 +- .../jet/plugin/quickfix/QuickFixes.java | 6 +- .../jet/plugin/quickfix/ReplaceCallFix.java | 68 +++++++++++++++++++ .../quickfix/ReplaceDotCallWithSafeCall.java | 65 ------------------ .../quickfix/ReplaceSafeCallWithDotCall.java | 56 --------------- .../expressions/afterUnnecessarySafeCall1.kt | 2 +- 6 files changed, 73 insertions(+), 126 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceCallFix.java delete mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceDotCallWithSafeCall.java delete mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceSafeCallWithDotCall.java diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java index 7f6ce8305d4..3f4b049f685 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java @@ -280,7 +280,7 @@ public interface Errors { ParameterizedDiagnosticFactory1 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 UNSUPPORTED = ParameterizedDiagnosticFactory1.create(ERROR, "Unsupported [{0}]"); - PsiElementOnlyDiagnosticFactory1 UNNECESSARY_SAFE_CALL = PsiElementOnlyDiagnosticFactory1.create(WARNING, "Unnecessary safe call on a non-null receiver of type {0}"); + ParameterizedDiagnosticFactory1 UNNECESSARY_SAFE_CALL = ParameterizedDiagnosticFactory1.create(WARNING, "Unnecessary safe call on a non-null receiver of type {0}"); ParameterizedDiagnosticFactory2 NAME_IN_CONSTRAINT_IS_NOT_A_TYPE_PARAMETER = new ParameterizedDiagnosticFactory2(ERROR, "{0} does not refer to a type parameter of {1}") { @Override protected String makeMessageForA(@NotNull JetTypeConstraint jetTypeConstraint) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index 7f3cb3abd63..54a4248003e 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -87,7 +87,6 @@ public class QuickFixes { add(Errors.USELESS_ELVIS, RemoveRightPartOfBinaryExpressionFix.createRemoveElvisOperatorFactory()); - add(Errors.UNNECESSARY_SAFE_CALL, ReplaceSafeCallWithDotCall.createFactory()); JetIntentionActionFactory 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 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)); } } \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceCallFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceCallFix.java new file mode 100644 index 00000000000..aa7a0b3d512 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceCallFix.java @@ -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; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceDotCallWithSafeCall.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceDotCallWithSafeCall.java deleted file mode 100644 index 0ddd2731c6f..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceDotCallWithSafeCall.java +++ /dev/null @@ -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; - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceSafeCallWithDotCall.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceSafeCallWithDotCall.java deleted file mode 100644 index c6a18316996..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceSafeCallWithDotCall.java +++ /dev/null @@ -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 { - - 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 createFactory() { - return new JetIntentionActionFactory() { - @Override - public JetIntentionAction createAction(DiagnosticWithPsiElement diagnostic) { - assert diagnostic.getPsiElement() instanceof JetElement; - return new ReplaceSafeCallWithDotCall((JetElement) diagnostic.getPsiElement()); - } - }; - } -} diff --git a/idea/testData/quickfix/expressions/afterUnnecessarySafeCall1.kt b/idea/testData/quickfix/expressions/afterUnnecessarySafeCall1.kt index 55d3d6c64f3..6c1ad9fe270 100644 --- a/idea/testData/quickfix/expressions/afterUnnecessarySafeCall1.kt +++ b/idea/testData/quickfix/expressions/afterUnnecessarySafeCall1.kt @@ -1,4 +1,4 @@ // "Replace with dot call" "true" fun foo(a: Any) { - a.equals(0) + a.equals(0) }