Quickfix for replacing unnecessary non-null asserted (!!.) call with dot call is moved to ReplaceCallFix.

This commit is contained in:
Sergey Lukjanov
2012-06-20 12:14:27 +04:00
parent 80485cbee1
commit b0958ce321
2 changed files with 43 additions and 7 deletions
@@ -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());
@@ -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());
}
}