From ecc2934b9c90491b00de00b33e335ded9caae2b2 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 4 Apr 2016 15:06:00 +0200 Subject: [PATCH] return lambda folding in some cases where anonymous->lambda conversion is anyway impossible --- .../folding/impl/JavaFoldingBuilderBase.java | 4 +- .../folding/JavaFolding8Test.groovy | 86 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/folding/JavaFolding8Test.groovy diff --git a/java/java-psi-impl/src/com/intellij/codeInsight/folding/impl/JavaFoldingBuilderBase.java b/java/java-psi-impl/src/com/intellij/codeInsight/folding/impl/JavaFoldingBuilderBase.java index 802489dc09c3..1513b71ee311 100644 --- a/java/java-psi-impl/src/com/intellij/codeInsight/folding/impl/JavaFoldingBuilderBase.java +++ b/java/java-psi-impl/src/com/intellij/codeInsight/folding/impl/JavaFoldingBuilderBase.java @@ -807,7 +807,7 @@ public abstract class JavaFoldingBuilderBase extends CustomFoldingBuilder implem if (argumentList != null && argumentList.getExpressions().length == 0) { final PsiMethod[] methods = anonymousClass.getMethods(); PsiClass baseClass = anonymousClass.getBaseClassType().resolve(); - if (hasOnlyOneLambdaMethod(anonymousClass, !quick) && seemsLikeLambda(baseClass) && !PsiUtil.isLanguageLevel8OrHigher(anonymousClass)) { + if (hasOnlyOneLambdaMethod(anonymousClass, !quick) && seemsLikeLambda(baseClass)) { final PsiMethod method = methods[0]; final PsiCodeBlock body = method.getBody(); if (body != null) { @@ -841,6 +841,8 @@ public abstract class JavaFoldingBuilderBase extends CustomFoldingBuilder implem String type = quick ? "" : getOptionalLambdaType(anonymousClass, expression); String methodName = quick || !isImplementingLambdaMethod(baseClass) ? method.getName() : ""; + if (StringUtil.isEmpty(methodName) && PsiUtil.isLanguageLevel8OrHigher(anonymousClass)) return false; + final String params = StringUtil.join(method.getParameterList().getParameters(), new Function() { @Override public String fun(final PsiParameter psiParameter) { diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/folding/JavaFolding8Test.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/folding/JavaFolding8Test.groovy new file mode 100644 index 000000000000..0e99cadb58c7 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/folding/JavaFolding8Test.groovy @@ -0,0 +1,86 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.folding + +import com.intellij.codeInsight.folding.impl.CodeFoldingManagerImpl +import com.intellij.codeInsight.folding.impl.JavaCodeFoldingSettingsImpl +import com.intellij.codeInsight.folding.impl.JavaFoldingBuilder +import com.intellij.openapi.editor.ex.FoldingModelEx +import com.intellij.openapi.editor.impl.FoldingModelImpl +import com.intellij.testFramework.LightProjectDescriptor +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase +/** + * @author peter + */ +public class JavaFolding8Test extends LightCodeInsightFixtureTestCase { + + def JavaCodeFoldingSettingsImpl myFoldingSettings + def JavaCodeFoldingSettingsImpl myFoldingStateToRestore + + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return JAVA_8 + } + + @Override + public void setUp() { + super.setUp() + myFoldingSettings = JavaCodeFoldingSettings.instance as JavaCodeFoldingSettingsImpl + myFoldingStateToRestore = new JavaCodeFoldingSettingsImpl() + myFoldingStateToRestore.loadState(myFoldingSettings) + } + + @Override + protected void tearDown() { + myFoldingSettings.loadState(myFoldingStateToRestore) + super.tearDown() + } + + public void "test no plain lambda folding where anonymous class can be real lambda but fold otherwise"() { + myFixture.addClass('interface Runnable2 { void run(); }') + myFixture.addClass('abstract class MyAction { public void run(); public void update() {} }') + def text = """\ +class Test { + void test() { + Runnable r = new Runnable2() { + public void run() { + System.out.println(); + } + }; + MyAction action = new MyAction() { + public void run() { + System.out.println(); + } + } + } +} +""" + configure text + def foldingModel = myFixture.editor.foldingModel as FoldingModelImpl + + assert foldingModel.getCollapsedRegionAtOffset(text.indexOf("MyAction(")).placeholderText == 'run() ' + JavaFoldingBuilder.rightArrow + ' { ' + assert !foldingModel.getCollapsedRegionAtOffset(text.indexOf("Runnable2(")) + } + + private def configure(String text) { + myFixture.configureByText("a.java", text) + CodeFoldingManagerImpl.getInstance(getProject()).buildInitialFoldings(myFixture.editor); + def foldingModel = myFixture.editor.foldingModel as FoldingModelEx + foldingModel.rebuild() + myFixture.doHighlighting() + } + +}