Fix element removing - the previous one produced invalid postfix expression.
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.plugin.quickfix;
|
package org.jetbrains.jet.plugin.quickfix;
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||||
import com.intellij.codeInsight.intention.IntentionAction;
|
import com.intellij.codeInsight.intention.IntentionAction;
|
||||||
import com.intellij.openapi.editor.Editor;
|
import com.intellij.openapi.editor.Editor;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
@@ -24,13 +25,14 @@ import com.intellij.psi.PsiFile;
|
|||||||
import com.intellij.psi.impl.source.tree.LeafPsiElement;
|
import com.intellij.psi.impl.source.tree.LeafPsiElement;
|
||||||
import com.intellij.util.IncorrectOperationException;
|
import com.intellij.util.IncorrectOperationException;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
import org.jetbrains.jet.lexer.JetTokens;
|
import org.jetbrains.jet.lexer.JetTokens;
|
||||||
import org.jetbrains.jet.plugin.JetBundle;
|
import org.jetbrains.jet.plugin.JetBundle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author slukjanov aka Frostman
|
* @author slukjanov aka Frostman
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("IntentionDescriptionNotFoundInspection")
|
||||||
public class UnnecessaryNotNullAssertionFix implements IntentionAction {
|
public class UnnecessaryNotNullAssertionFix implements IntentionAction {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
@@ -46,15 +48,27 @@ public class UnnecessaryNotNullAssertionFix implements IntentionAction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||||
return file instanceof JetFile && getExclExclElement(editor, file) != null;
|
if (file instanceof JetFile) {
|
||||||
|
JetPostfixExpression postfixExpression = getExclExclPostfixExpression(editor, file);
|
||||||
|
return (postfixExpression != null && postfixExpression.getParent() != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||||
final PsiElement exclExcl = getExclExclElement(editor, file);
|
if (!CodeInsightUtilBase.prepareFileForWrite(file)) {
|
||||||
assert exclExcl != null;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
exclExcl.delete();
|
final JetPostfixExpression postfixExpression = getExclExclPostfixExpression(editor, file);
|
||||||
|
|
||||||
|
PsiElement parent = postfixExpression.getParent();
|
||||||
|
if (parent != null) {
|
||||||
|
JetExpression expression = JetPsiFactory.createExpression(project, postfixExpression.getBaseExpression().getText());
|
||||||
|
postfixExpression.replace(expression);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -70,4 +84,19 @@ public class UnnecessaryNotNullAssertionFix implements IntentionAction {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static JetPostfixExpression getExclExclPostfixExpression(Editor editor, PsiFile file) {
|
||||||
|
PsiElement exclExclElement = getExclExclElement(editor, file);
|
||||||
|
if (exclExclElement != null) {
|
||||||
|
PsiElement parent = exclExclElement.getParent();
|
||||||
|
if (parent instanceof JetSimpleNameExpression) {
|
||||||
|
PsiElement operationParent = parent.getParent();
|
||||||
|
if (operationParent instanceof JetPostfixExpression) {
|
||||||
|
return (JetPostfixExpression) operationParent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user