Search in sources :

Example 11 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 12 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 13 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)

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