fix unwrap lambda for nested expression lambdas (IDEA-163987)
authorAnna.Kozlova <anna.kozlova@jetbrains.com>
Tue, 15 Nov 2016 16:03:15 +0000 (17:03 +0100)
committerAnna.Kozlova <anna.kozlova@jetbrains.com>
Tue, 15 Nov 2016 17:11:31 +0000 (18:11 +0100)
java/java-impl/src/com/intellij/codeInsight/unwrap/JavaAnonymousUnwrapper.java
java/java-impl/src/com/intellij/codeInsight/unwrap/JavaLambdaUnwrapper.java
java/java-tests/testSrc/com/intellij/codeInsight/unwrap/UnwrapLambdaTest.java

index 4e0094267fa83574100d063d7d8c4a2b782bf358..01d636a51306bef481c2cb40b0d259313351cb5d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2009 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -96,7 +96,7 @@ public class JavaAnonymousUnwrapper extends JavaUnwrapper {
   private static PsiElement findTopmostParentOfType(PsiElement el, Class<? extends PsiElement> clazz) {
     while (true) {
       @SuppressWarnings({"unchecked"})
-      PsiElement temp = PsiTreeUtil.getParentOfType(el, clazz, true, PsiAnonymousClass.class);
+      PsiElement temp = PsiTreeUtil.getParentOfType(el, clazz, true, PsiAnonymousClass.class, PsiLambdaExpression.class);
       if (temp == null || temp instanceof PsiFile) return el;
       el = temp;
     }
index 65007a462b43542eaa896feaa31e7deca92677ba..d2737cc1d2175a17ee5b7595347776da446b97eb 100644 (file)
@@ -56,7 +56,7 @@ public class JavaLambdaUnwrapper extends JavaUnwrapper {
     }
     else {
       context.extractElement(body, from);
-      if (context.myIsEffective) {
+      if (context.myIsEffective && !(from.getParent() instanceof PsiLambdaExpression)) {
         PsiStatement emptyStatement = JavaPsiFacade.getElementFactory(from.getProject()).createStatementFromText(";", from);
         from.getParent().addBefore(emptyStatement, from);
       }
index c23b3ae9e17ae6176602fb95d1d18c6232765117..91564e531d554a8bf95109820904118403134fcf 100644 (file)
@@ -27,6 +27,16 @@ public class UnwrapLambdaTest extends UnwrapTestCase {
                     "    Sys<caret>tem.gc();\n" +
                     "}\n");
   }
+
+  public void testUnwrapNestedLambda() throws Exception {
+    assertUnwrapped("{\n" +
+                    "    bar(() -> bar(() -> Sys<caret>tem.gc()));\n" +
+                    "}\n",
+
+                    "{\n" +
+                    "    bar(() -> Sys<caret>tem.gc());\n" +
+                    "}\n", 1);
+  }
   
   public void testUnwrapExpressionDeclaration() throws Exception {
     assertUnwrapped("{\n" +
@@ -61,4 +71,14 @@ public class UnwrapLambdaTest extends UnwrapTestCase {
                     "    null;\n" +
                     "}\n");
   }
+
+  @Override
+  protected String createCode(String codeBefore) {
+    return "public class A {\n" +
+           "    void foo() {\n" +
+           indentTwice(codeBefore) +
+           "    }\n" +
+           "    void bar(Runnable r){}\n" +
+           "}";
+  }
 }
\ No newline at end of file