mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
IDEA-140331 Improve intention action for 'Method invocation may produce NPE'
This commit is contained in:
@@ -27,6 +27,7 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtilBase;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ipp.trivialif.MergeIfAndIntention;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -57,7 +58,7 @@ public class SurroundWithIfFix implements LocalQuickFix {
|
||||
PsiFile file = element.getContainingFile();
|
||||
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
|
||||
Document document = documentManager.getDocument(file);
|
||||
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
|
||||
if (document == null || !FileModificationService.getInstance().prepareFileForWrite(file)) return;
|
||||
PsiElement[] elements = {anchorStatement};
|
||||
PsiElement prev = PsiTreeUtil.skipSiblingsBackward(anchorStatement, PsiWhiteSpace.class);
|
||||
if (prev instanceof PsiComment && JavaSuppressionUtil.getSuppressedInspectionIdsIn(prev) != null) {
|
||||
@@ -71,6 +72,11 @@ public class SurroundWithIfFix implements LocalQuickFix {
|
||||
document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(),newText);
|
||||
|
||||
editor.getCaretModel().moveToOffset(textRange.getEndOffset() + newText.length());
|
||||
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
|
||||
new MergeIfAndIntention().invoke(project, editor, file);
|
||||
|
||||
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Surround with 'if (i != null)'" "true"
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
class A {
|
||||
void foo(@Nullable String i) {
|
||||
if (i != null && i.length() > 0) {
|
||||
if (i != "a") {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Surround with 'if (i != null)'" "true"
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
class A {
|
||||
void foo(@Nullable String i) {
|
||||
if (i.le<caret>ngth() > 0) {
|
||||
if (i != "a") {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.completion.SmartTypeCompletionDfaTest;
|
||||
import com.intellij.codeInsight.daemon.quickFix.AddAssertStatementFixTest;
|
||||
import com.intellij.codeInsight.daemon.quickFix.ReplaceFromOfNullableFixTest;
|
||||
import com.intellij.codeInsight.daemon.quickFix.ReplaceWithOfNullableFixTest;
|
||||
import com.intellij.codeInsight.daemon.quickFix.SurroundWithIfFixTest;
|
||||
import com.intellij.slicer.SliceBackwardTest;
|
||||
import com.intellij.slicer.SliceTreeTest;
|
||||
import junit.framework.Test;
|
||||
@@ -49,6 +50,7 @@ public class DataFlowInspectionTestSuite {
|
||||
suite.addTestSuite(NullableStuffInspection14Test.class);
|
||||
|
||||
suite.addTestSuite(AddAssertStatementFixTest.class);
|
||||
suite.addTestSuite(SurroundWithIfFixTest.class);
|
||||
suite.addTestSuite(ReplaceWithOfNullableFixTest.class);
|
||||
suite.addTestSuite(ReplaceFromOfNullableFixTest.class);
|
||||
return suite;
|
||||
|
||||
Reference in New Issue
Block a user