Search in sources :

Example 36 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 37 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)

Example 38 with GrMethodCallExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.

the class NewGantScriptAction method doCreate.

@Override
@NotNull
protected PsiElement[] doCreate(String newName, PsiDirectory directory) throws Exception {
    PsiFile file = createGantScriptFromTemplate(directory, newName, GroovyTemplates.GANT_SCRIPT);
    PsiElement lastChild = file.getLastChild();
    PsiElement child = null;
    if (lastChild instanceof GrMethodCallExpression) {
        child = lastChild;
    }
    if (child == null && file.getChildren().length > 0) {
        child = file.getLastChild();
    }
    return child != null ? new PsiElement[] { file, child } : new PsiElement[] { file };
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 39 with GrMethodCallExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.

the class OldReferencesResolver method canRemoveQualifier.

private static boolean canRemoveQualifier(GrReferenceExpression refExpr) {
    try {
        GrExpression qualifier = refExpr.getQualifier();
        if (!(qualifier instanceof GrReferenceExpression))
            return false;
        PsiElement qualifierRefElement = ((GrReferenceExpression) qualifier).resolve();
        if (!(qualifierRefElement instanceof PsiClass))
            return false;
        PsiElement refElement = refExpr.resolve();
        if (refElement == null)
            return false;
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(refExpr.getProject());
        if (refExpr.getParent() instanceof GrMethodCallExpression) {
            GrMethodCallExpression methodCall = (GrMethodCallExpression) refExpr.getParent();
            GrMethodCallExpression newMethodCall = (GrMethodCallExpression) factory.createExpressionFromText(refExpr.getReferenceName() + "()", refExpr);
            newMethodCall.getArgumentList().replace(methodCall.getArgumentList());
            PsiElement newRefElement = ((GrReferenceExpression) newMethodCall.getInvokedExpression()).resolve();
            return refElement.equals(newRefElement);
        } else {
            GrReferenceExpression newRefExpr = (GrReferenceExpression) factory.createExpressionFromText(refExpr.getReferenceName(), refExpr);
            PsiElement newRefElement = newRefExpr.resolve();
            return refElement.equals(newRefElement);
        }
    } catch (IncorrectOperationException e) {
        return false;
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 40 with GrMethodCallExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.

the class ConvertConcatenationToGstringIntention method isToStringMethod.

/**
   * append text to builder if the operand is 'something'.toString()
   */
private static boolean isToStringMethod(GrExpression operand, StringBuilder builder) {
    if (!(operand instanceof GrMethodCallExpression))
        return false;
    final GrExpression expression = ((GrMethodCallExpression) operand).getInvokedExpression();
    if (!(expression instanceof GrReferenceExpression))
        return false;
    final GrReferenceExpression refExpr = (GrReferenceExpression) expression;
    final GrExpression qualifier = refExpr.getQualifierExpression();
    if (qualifier == null)
        return false;
    final GroovyResolveResult[] results = refExpr.multiResolve(false);
    if (results.length != 1)
        return false;
    final PsiElement element = results[0].getElement();
    if (!(element instanceof PsiMethod))
        return false;
    final PsiMethod method = (PsiMethod) element;
    final PsiClass objectClass = JavaPsiFacade.getInstance(operand.getProject()).findClass(CommonClassNames.JAVA_LANG_OBJECT, operand.getResolveScope());
    if (objectClass == null)
        return false;
    final PsiMethod[] toStringMethod = objectClass.findMethodsByName("toString", true);
    if (MethodSignatureUtil.isSubsignature(toStringMethod[0].getHierarchicalMethodSignature(), method.getHierarchicalMethodSignature())) {
        builder.append(START_BRACE).append(qualifier.getText()).append(END_BRACE);
        return true;
    }
    return false;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) 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