mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
unwrap lambda (IDEA-151266)
This commit is contained in:
@@ -51,13 +51,7 @@ public class JavaAnonymousUnwrapper extends JavaUnwrapper {
|
||||
final PsiStatement[] statements = body.getStatements();
|
||||
if (statements.length == 1 && statements[0] instanceof PsiReturnStatement) {
|
||||
final PsiExpression returnValue = ((PsiReturnStatement)statements[0]).getReturnValue();
|
||||
if (from instanceof PsiDeclarationStatement) {
|
||||
final PsiElement[] declaredElements = ((PsiDeclarationStatement)from).getDeclaredElements();
|
||||
if (declaredElements.length == 1 && declaredElements[0] instanceof PsiVariable) {
|
||||
context.setInitializer((PsiVariable)declaredElements[0], returnValue);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (toAssignment(context, from, returnValue)) return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,7 +67,18 @@ public class JavaAnonymousUnwrapper extends JavaUnwrapper {
|
||||
context.deleteExactly(from);
|
||||
}
|
||||
|
||||
private static PsiElement findElementToExtractFrom(PsiElement el) {
|
||||
public static boolean toAssignment(Context context, PsiElement from, PsiExpression returnValue) {
|
||||
if (from instanceof PsiDeclarationStatement) {
|
||||
final PsiElement[] declaredElements = ((PsiDeclarationStatement)from).getDeclaredElements();
|
||||
if (declaredElements.length == 1 && declaredElements[0] instanceof PsiVariable) {
|
||||
context.setInitializer((PsiVariable)declaredElements[0], returnValue);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static PsiElement findElementToExtractFrom(PsiElement el) {
|
||||
if (el.getParent() instanceof PsiNewExpression) el = el.getParent();
|
||||
el = findTopmostParentOfType(el, PsiMethodCallExpression.class);
|
||||
el = findTopmostParentOfType(el, PsiAssignmentExpression.class);
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.unwrap;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightBundle;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class JavaLambdaUnwrapper extends JavaUnwrapper {
|
||||
public JavaLambdaUnwrapper() {
|
||||
super(CodeInsightBundle.message("unwrap.lambda"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicableTo(PsiElement e) {
|
||||
return e instanceof PsiLambdaExpression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiElement collectAffectedElements(PsiElement e, List<PsiElement> toExtract) {
|
||||
super.collectAffectedElements(e, toExtract);
|
||||
return JavaAnonymousUnwrapper.findElementToExtractFrom(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
|
||||
PsiElement from = JavaAnonymousUnwrapper.findElementToExtractFrom(element);
|
||||
PsiLambdaExpression lambdaExpression = (PsiLambdaExpression)element;
|
||||
PsiElement body = lambdaExpression.getBody();
|
||||
if (body instanceof PsiExpression || body instanceof PsiCodeBlock && ((PsiCodeBlock)body).getStatements().length == 1) {
|
||||
List<PsiExpression> returnExpressions = LambdaUtil.getReturnExpressions(lambdaExpression);
|
||||
if (returnExpressions.size() == 1
|
||||
&& !PsiType.VOID.equals(returnExpressions.get(0).getType())
|
||||
&& JavaAnonymousUnwrapper.toAssignment(context, from, returnExpressions.get(0))) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (body instanceof PsiCodeBlock) {
|
||||
context.extractFromCodeBlock((PsiCodeBlock)body, from);
|
||||
}
|
||||
else {
|
||||
context.extractElement(body, from);
|
||||
if (context.myIsEffective) {
|
||||
PsiStatement emptyStatement = JavaPsiFacade.getElementFactory(from.getProject()).createStatementFromText(";", from);
|
||||
from.getParent().addBefore(emptyStatement, from);
|
||||
}
|
||||
}
|
||||
context.deleteExactly(from);
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ public class JavaUnwrapDescriptor extends UnwrapDescriptorBase {
|
||||
new JavaCatchRemover(),
|
||||
new JavaSynchronizedUnwrapper(),
|
||||
new JavaAnonymousUnwrapper(),
|
||||
new JavaLambdaUnwrapper(),
|
||||
new JavaConditionalUnwrapper(),
|
||||
new JavaPolyadicExpressionUnwrapper()
|
||||
};
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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.unwrap;
|
||||
|
||||
public class UnwrapLambdaTest extends UnwrapTestCase {
|
||||
public void testUnwrap() throws Exception {
|
||||
assertUnwrapped("{\n" +
|
||||
" Runnable r = () -> {\n" +
|
||||
" Sys<caret>tem.gc();\n" +
|
||||
" }\n" +
|
||||
"}\n",
|
||||
|
||||
"{\n" +
|
||||
" Sys<caret>tem.gc();\n" +
|
||||
"}\n");
|
||||
}
|
||||
|
||||
public void testUnwrapExpressionDeclaration() throws Exception {
|
||||
assertUnwrapped("{\n" +
|
||||
" interface I {int get();}" +
|
||||
" I i = () -> <caret>1;\n" +
|
||||
"}\n",
|
||||
|
||||
"{\n" +
|
||||
" interface I {int get();}" +
|
||||
" I i = 1;\n" +
|
||||
"}\n");
|
||||
}
|
||||
|
||||
public void testUnwrapBlockDeclaration() throws Exception {
|
||||
assertUnwrapped("{\n" +
|
||||
" interface I {int get();}" +
|
||||
" I i = () -> { return <caret>1;};\n" +
|
||||
"}\n",
|
||||
|
||||
"{\n" +
|
||||
" interface I {int get();}" +
|
||||
" I i = 1;\n" +
|
||||
"}\n");
|
||||
}
|
||||
|
||||
public void testUnwrapUnresolved() throws Exception {
|
||||
assertUnwrapped("{\n" +
|
||||
" () -> <caret>null;\n" +
|
||||
"}\n",
|
||||
|
||||
"{\n" +
|
||||
" null;\n" +
|
||||
"}\n");
|
||||
}
|
||||
}
|
||||
@@ -102,6 +102,7 @@ unwrap.array.initializer=Unwrap array initializer
|
||||
unwrap.synchronized=Unwrap 'synchronized...'
|
||||
unwrap.with.placeholder=Unwrap ''{0}''
|
||||
unwrap.anonymous=Unwrap 'anonymous...'
|
||||
unwrap.lambda=Unwrap 'lambda...'
|
||||
generate.equals.hashcode.wizard.title=Generate equals() and hashCode()
|
||||
generate.equals.hashcode.equals.fields.chooser.title=Choose &fields to be included in equals()
|
||||
generate.equals.hashcode.hashcode.fields.chooser.title=Choose &fields to be included in hashCode()
|
||||
|
||||
Reference in New Issue
Block a user