use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class ForToEachIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrForStatement parentStatement = (GrForStatement) element;
final GrForInClause clause = (GrForInClause) parentStatement.getClause();
final GrVariable var = clause.getDeclaredVariable();
final GrStatement body = parentStatement.getBody();
final String bodyText;
if (body instanceof GrBlockStatement) {
final String text = body.getText();
bodyText = text.substring(1, text.length() - 1);
} else {
bodyText = body.getText();
}
GrExpression collection = clause.getIteratedExpression();
assert collection != null;
@NonNls final String statement = "x.each{" + var.getText() + " -> " + bodyText + " }";
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentStatement.getProject());
final GrMethodCallExpression eachExpression = (GrMethodCallExpression) factory.createTopElementFromText(statement);
((GrReferenceExpression) eachExpression.getInvokedExpression()).getQualifierExpression().replaceWithExpression(collection, true);
parentStatement.replaceWithStatement(eachExpression);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class MakeClosureCallExplicitIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrMethodCallExpression expression = (GrMethodCallExpression) element;
final GrExpression invokedExpression = expression.getInvokedExpression();
final GrArgumentList argList = expression.getArgumentList();
final GrClosableBlock[] closureArgs = expression.getClosureArguments();
final StringBuilder newExpression = new StringBuilder();
newExpression.append(invokedExpression.getText());
newExpression.append(".call");
newExpression.append(argList.getText());
for (GrClosableBlock closureArg : closureArgs) {
newExpression.append(closureArg.getText());
}
PsiImplUtil.replaceExpression(newExpression.toString(), expression);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class GroovyIntroduceParameterUtil method processChangedMethodCall.
public static void processChangedMethodCall(PsiElement element, GrIntroduceParameterSettings settings, Project project) {
if (!(element.getParent() instanceof GrMethodCallExpression)) {
LOG.error(element.getParent());
return;
}
GrMethodCallExpression methodCall = (GrMethodCallExpression) element.getParent();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final String name = settings.getName();
LOG.assertTrue(name != null);
GrExpression expression = factory.createExpressionFromText(name, null);
final GrArgumentList argList = methodCall.getArgumentList();
final PsiElement[] exprs = argList.getAllArguments();
if (exprs.length > 0) {
argList.addAfter(expression, exprs[exprs.length - 1]);
} else {
argList.add(expression);
}
removeParametersFromCall(methodCall, settings);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class GroovyIntroduceParameterMethodUsagesProcessor method processChangeMethodUsage.
@Override
public boolean processChangeMethodUsage(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(usage.getElement());
if (callExpression == null)
return true;
GrArgumentList argList = callExpression.getArgumentList();
GrExpression[] oldArgs = argList.getExpressionArguments();
final GrExpression anchor;
if (!data.getMethodToSearchFor().isVarArgs()) {
anchor = getLast(oldArgs);
} else {
final PsiParameter[] parameters = data.getMethodToSearchFor().getParameterList().getParameters();
if (parameters.length > oldArgs.length) {
anchor = getLast(oldArgs);
} else {
final int lastNonVararg = parameters.length - 2;
anchor = lastNonVararg >= 0 ? oldArgs[lastNonVararg] : null;
}
}
PsiMethod method = PsiTreeUtil.getParentOfType(argList, PsiMethod.class);
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
if (signature == null)
signature = GrClosureSignatureUtil.createSignature(data.getMethodToSearchFor(), PsiSubstitutor.EMPTY);
final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, false, true);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(data.getProject());
if (method != null && IntroduceParameterUtil.isMethodInUsages(data, method, usages)) {
argList.addAfter(factory.createExpressionFromText(data.getParameterName()), anchor);
} else {
final PsiElement _expr = data.getParameterInitializer().getExpression();
PsiElement initializer = ExpressionConverter.getExpression(_expr, GroovyLanguage.INSTANCE, data.getProject());
LOG.assertTrue(initializer instanceof GrExpression);
GrExpression newArg = GroovyIntroduceParameterUtil.addClosureToCall(initializer, argList);
if (newArg == null) {
final PsiElement dummy = argList.addAfter(factory.createExpressionFromText("1"), anchor);
newArg = ((GrExpression) dummy).replaceWithExpression((GrExpression) initializer, true);
}
final PsiMethod methodToReplaceIn = data.getMethodToReplaceIn();
new OldReferencesResolver(callExpression, newArg, methodToReplaceIn, data.getReplaceFieldsWithGetters(), initializer, signature, actualArgs, methodToReplaceIn.getParameterList().getParameters()).resolve();
ChangeContextUtil.clearContextInfo(initializer);
//newArg can be replaced by OldReferenceResolver
if (newArg.isValid()) {
JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
CodeStyleManager.getInstance(data.getProject()).reformat(newArg);
}
}
if (actualArgs == null) {
removeParamsFromUnresolvedCall(callExpression, data);
} else {
removeParametersFromCall(actualArgs, data.getParametersToRemove());
}
if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
argList.replace(emptyArgList);
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class GroovySimpleManyStatementsSurrounder method getSurroundSelectionRange.
@Override
protected final TextRange getSurroundSelectionRange(GroovyPsiElement element) {
assert element instanceof GrMethodCallExpression;
GrMethodCallExpression withCall = (GrMethodCallExpression) element;
GrCondition condition = withCall.getExpressionArguments()[0];
int endOffset = condition.getTextRange().getStartOffset();
condition.getParent().getNode().removeChild(condition.getNode());
return new TextRange(endOffset, endOffset);
}
Aggregations