Search in sources :

Example 41 with GrMethodCallExpression

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

the class IndexingMethodConversionIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrMethodCallExpression callExpression = (GrMethodCallExpression) element;
    final GrArgumentList argList = callExpression.getArgumentList();
    final GrExpression[] arguments = argList.getExpressionArguments();
    GrReferenceExpression methodExpression = (GrReferenceExpression) callExpression.getInvokedExpression();
    final IElementType referenceType = methodExpression.getDotTokenType();
    final String methodName = methodExpression.getReferenceName();
    final GrExpression qualifier = methodExpression.getQualifierExpression();
    if ("getAt".equals(methodName) || "get".equals(methodName)) {
        PsiImplUtil.replaceExpression(qualifier.getText() + '[' + arguments[0].getText() + ']', callExpression);
    } else {
        PsiImplUtil.replaceExpression(qualifier.getText() + '[' + arguments[0].getText() + "]=" + arguments[1].getText(), callExpression);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 42 with GrMethodCallExpression

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

the class RemoveParenthesesFromMethodCallIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrMethodCallExpression expression = (GrMethodCallExpression) element;
    final StringBuilder newStatementText = new StringBuilder();
    newStatementText.append(expression.getInvokedExpression().getText()).append(' ');
    final GrArgumentList argumentList = expression.getArgumentList();
    final PsiElement leftParen = argumentList.getLeftParen();
    final PsiElement rightParen = argumentList.getRightParen();
    if (leftParen != null)
        leftParen.delete();
    if (rightParen != null)
        rightParen.delete();
    newStatementText.append(argumentList.getText());
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
    final GrStatement newStatement = factory.createStatementFromText(newStatementText.toString());
    expression.replaceWithStatement(newStatement);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 43 with GrMethodCallExpression

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

the class RemoveParenthesesFromMethodPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrMethodCallExpression))
        return false;
    if (!PsiUtil.isExpressionStatement(element))
        return false;
    final GrMethodCallExpression expression = (GrMethodCallExpression) element;
    if (PsiImplUtil.hasClosureArguments(expression))
        return false;
    final StringBuilder newStatementText = new StringBuilder();
    newStatementText.append(expression.getInvokedExpression().getText()).append(' ');
    GrArgumentList argumentList = expression.getArgumentList();
    final GroovyPsiElement[] allArguments = argumentList.getAllArguments();
    argumentList = (GrArgumentList) argumentList.copy();
    final PsiElement leftParen = argumentList.getLeftParen();
    final PsiElement rightParen = argumentList.getRightParen();
    if (leftParen != null)
        leftParen.delete();
    if (rightParen != null)
        rightParen.delete();
    newStatementText.append(argumentList.getText());
    final GrStatement newStatement;
    try {
        newStatement = GroovyPsiElementFactory.getInstance(element.getProject()).createStatementFromText(newStatementText.toString());
    } catch (IncorrectOperationException e) {
        return false;
    }
    if (newStatement instanceof GrApplicationStatement) {
        final GrCommandArgumentList newArgList = ((GrApplicationStatement) newStatement).getArgumentList();
        if (newArgList.getAllArguments().length == allArguments.length) {
            return true;
        }
    }
    return false;
}
Also used : GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 44 with GrMethodCallExpression

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

the class ConvertJavaStyleArrayIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (!(element instanceof GrMethodCallExpression))
                return false;
            final GrExpression expression = ((GrMethodCallExpression) element).getInvokedExpression();
            if (!(expression instanceof GrNewExpression))
                return false;
            if (((GrNewExpression) expression).getArrayCount() == 0)
                return false;
            if (!((GrMethodCallExpression) element).getArgumentList().getText().trim().isEmpty())
                return false;
            final GrClosableBlock[] closureArguments = ((GrMethodCallExpression) element).getClosureArguments();
            if (closureArguments.length != 1)
                return false;
            final GrClosableBlock block = closureArguments[0];
            if (block.getLBrace() == null || block.getRBrace() == null)
                return false;
            return true;
        }
    };
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 45 with GrMethodCallExpression

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

the class GroovyBlockGenerator method addNestedChildren.

private void addNestedChildren(final PsiElement elem, List<Block> list, @Nullable AlignmentProvider.Aligner aligner, final boolean topLevel, Wrap wrap) {
    final List<ASTNode> children = visibleChildren(elem.getNode());
    if (elem instanceof GrMethodCallExpression) {
        GrExpression invokedExpression = ((GrMethodCallExpression) elem).getInvokedExpression();
        if (invokedExpression instanceof GrQualifiedReference) {
            final PsiElement nameElement = ((GrQualifiedReference) invokedExpression).getReferenceNameElement();
            if (nameElement != null) {
                List<ASTNode> grandChildren = visibleChildren(invokedExpression.getNode());
                int i = 0;
                while (i < grandChildren.size() && nameElement != grandChildren.get(i).getPsi()) i++;
                if (i > 0) {
                    processNestedChildrenPrefix(list, aligner, false, grandChildren, i, wrap);
                }
                if (i < grandChildren.size()) {
                    LOG.assertTrue(nameElement == grandChildren.get(i).getPsi());
                    list.add(new MethodCallWithoutQualifierBlock(nameElement, wrap, topLevel, children, elem, myContext));
                }
                return;
            }
        }
    }
    processNestedChildrenPrefix(list, aligner, topLevel, children, children.size(), wrap);
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) ASTNode(com.intellij.lang.ASTNode) GrQualifiedReference(org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

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