Search in sources :

Example 21 with GrMethodCallExpression

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;
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 22 with GrMethodCallExpression

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();
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Document(com.intellij.openapi.editor.Document) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause) VariableInplaceRenamer(com.intellij.refactoring.rename.inplace.VariableInplaceRenamer) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 23 with GrMethodCallExpression

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);
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType)

Example 24 with GrMethodCallExpression

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);
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiType(com.intellij.psi.PsiType)

Example 25 with GrMethodCallExpression

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);
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)48 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)22 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)17 PsiElement (com.intellij.psi.PsiElement)16 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)14 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)11 Nullable (org.jetbrains.annotations.Nullable)8 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)8 NotNull (org.jetbrains.annotations.NotNull)5 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)5 TextRange (com.intellij.openapi.util.TextRange)4 PsiType (com.intellij.psi.PsiType)4 IElementType (com.intellij.psi.tree.IElementType)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 NonNls (org.jetbrains.annotations.NonNls)4 ASTNode (com.intellij.lang.ASTNode)3 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)3 GrCommandArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList)3