From 7f46ed8e53725577a5e57bb0a53d54f65bee4ccc Mon Sep 17 00:00:00 2001 From: svtk Date: Tue, 27 Dec 2011 17:44:35 +0400 Subject: [PATCH] added fix "replace dot call with safe call" (from Sergey Ignatov) --- .../jetbrains/jet/plugin/JetBundle.properties | 1 + .../jet/plugin/quickfix/QuickFixes.java | 2 + .../quickfix/ReplaceDotCallWithSafeCall.java | 65 +++++++++++++++++++ .../quickfix/expressions/afterUnsafeCall.kt | 4 ++ .../quickfix/expressions/beforeUnsafeCall.kt | 4 ++ 5 files changed, 76 insertions(+) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceDotCallWithSafeCall.java create mode 100644 idea/testData/quickfix/expressions/afterUnsafeCall.kt create mode 100644 idea/testData/quickfix/expressions/beforeUnsafeCall.kt diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index 117e0088faa..8b19a79b430 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -24,5 +24,6 @@ remove.elvis.operator=Remove elvis operator replace.operation.in.binary.expression=Replace operation in a binary expression replace.cast.with.static.assert=Replace a cast with a static assert replace.with.dot.call=Replace with dot call +replace.with.safe.call=Replace with safe call change.to.backing.field=Change reference to backing field implement.members=Implement members \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index 7c1569b2534..7f3cb3abd63 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -126,5 +126,7 @@ public class QuickFixes { ChangeVariableMutabilityFix changeVariableMutabilityFix = new ChangeVariableMutabilityFix(); actionMap.put(Errors.VAL_WITH_SETTER, changeVariableMutabilityFix); actionMap.put(Errors.VAL_REASSIGNMENT, changeVariableMutabilityFix); + + actionMap.put(Errors.UNSAFE_CALL, new ReplaceDotCallWithSafeCall()); } } \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceDotCallWithSafeCall.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceDotCallWithSafeCall.java new file mode 100644 index 00000000000..0ddd2731c6f --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceDotCallWithSafeCall.java @@ -0,0 +1,65 @@ +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/testData/quickfix/expressions/afterUnsafeCall.kt b/idea/testData/quickfix/expressions/afterUnsafeCall.kt new file mode 100644 index 00000000000..af830372f6e --- /dev/null +++ b/idea/testData/quickfix/expressions/afterUnsafeCall.kt @@ -0,0 +1,4 @@ +// "Replace with safe call" "true" +fun foo(a: Int?) { + a?.plus(1) +} diff --git a/idea/testData/quickfix/expressions/beforeUnsafeCall.kt b/idea/testData/quickfix/expressions/beforeUnsafeCall.kt new file mode 100644 index 00000000000..468555b4ff0 --- /dev/null +++ b/idea/testData/quickfix/expressions/beforeUnsafeCall.kt @@ -0,0 +1,4 @@ +// "Replace with safe call" "true" +fun foo(a: Int?) { + a.plus(1) +}