diff --git a/java/java-impl/src/com/intellij/codeInspection/SurroundWithIfFix.java b/java/java-impl/src/com/intellij/codeInspection/SurroundWithIfFix.java index 9afbb9de848b..ed0901a19601 100644 --- a/java/java-impl/src/com/intellij/codeInspection/SurroundWithIfFix.java +++ b/java/java-impl/src/com/intellij/codeInspection/SurroundWithIfFix.java @@ -26,6 +26,7 @@ import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtilBase; +import com.intellij.refactoring.util.RefactoringUtil; import com.intellij.util.IncorrectOperationException; import com.siyeh.ipp.trivialif.MergeIfAndIntention; import org.jetbrains.annotations.NonNls; @@ -51,11 +52,16 @@ public class SurroundWithIfFix implements LocalQuickFix { @Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); - PsiStatement anchorStatement = PsiTreeUtil.getParentOfType(element, PsiStatement.class); + PsiElement anchorStatement = RefactoringUtil.getParentStatement(element, false); LOG.assertTrue(anchorStatement != null); - Editor editor = PsiUtilBase.findEditor(element); + if (anchorStatement.getParent() instanceof PsiLambdaExpression) { + final PsiElement body = ((PsiLambdaExpression)RefactoringUtil.expandExpressionLambdaToCodeBlock(anchorStatement)).getBody(); + LOG.assertTrue(body instanceof PsiCodeBlock); + anchorStatement = ((PsiCodeBlock)body).getStatements()[0]; + } + Editor editor = PsiUtilBase.findEditor(anchorStatement); if (editor == null) return; - PsiFile file = element.getContainingFile(); + PsiFile file = anchorStatement.getContainingFile(); PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); Document document = documentManager.getDocument(file); if (document == null || !FileModificationService.getInstance().prepareFileForWrite(file)) return; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithIf/afterInsideLambda.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithIf/afterInsideLambda.java new file mode 100644 index 000000000000..31d92ba2228d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithIf/afterInsideLambda.java @@ -0,0 +1,12 @@ +// "Surround with 'if (i != null)'" "true" +import org.jetbrains.annotations.Nullable; + +class A { + void foo(@Nullable String i) { + Runnable r = () -> { + if (i != null) { + i.length(); + } + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithIf/beforeInsideLambda.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithIf/beforeInsideLambda.java new file mode 100644 index 000000000000..f42f3cfeefc6 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithIf/beforeInsideLambda.java @@ -0,0 +1,8 @@ +// "Surround with 'if (i != null)'" "true" +import org.jetbrains.annotations.Nullable; + +class A { + void foo(@Nullable String i) { + Runnable r = () -> i.length(); + } +} \ No newline at end of file