InlineStreamMapAction: restore comments

This commit is contained in:
Tagir Valeev
2016-12-06 12:03:15 +07:00
parent d7cb4edc8d
commit f3d41d8a3c
5 changed files with 22 additions and 13 deletions

View File

@@ -29,6 +29,7 @@ import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.util.LambdaRefactoringUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Contract;
@@ -199,7 +200,8 @@ public class InlineStreamMapAction extends PsiElementBaseIntentionAction {
PsiMethodCallExpression nextCall = getNextExpressionToMerge(mapCall);
if(nextCall == null) return;
PsiExpression nextQualifier = nextCall.getMethodExpression().getQualifierExpression();
PsiReferenceExpression nextRef = nextCall.getMethodExpression();
PsiExpression nextQualifier = nextRef.getQualifierExpression();
if(nextQualifier == null) return;
String newName = translateName(mapCall, nextCall);
@@ -214,11 +216,14 @@ public class InlineStreamMapAction extends PsiElementBaseIntentionAction {
PsiLambdaExpression lambda = getLambda(nextCall);
LOG.assertTrue(lambda != null);
CommentTracker ct = new CommentTracker();
if(!lambda.isPhysical()) {
lambda = (PsiLambdaExpression)nextCall.getArgumentList().add(lambda);
}
PsiElement body = lambda.getBody();
LOG.assertTrue(body != null);
ct.markUnchanged(body);
PsiParameter[] nextParameters = lambda.getParameterList().getParameters();
LOG.assertTrue(nextParameters.length == 1);
@@ -227,7 +232,7 @@ public class InlineStreamMapAction extends PsiElementBaseIntentionAction {
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
for(PsiReference ref : ReferencesSearch.search(nextParameters[0], new LocalSearchScope(body)).findAll()) {
PsiElement e = ref.getElement();
PsiExpression replacement = previousBody;
PsiExpression replacement = ct.markUnchanged(previousBody);
if (e.getParent() instanceof PsiExpression &&
ParenthesesUtils.areParenthesesNeeded(previousBody, (PsiExpression)e.getParent(), false)) {
replacement = factory.createExpressionFromText("(a)", e);
@@ -235,18 +240,17 @@ public class InlineStreamMapAction extends PsiElementBaseIntentionAction {
LOG.assertTrue(parenthesized != null);
parenthesized.replace(previousBody);
}
e.replace(replacement);
ct.replace(e, replacement);
}
nextParameters[0].replace(prevParameters[0]);
PsiElement nameElement = nextCall.getMethodExpression().getReferenceNameElement();
if(nameElement != null && !nameElement.getText().equals(newName)) {
nameElement.replace(factory.createIdentifier(newName));
ct.replace(nextParameters[0], ct.markUnchanged(prevParameters[0]));
if(!newName.equals(nextRef.getReferenceName())) {
nextRef.handleElementRename(newName);
}
PsiExpression prevQualifier = mapCall.getMethodExpression().getQualifierExpression();
if(prevQualifier == null) {
nextQualifier.delete();
ct.deleteAndRestoreComments(nextQualifier);
} else {
nextQualifier.replace(prevQualifier);
ct.replaceAndRestoreComments(nextQualifier, ct.markUnchanged(prevQualifier));
}
CodeStyleManager.getInstance(project).reformat(lambda);
}

View File

@@ -3,6 +3,9 @@ import java.util.List;
public class Main {
public static void test(List<CharSequence> list) {
list.stream().map(cs -> cs.subSequence(1, 5).length()).forEach(System.out::println);
/*before dot*/
/*after dot*/
list.stream()
/*before dot2*/./*after dot2*/map(cs -> /*length!!!*/ cs.subSequence(/*subsequence*/1, 5).length()).forEach(System.out::println);
}
}

View File

@@ -3,6 +3,7 @@ import java.util.List;
public class Main {
public static void test(List<CharSequence> list) {
list.stream().map(cs -> cs.subSequence(1, 5).length()).forEach(System.out::println);
/*out of body*/
list.stream().map(cs -> cs/*in body*/.subSequence(1, 5).length()).forEach(System.out::println);
}
}

View File

@@ -3,6 +3,7 @@ import java.util.List;
public class Main {
public static void test(List<CharSequence> list) {
list.stream().m<caret>ap(cs -> cs.subSequence(1, 5)).map(cs -> cs.length()).forEach(System.out::println);
list.stream()/*before dot*/./*after dot*/m<caret>ap(cs -> cs.subSequence(/*subsequence*/1, 5))
/*before dot2*/./*after dot2*/map(cs -> /*length!!!*/ cs.length()).forEach(System.out::println);
}
}

View File

@@ -4,7 +4,7 @@ import java.util.List;
public class Main {
public static void test(List<CharSequence> list) {
list.stream().m<caret>ap(cs -> {
return cs.subSequence(1, 5);
/*out of body*/ return cs/*in body*/.subSequence(1, 5);
}).map(cs -> cs.length()).forEach(System.out::println);
}
}