Search in sources :

Example 6 with GrApplicationStatement

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

the class ApplicationStatementUtil method convertToMethodCallExpression.

public static GrExpression convertToMethodCallExpression(GrExpression expr) {
    final Project project = expr.getProject();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    boolean copied = false;
    if (expr instanceof GrApplicationStatement) {
        expr = convertAppInternal(factory, (GrApplicationStatement) expr);
        copied = true;
    }
    if (expr instanceof GrReferenceExpression && ((GrReferenceExpression) expr).getDotToken() == null && ((GrReferenceExpression) expr).getQualifier() != null) {
        expr = convertRefInternal(factory, ((GrReferenceExpression) expr));
        copied = true;
    }
    if (!shouldManage(expr))
        return expr;
    if (!copied)
        expr = (GrExpression) expr.copy();
    for (PsiElement child = expr.getFirstChild(); child != null; child = child.getFirstChild()) {
        if (child instanceof GrApplicationStatement) {
            child = child.replace(convertAppInternal(factory, (GrApplicationStatement) child));
        } else if (child instanceof GrReferenceExpression && ((GrReferenceExpression) child).getDotToken() == null && ((GrReferenceExpression) child).getQualifier() != null) {
            child = child.replace(convertRefInternal(factory, ((GrReferenceExpression) child)));
        }
    }
    return expr;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 7 with GrApplicationStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement 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 8 with GrApplicationStatement

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

the class ImportMavenRepositoriesTask method findMavenRemoteRepositories.

@NotNull
private Collection<? extends MavenRemoteRepository> findMavenRemoteRepositories(@Nullable GrClosableBlock repositoriesBlock) {
    Set<MavenRemoteRepository> myRemoteRepositories = ContainerUtil.newHashSet();
    for (GrMethodCall repo : PsiTreeUtil.getChildrenOfTypeAsList(repositoriesBlock, GrMethodCall.class)) {
        final String expressionText = repo.getInvokedExpression().getText();
        if ("mavenCentral".equals(expressionText)) {
            myRemoteRepositories.add(mavenCentralRemoteRepository);
        } else if ("mavenRepo".equals(expressionText)) {
            for (GrNamedArgument namedArgument : repo.getNamedArguments()) {
                if ("url".equals(namedArgument.getLabelName())) {
                    URI urlArgumentValue = resolveUriFromSimpleExpression(namedArgument.getExpression());
                    if (urlArgumentValue != null) {
                        String textUri = urlArgumentValue.toString();
                        myRemoteRepositories.add(new MavenRemoteRepository(textUri, null, textUri, null, null, null));
                    }
                    break;
                }
            }
        } else if ("maven".equals(expressionText) && repo.getClosureArguments().length > 0) {
            List<GrApplicationStatement> applicationStatementList = PsiTreeUtil.getChildrenOfTypeAsList(repo.getClosureArguments()[0], GrApplicationStatement.class);
            if (!applicationStatementList.isEmpty()) {
                GrApplicationStatement statement = applicationStatementList.get(0);
                if (statement == null)
                    continue;
                GrExpression expression = statement.getInvokedExpression();
                if ("url".equals(expression.getText())) {
                    URI urlArgumentValue = resolveUriFromSimpleExpression(statement.getExpressionArguments()[0]);
                    if (urlArgumentValue != null) {
                        String textUri = urlArgumentValue.toString();
                        myRemoteRepositories.add(new MavenRemoteRepository(textUri, null, textUri, null, null, null));
                    }
                }
            }
            List<GrAssignmentExpression> assignmentExpressionList = PsiTreeUtil.getChildrenOfTypeAsList(repo.getClosureArguments()[0], GrAssignmentExpression.class);
            if (!assignmentExpressionList.isEmpty()) {
                GrAssignmentExpression statement = assignmentExpressionList.get(0);
                if (statement == null)
                    continue;
                GrExpression expression = statement.getLValue();
                if ("url".equals(expression.getText())) {
                    URI urlArgumentValue = resolveUriFromSimpleExpression(statement.getRValue());
                    if (urlArgumentValue != null) {
                        String textUri = urlArgumentValue.toString();
                        myRemoteRepositories.add(new MavenRemoteRepository(textUri, null, textUri, null, null, null));
                    }
                }
            }
        }
    }
    return myRemoteRepositories;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) MavenRemoteRepository(org.jetbrains.idea.maven.model.MavenRemoteRepository) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) URI(java.net.URI) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with GrApplicationStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement in project android by JetBrains.

the class GradleEditorModelParserFacade method fillContext.

/**
   * Processes given PSI file and fills given context
   * by {@link GradleEditorModelParseContext#getAssignments(Variable) corresponding assignments}.
   *
   * @param context  context to fill
   * @param psiFile  psi file to parse
   */
private static void fillContext(@NotNull final GradleEditorModelParseContext context, @NotNull PsiFile psiFile) {
    psiFile.acceptChildren(new GroovyPsiElementVisitor(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitMethodCallExpression(GrMethodCallExpression methodCallExpression) {
            Pair<String, TextRange> pair = GradleEditorValueExtractor.extractMethodName(methodCallExpression);
            GrClosableBlock[] closureArguments = methodCallExpression.getClosureArguments();
            if (pair == null || closureArguments.length > 1) {
                super.visitMethodCallExpression(methodCallExpression);
                return;
            }
            if (closureArguments.length == 0) {
                if (methodCallExpression.getArgumentList().getAllArguments().length == 0) {
                    // This is a no-args method, so, we just register it for cases like 'mavenCentral()' or 'jcenter()'.
                    context.addCachedValue(NO_ARGS_METHOD_ASSIGNMENT_VALUE, TextRange.create(pair.second.getEndOffset(), methodCallExpression.getTextRange().getEndOffset()));
                    context.registerAssignmentFromCachedData(pair.first, pair.second, methodCallExpression);
                }
                return;
            }
            context.onMethodEnter(pair.getFirst());
            try {
                super.visitClosure(closureArguments[0]);
            } finally {
                context.onMethodExit();
            }
        }

        @Override
        public void visitApplicationStatement(GrApplicationStatement applicationStatement) {
            Pair<String, TextRange> methodName = GradleEditorValueExtractor.extractMethodName(applicationStatement);
            if (methodName == null) {
                return;
            }
            GroovyPsiElement[] allArguments = applicationStatement.getArgumentList().getAllArguments();
            if (allArguments.length == 1) {
                context.resetCaches();
                extractValueOrVariable(allArguments[0], context);
                context.registerAssignmentFromCachedData(methodName.getFirst(), methodName.getSecond(), applicationStatement.getArgumentList());
            }
        }

        @Override
        public void visitAssignmentExpression(GrAssignmentExpression expression) {
            // General idea is to try to extract variable from the given expression and, in case of success, try to extract rvalue and
            // register corresponding assignment with them.
            context.resetCaches();
            extractValueOrVariable(expression.getLValue(), context);
            Multimap<Variable, Location> vars = context.getCachedVariables();
            if (vars.size() != 1) {
                context.resetCaches();
                return;
            }
            Map.Entry<Variable, Location> entry = vars.entries().iterator().next();
            Variable lVariable = entry.getKey();
            Location lVariableLocation = entry.getValue();
            context.resetCaches();
            GrExpression rValue = expression.getRValue();
            if (rValue == null) {
                return;
            }
            extractValueOrVariable(rValue, context);
            if (context.getCachedValues().size() > 1) {
                Value value = new Value("", new Location(context.getCurrentFile(), GradleEditorModelUtil.interestedRange(rValue)));
                context.setCachedValues(Collections.singletonList(value));
            }
            context.registerAssignmentFromCachedData(lVariable, lVariableLocation, rValue);
            context.resetCaches();
        }

        @Override
        public void visitVariable(GrVariable variable) {
            TextRange nameRange = null;
            boolean lookForInitializer = false;
            ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.findSingle(GroovyLanguage.INSTANCE);
            for (PsiElement e = variable.getFirstChild(); e != null; e = e.getNextSibling()) {
                ASTNode node = e.getNode();
                if (node == null) {
                    continue;
                }
                if (!lookForInitializer) {
                    if (node.getElementType() == GroovyTokenTypes.mIDENT) {
                        nameRange = e.getTextRange();
                    } else if (node.getElementType() == GroovyTokenTypes.mASSIGN) {
                        if (nameRange == null) {
                            return;
                        }
                        lookForInitializer = true;
                    }
                    continue;
                }
                if (node.getElementType() == GroovyTokenTypes.mNLS || node.getElementType() == GroovyTokenTypes.mSEMI) {
                    break;
                }
                if (parserDefinition.getWhitespaceTokens().contains(node.getElementType())) {
                    continue;
                }
                extractValueOrVariable(e, context);
                if (context.getCachedValues().size() > 1) {
                    Value value = new Value("", new Location(context.getCurrentFile(), GradleEditorModelUtil.interestedRange(e)));
                    context.setCachedValues(Collections.singletonList(value));
                }
                if (context.registerAssignmentFromCachedData(variable.getName(), nameRange, e)) {
                    return;
                }
            }
        }
    }));
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) TextRange(com.intellij.openapi.util.TextRange) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) ParserDefinition(com.intellij.lang.ParserDefinition) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GroovyPsiElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementVisitor) ASTNode(com.intellij.lang.ASTNode) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 10 with GrApplicationStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement in project android by JetBrains.

the class GradleDslElement method deleteIfEmpty.

protected static void deleteIfEmpty(@Nullable PsiElement element) {
    if (element == null || !element.isValid()) {
        return;
    }
    PsiElement parent = element.getParent();
    if (element instanceof GrAssignmentExpression) {
        if (((GrAssignmentExpression) element).getRValue() == null) {
            element.delete();
        }
    } else if (element instanceof GrApplicationStatement) {
        if (((GrApplicationStatement) element).getArgumentList() == null) {
            element.delete();
        }
    } else if (element instanceof GrClosableBlock) {
        final Boolean[] isEmpty = new Boolean[] { true };
        ((GrClosableBlock) element).acceptChildren(new GroovyElementVisitor() {

            @Override
            public void visitElement(GroovyPsiElement child) {
                if (child instanceof GrParameterList) {
                    if (((GrParameterList) child).getParameters().length == 0) {
                        // Ignore the empty parameter list.
                        return;
                    }
                }
                isEmpty[0] = false;
            }
        });
        if (isEmpty[0]) {
            element.delete();
        }
    } else if (element instanceof GrMethodCallExpression) {
        GrMethodCallExpression call = ((GrMethodCallExpression) element);
        GrArgumentList argumentList;
        try {
            argumentList = call.getArgumentList();
        } catch (AssertionError e) {
            // We will get this exception if the argument list is already deleted.
            argumentList = null;
        }
        GrClosableBlock[] closureArguments = call.getClosureArguments();
        if ((argumentList == null || argumentList.getAllArguments().length == 0) && closureArguments.length == 0) {
            element.delete();
        }
    } else if (element instanceof GrCommandArgumentList) {
        GrCommandArgumentList commandArgumentList = (GrCommandArgumentList) element;
        if (commandArgumentList.getAllArguments().length == 0) {
            commandArgumentList.delete();
        }
    } else if (element instanceof GrListOrMap) {
        GrListOrMap listOrMap = (GrListOrMap) element;
        if ((listOrMap.isMap() && listOrMap.getNamedArguments().length == 0) || (!listOrMap.isMap() && listOrMap.getInitializers().length == 0)) {
            listOrMap.delete();
        }
    }
    if (!element.isValid()) {
        // If this element is deleted, also delete the parent if it is empty.
        deleteIfEmpty(parent);
    }
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) GroovyElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) 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) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)13 PsiElement (com.intellij.psi.PsiElement)11 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)9 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)6 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)5 Nullable (org.jetbrains.annotations.Nullable)4 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)4 NotNull (org.jetbrains.annotations.NotNull)3 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)3 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)3 GrCommandArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList)3 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)3 Project (com.intellij.openapi.project.Project)2 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)2 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)2 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)2 ResourceFolderType (com.android.resources.ResourceFolderType)1