use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class GroovyRefactoringUtil method verifySafeCopyExpressionSubElement.
private static int verifySafeCopyExpressionSubElement(PsiElement element) {
int result = RefactoringUtil.EXPR_COPY_SAFE;
if (element == null)
return result;
if (element instanceof GrNamedElement) {
return RefactoringUtil.EXPR_COPY_SAFE;
}
if (element instanceof GrMethodCallExpression) {
result = RefactoringUtil.EXPR_COPY_UNSAFE;
}
if (element instanceof GrNewExpression) {
return RefactoringUtil.EXPR_COPY_PROHIBITED;
}
if (element instanceof GrAssignmentExpression) {
return RefactoringUtil.EXPR_COPY_PROHIBITED;
}
if (element instanceof GrClosableBlock) {
return RefactoringUtil.EXPR_COPY_PROHIBITED;
}
if (isPlusPlusOrMinusMinus(element)) {
return RefactoringUtil.EXPR_COPY_PROHIBITED;
}
PsiElement[] children = element.getChildren();
for (PsiElement child : children) {
int childResult = verifySafeCopyExpressionSubElement(child);
result = Math.max(result, childResult);
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class EachToForIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrMethodCallExpression expression = (GrMethodCallExpression) element;
final GrClosableBlock block = expression.getClosureArguments()[0];
final GrParameterList parameterList = block.getParameterList();
final GrParameter[] parameters = parameterList.getParameters();
String var;
if (parameters.length == 1) {
var = parameters[0].getText();
var = StringUtil.replace(var, GrModifier.DEF, "");
} else {
var = "it";
}
final GrExpression invokedExpression = expression.getInvokedExpression();
GrExpression qualifier = ((GrReferenceExpression) invokedExpression).getQualifierExpression();
final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(element.getProject());
if (qualifier == null) {
qualifier = elementFactory.createExpressionFromText("this");
}
StringBuilder builder = new StringBuilder();
builder.append("for (").append(var).append(" in ").append(qualifier.getText()).append(") {\n");
String text = block.getText();
final PsiElement blockArrow = block.getArrow();
int index;
if (blockArrow != null) {
index = blockArrow.getStartOffsetInParent() + blockArrow.getTextLength();
} else {
index = 1;
}
while (index < text.length() && Character.isWhitespace(text.charAt(index))) index++;
text = text.substring(index, text.length() - 1);
builder.append(text);
builder.append("}");
final GrStatement statement = elementFactory.createStatementFromText(builder.toString());
GrForStatement forStatement = (GrForStatement) expression.replaceWithStatement(statement);
final GrForClause clause = forStatement.getClause();
GrVariable variable = clause.getDeclaredVariable();
forStatement = updateReturnStatements(forStatement);
if (variable == null)
return;
if (ApplicationManager.getApplication().isUnitTestMode())
return;
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document doc = documentManager.getDocument(element.getContainingFile());
if (doc == null)
return;
documentManager.doPostponedOperationsAndUnblockDocument(doc);
editor.getCaretModel().moveToOffset(variable.getTextOffset());
new VariableInplaceRenamer(variable, editor).performInplaceRename();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class ExplicitClosureCallPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrMethodCallExpression)) {
return false;
}
final GrMethodCallExpression call = (GrMethodCallExpression) element;
final GrExpression invokedExpression = call.getInvokedExpression();
if (invokedExpression == null) {
return false;
}
if (!(invokedExpression instanceof GrReferenceExpression)) {
return false;
}
final GrReferenceExpression referenceExpression = (GrReferenceExpression) invokedExpression;
final String name = referenceExpression.getReferenceName();
if (!"call".equals(name)) {
return false;
}
final GrExpression qualifier = referenceExpression.getQualifierExpression();
if (qualifier == null) {
return false;
}
final PsiType qualifierType = qualifier.getType();
if (qualifierType == null) {
return false;
}
if (!qualifierType.equalsToText(GroovyCommonClassNames.GROOVY_LANG_CLOSURE)) {
return false;
}
return !ErrorUtil.containsError(element);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class ImplicitClosureCallPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrMethodCallExpression)) {
return false;
}
final GrMethodCallExpression call = (GrMethodCallExpression) element;
final GrExpression invokedExpression = call.getInvokedExpression();
if (invokedExpression == null) {
return false;
}
final PsiType type = invokedExpression.getType();
if (type == null) {
return false;
}
if (!type.equalsToText(GroovyCommonClassNames.GROOVY_LANG_CLOSURE)) {
return false;
}
return !ErrorUtil.containsError(element);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class MakeClosureCallImplicitIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrMethodCallExpression expression = (GrMethodCallExpression) element;
final GrReferenceExpression invokedExpression = (GrReferenceExpression) expression.getInvokedExpression();
final GrExpression qualifier = invokedExpression.getQualifierExpression();
final GrArgumentList argList = expression.getArgumentList();
final GrClosableBlock[] closureArgs = expression.getClosureArguments();
final StringBuilder newExpression = new StringBuilder();
newExpression.append(qualifier.getText());
newExpression.append(argList.getText());
for (GrClosableBlock closureArg : closureArgs) {
newExpression.append(closureArg.getText());
}
PsiImplUtil.replaceExpression(newExpression.toString(), expression);
}
Aggregations