From 0100d6fab59899556fd393d82718209a9270f908 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Thu, 14 Apr 2016 14:28:43 +0200 Subject: [PATCH] allow variable introduction inside lambda chain with expression -> block lambda conversion --- .../introduceVariable/IntroduceVariableBase.java | 2 +- .../introduceVariable/PutInNestedLambdaBody.after.java | 10 ++++++++++ .../introduceVariable/PutInNestedLambdaBody.java | 7 +++++++ .../intellij/refactoring/IntroduceVariableTest.java | 4 ++++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/refactoring/introduceVariable/PutInNestedLambdaBody.after.java create mode 100644 java/java-tests/testData/refactoring/introduceVariable/PutInNestedLambdaBody.java diff --git a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java index 09aa58c9e48e..2fb56af33023 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java @@ -597,7 +597,7 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { final PsiElement tempContainer = anchorStatement.getParent(); - if (!(tempContainer instanceof PsiCodeBlock) && !RefactoringUtil.isLoopOrIf(tempContainer) && (tempContainer.getParent() instanceof PsiLambdaExpression)) { + if (!(tempContainer instanceof PsiCodeBlock) && !RefactoringUtil.isLoopOrIf(tempContainer) && !(tempContainer instanceof PsiLambdaExpression) && (tempContainer.getParent() instanceof PsiLambdaExpression)) { String message = RefactoringBundle.message("refactoring.is.not.supported.in.the.current.context", REFACTORING_NAME); showErrorMessage(project, editor, message); return false; diff --git a/java/java-tests/testData/refactoring/introduceVariable/PutInNestedLambdaBody.after.java b/java/java-tests/testData/refactoring/introduceVariable/PutInNestedLambdaBody.after.java new file mode 100644 index 000000000000..1e25b7b2ce00 --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceVariable/PutInNestedLambdaBody.after.java @@ -0,0 +1,10 @@ +import java.util.function.Function; +class Test { + + Function f = (str) -> () -> { + String s = ""; + foo(s); + }; + + void foo(String s) {} +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/introduceVariable/PutInNestedLambdaBody.java b/java/java-tests/testData/refactoring/introduceVariable/PutInNestedLambdaBody.java new file mode 100644 index 000000000000..c5d32b89c053 --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceVariable/PutInNestedLambdaBody.java @@ -0,0 +1,7 @@ +import java.util.function.Function; +class Test { + + Function f = (str) -> () -> foo(""); + + void foo(String s) {} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java index 24eb5804792b..d1d0aaafc912 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java @@ -492,6 +492,10 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase { doTest(new MockIntroduceVariableHandler("s", false, false, false, "java.lang.String")); } + public void testPutInNestedLambdaBody() { + doTest(new MockIntroduceVariableHandler("s", false, false, false, "java.lang.String")); + } + public void testNormalizeDeclarations() { doTest(new MockIntroduceVariableHandler("i3", false, false, false, "int")); }