preserve comments when transform expr lambda to code block (IDEA-179643)

This commit is contained in:
Anna.Kozlova
2017-11-21 16:51:10 +01:00
parent 443fa52729
commit 8a020a1d46
3 changed files with 6 additions and 8 deletions
@@ -889,16 +889,13 @@ public class RefactoringUtil {
final PsiElement invalidBody = lambdaExpression.getBody();
if (invalidBody == null) return declaration;
final PsiLambdaExpression expressionFromText = (PsiLambdaExpression)elementFactory
.createExpressionFromText(lambdaExpression.getParameterList().getText() + " -> {}", lambdaExpression.getParent());
String lambdaParamListWithArrowAndComments = lambdaExpression.getText()
.substring(0, (declaration.isPhysical() ? declaration : invalidBody).getStartOffsetInParent());
final PsiLambdaExpression expressionFromText = (PsiLambdaExpression)elementFactory.createExpressionFromText(lambdaParamListWithArrowAndComments + "{}", lambdaExpression.getParent());
PsiCodeBlock newBody = (PsiCodeBlock)expressionFromText.getBody();
LOG.assertTrue(newBody != null);
newBody.add(declaration);
lambdaExpression =
(PsiLambdaExpression)lambdaExpression.replace(elementFactory.createExpressionFromText(
lambdaExpression.getParameterList().getText() + " -> " + invalidBody.getText(), lambdaExpression));
final PsiElement lambdaExpressionBody = lambdaExpression.getBody();
LOG.assertTrue(lambdaExpressionBody != null);
final PsiStatement lastBodyStatement;
@@ -8,7 +8,8 @@ class X {
public void foo(Class<?> cls)
{
Stream.of(cls.getMethods())
.filter(method -> {
.filter(method ->//my comment to keep
{
Class<?> c = method.getReturnType();
return Collection.class.isAssignableFrom(c) || Map.class.isAssignableFrom(c);
})
@@ -8,7 +8,7 @@ class X {
public void foo(Class<?> cls)
{
Stream.of(cls.getMethods())
.filter(method ->
.filter(method ->//my comment to keep
Collection.class.isAssignableFrom(<selection>method.getReturnType()</selection>) || Map.class.isAssignableFrom(method.getReturnType()))
.collect(Collectors.<Method>toList());
}