From b0958ce321199d636001b63de4b630f703495738 Mon Sep 17 00:00:00 2001 From: Sergey Lukjanov Date: Wed, 20 Jun 2012 12:14:27 +0400 Subject: [PATCH] Quickfix for replacing unnecessary non-null asserted (!!.) call with dot call is moved to ReplaceCallFix. --- .../jet/plugin/quickfix/QuickFixes.java | 4 +- .../jet/plugin/quickfix/ReplaceCallFix.java | 46 ++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index 3ce8ed98864..0b98f37fb27 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -133,9 +133,11 @@ public class QuickFixes { actions.put(VAL_WITH_SETTER, changeVariableMutabilityFix); actions.put(VAL_REASSIGNMENT, changeVariableMutabilityFix); - actions.put(UNNECESSARY_SAFE_CALL, ReplaceCallFix.toDotCall()); + actions.put(UNNECESSARY_SAFE_CALL, ReplaceCallFix.toDotCallFromSafeCall()); actions.put(UNSAFE_CALL, ReplaceCallFix.toSafeCall()); + actions.put(UNSAFE_CALL, ReplaceCallFix.toNonNullAssertedCall()); + actions.put(UNNECESSARY_NOT_NULL_ASSERTION, ReplaceCallFix.toDotCallFromNonNullAssertedCall()); actions.put(UNNECESSARY_NOT_NULL_ASSERTION, new UnnecessaryNotNullAssertionFix()); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceCallFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceCallFix.java index da74eed8fa8..0faf723cab8 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceCallFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceCallFix.java @@ -21,10 +21,12 @@ 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.impl.source.tree.LeafPsiElement; 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.lexer.JetTokens; import org.jetbrains.jet.plugin.JetBundle; /** @@ -57,10 +59,17 @@ public class ReplaceCallFix implements IntentionAction { /** * @return quickfix for replacing unnecessary safe (?.) call with dot call */ - public static ReplaceCallFix toDotCall() { + public static ReplaceCallFix toDotCallFromSafeCall() { return new ReplaceCallFix(true, false); } + /** + * @return quickfix for replacing unnecessary non-null asserted (!!.) call with dot call + */ + public static ReplaceCallFix toDotCallFromNonNullAssertedCall() { + return new ReplaceCallFix(false, false); + } + @NotNull @Override public String getText() { @@ -83,11 +92,6 @@ public class ReplaceCallFix implements IntentionAction { return false; } - private JetQualifiedExpression getCallExpression(@NotNull Editor editor, @NotNull JetFile file) { - final PsiElement elementAtCaret = file.findElementAt(editor.getCaretModel().getOffset()); - return PsiTreeUtil.getParentOfType(elementAtCaret, fromDot ? JetDotQualifiedExpression.class : JetSafeQualifiedExpression.class); - } - @Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { JetQualifiedExpression callExpression = getCallExpression(editor, (JetFile) file); @@ -95,6 +99,26 @@ public class ReplaceCallFix implements IntentionAction { JetExpression selector = callExpression.getSelectorExpression(); if (selector != null) { + if (!fromDot && !safe) { + final PsiElement elementAtCaret = getElementAtCaret(editor, file); + if (elementAtCaret instanceof LeafPsiElement) { + final LeafPsiElement leafElement = (LeafPsiElement) elementAtCaret; + PsiElement exclExclElement = null; + if (leafElement.getElementType() == JetTokens.EXCLEXCL) { + exclExclElement = leafElement; + } + else if (leafElement.getElementType() == JetTokens.DOT) { + PsiElement prevSibling = leafElement.getPrevSibling(); + if (prevSibling != null) { + exclExclElement = prevSibling.getLastChild(); + } + } + if (exclExclElement != null) { + exclExclElement.delete(); + } + } + } + JetQualifiedExpression newElement = (JetQualifiedExpression) JetPsiFactory.createExpression( project, callExpression.getReceiverExpression().getText() + (fromDot ? (safe ? "?." : "!!.") : ".") + selector.getText()); @@ -107,4 +131,14 @@ public class ReplaceCallFix implements IntentionAction { public boolean startInWriteAction() { return true; } + + private JetQualifiedExpression getCallExpression(@NotNull Editor editor, @NotNull JetFile file) { + final PsiElement elementAtCaret = getElementAtCaret(editor, file); + return PsiTreeUtil + .getParentOfType(elementAtCaret, fromDot || !safe ? JetDotQualifiedExpression.class : JetSafeQualifiedExpression.class); + } + + private static PsiElement getElementAtCaret(Editor editor, PsiFile file) { + return file.findElementAt(editor.getCaretModel().getOffset()); + } }