use of com.siyeh.ig.psiutils.CommentTracker in project intellij-community by JetBrains.
the class TrivialFunctionalExpressionUsageInspection method replaceWithLambdaBody.
private static void replaceWithLambdaBody(PsiMethodCallExpression callExpression, PsiLambdaExpression element) {
PsiElement body = element.getBody();
PsiExpression expression = LambdaUtil.extractSingleExpressionFromBody(body);
if (expression != null) {
final CommentTracker ct = new CommentTracker();
inlineCallArguments(callExpression, element, ct);
ct.replaceAndRestoreComments(callExpression, ct.markUnchanged(expression));
} else if (body instanceof PsiCodeBlock) {
element = RefactoringUtil.ensureCodeBlock(element);
if (element == null)
return;
body = element.getBody();
if (!(body instanceof PsiCodeBlock))
return;
callExpression = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class);
if (callExpression == null)
return;
final CommentTracker ct = new CommentTracker();
inlineCallArguments(callExpression, element, ct);
final PsiElement parent = callExpression.getParent();
final PsiStatement[] statements = ((PsiCodeBlock) body).getStatements();
if (statements.length > 0) {
final PsiStatement anchor = PsiTreeUtil.getParentOfType(parent, PsiStatement.class, false);
PsiReturnStatement statement = ObjectUtils.tryCast(statements[statements.length - 1], PsiReturnStatement.class);
if (anchor != null) {
final PsiElement gParent = anchor.getParent();
for (PsiElement child : body.getChildren()) {
if (child != statement && !(child instanceof PsiJavaToken)) {
gParent.addBefore(ct.markUnchanged(child), anchor);
}
}
}
final PsiExpression returnValue = statement == null ? null : statement.getReturnValue();
if (returnValue != null) {
ct.replaceAndRestoreComments(callExpression, ct.markUnchanged(returnValue));
} else {
ct.deleteAndRestoreComments(callExpression);
}
}
}
}
use of com.siyeh.ig.psiutils.CommentTracker in project intellij-community by JetBrains.
the class InlineIncrementIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element) {
final PsiReferenceExpression operandExpression = IncrementUtil.getIncrementOrDecrementOperand(element);
final PsiExpressionStatement expressionStatement = ObjectUtils.tryCast(element.getParent(), PsiExpressionStatement.class);
final String operatorText = IncrementUtil.getOperatorText(element);
if (operandExpression != null && expressionStatement != null && operatorText != null) {
final PsiVariable variable = resolveSimpleVariableReference(operandExpression);
if (variable != null) {
final Occurrence occurrence = findSingleReadOccurrence(expressionStatement, variable);
if (occurrence != null) {
final Project project = expressionStatement.getProject();
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
final String text = occurrence.isPrevious ? occurrence.referenceExpression.getText() + operatorText : operatorText + occurrence.referenceExpression.getText();
PsiExpression incrementOrDecrement = factory.createExpressionFromText(text, expressionStatement);
incrementOrDecrement = (PsiExpression) occurrence.referenceExpression.replace(incrementOrDecrement);
final CodeStyleManager codeStyle = CodeStyleManager.getInstance(project);
codeStyle.reformat(incrementOrDecrement, true);
final CommentTracker ct = new CommentTracker();
ct.deleteAndRestoreComments(expressionStatement);
}
}
}
}
use of com.siyeh.ig.psiutils.CommentTracker in project intellij-community by JetBrains.
the class InlineStreamMapAction method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
PsiMethodCallExpression mapCall = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class);
if (mapCall == null)
return;
PsiMethodCallExpression nextCall = getNextExpressionToMerge(mapCall);
if (nextCall == null)
return;
PsiReferenceExpression nextRef = nextCall.getMethodExpression();
PsiExpression nextQualifier = nextRef.getQualifierExpression();
if (nextQualifier == null)
return;
String newName = translateName(mapCall, nextCall);
if (newName == null)
return;
PsiLambdaExpression previousLambda = getLambda(mapCall);
LOG.assertTrue(previousLambda != null);
PsiExpression previousBody = LambdaUtil.extractSingleExpressionFromBody(previousLambda.getBody());
LOG.assertTrue(previousBody != null);
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);
PsiParameter[] prevParameters = previousLambda.getParameterList().getParameters();
LOG.assertTrue(prevParameters.length == 1);
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
for (PsiReference ref : ReferencesSearch.search(nextParameters[0], new LocalSearchScope(body)).findAll()) {
PsiElement e = ref.getElement();
PsiExpression replacement = ct.markUnchanged(previousBody);
if (e.getParent() instanceof PsiExpression && ParenthesesUtils.areParenthesesNeeded(previousBody, (PsiExpression) e.getParent(), false)) {
replacement = factory.createExpressionFromText("(a)", e);
PsiExpression parenthesized = ((PsiParenthesizedExpression) replacement).getExpression();
LOG.assertTrue(parenthesized != null);
parenthesized.replace(previousBody);
}
ct.replace(e, replacement);
}
ct.replace(nextParameters[0], ct.markUnchanged(prevParameters[0]));
ExpressionUtils.bindReferenceTo(nextRef, newName);
PsiExpression prevQualifier = mapCall.getMethodExpression().getQualifierExpression();
if (prevQualifier == null) {
ct.deleteAndRestoreComments(nextQualifier);
} else {
ct.replaceAndRestoreComments(nextQualifier, ct.markUnchanged(prevQualifier));
}
CodeStyleManager.getInstance(project).reformat(lambda);
}
use of com.siyeh.ig.psiutils.CommentTracker in project intellij-community by JetBrains.
the class ComposeFunctionChainAction method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
PsiMethodCallExpression call = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class, false, PsiStatement.class, PsiLambdaExpression.class);
if (call == null)
return;
PsiElement outer = call.getParent().getParent();
if (!(outer instanceof PsiMethodCallExpression))
return;
PsiMethodCallExpression outerCall = (PsiMethodCallExpression) outer;
PsiMethod outerMethod = outerCall.resolveMethod();
if (outerMethod == null)
return;
PsiClass outerClass = outerMethod.getContainingClass();
if (outerClass == null)
return;
String outerClassName = outerClass.getQualifiedName();
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
PsiExpression outerQualifier = outerCall.getMethodExpression().getQualifierExpression();
CommentTracker ct = new CommentTracker();
String reference;
if (outerMethod.getName().equals("apply") && CommonClassNames.JAVA_UTIL_FUNCTION_FUNCTION.equals(outerClassName)) {
reference = outerQualifier == null ? "this" : ct.text(outerQualifier);
} else if (outerMethod.hasModifierProperty(PsiModifier.STATIC)) {
reference = outerClassName + "::" + outerMethod.getName();
} else {
reference = outerQualifier == null ? "this" : ct.text(outerQualifier) + "::" + outerMethod.getName();
}
String resultQualifier = qualifier != null ? ct.text(qualifier) + "." : "";
String replacement = resultQualifier + "andThen(" + reference + ").apply" + ct.text(call.getArgumentList());
PsiElement result = ct.replaceAndRestoreComments(outer, replacement);
result = CodeStyleManager.getInstance(project).reformat(result);
PsiElement applyElement = ((PsiMethodCallExpression) result).getMethodExpression().getReferenceNameElement();
if (applyElement != null) {
editor.getCaretModel().moveToOffset(applyElement.getTextOffset() + applyElement.getTextLength());
}
}
Aggregations