added fix "replace dot call with safe call" (from Sergey Ignatov)
This commit is contained in:
@@ -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
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Replace with safe call" "true"
|
||||
fun foo(a: Int?) {
|
||||
a?.plus(1)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Replace with safe call" "true"
|
||||
fun foo(a: Int?) {
|
||||
a<caret>.plus(1)
|
||||
}
|
||||
Reference in New Issue
Block a user