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 84bc10e52484..385fe8b578ee 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java @@ -319,7 +319,8 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { if (endOffset <= startOffset) return null; PsiElement elementAt = PsiTreeUtil.findCommonParent(elementAtStart, elementAtEnd); - if (PsiTreeUtil.getParentOfType(elementAt, PsiExpression.class, false) == null) { + final PsiExpression containingExpression = PsiTreeUtil.getParentOfType(elementAt, PsiExpression.class, false); + if (containingExpression == null || containingExpression instanceof PsiLambdaExpression) { if (injectedLanguageManager.isInjectedFragment(file)) { return getSelectionFromInjectedHost(project, file, injectedLanguageManager, startOffset, endOffset); } diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/LocalClasses.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/LocalClasses.java new file mode 100644 index 000000000000..079d6f7cc30b --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/LocalClasses.java @@ -0,0 +1,8 @@ +interface I { + String apply(String s); +} +class Test { + { + I f = s -> s; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/NoParenthesisSurrounderForLambdaParameter.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/NoParenthesisSurrounderForLambdaParameter.java new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java index 5d023b45137c..a2a9ac9c59f9 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java @@ -194,6 +194,16 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase { doTest(new JavaWithBlockSurrounder()); } + public void testNoParenthesisSurrounderForLambdaParameter() throws Exception { + configureByFile(BASE_PATH + getTestName(false) + ".java"); + + SurroundDescriptor item = ContainerUtil.getFirstItem(LanguageSurrounders.INSTANCE.allForLanguage(JavaLanguage.INSTANCE)); + assertNotNull(item); + SelectionModel selectionModel = getEditor().getSelectionModel(); + PsiElement[] elements = item.getElementsToSurround(getFile(), selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); + assertEmpty(elements); + } + private void doTest(Surrounder surrounder) { doTest(getTestName(false), surrounder); }