Search in sources :

Example 1 with GrApplicationStatement

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

the class JavaStylePropertiesInvocationInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitMethodCallExpression(@NotNull GrMethodCallExpression methodCallExpression) {
            super.visitMethodCallExpression(methodCallExpression);
            visitMethodCall(methodCallExpression);
        }

        @Override
        public void visitApplicationStatement(@NotNull GrApplicationStatement applicationStatement) {
            super.visitApplicationStatement(applicationStatement);
            visitMethodCall(applicationStatement);
        }

        private void visitMethodCall(GrMethodCall methodCall) {
            if (JavaStylePropertiesUtil.isPropertyAccessor(methodCall)) {
                final String message = GroovyInspectionBundle.message("java.style.property.access");
                final GrExpression expression = methodCall.getInvokedExpression();
                if (expression instanceof GrReferenceExpression) {
                    PsiElement referenceNameElement = ((GrReferenceExpression) expression).getReferenceNameElement();
                    registerError(referenceNameElement, message, myFixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
                }
            }
        }
    };
}
Also used : BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GrApplicationStatement

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

the class AndroidGradleSpellcheckingStrategy method isPrint.

private static boolean isPrint(PsiElement element) {
    PsiElement parent0 = element.getParent();
    if (parent0 == null) {
        return false;
    }
    PsiElement parent1 = parent0.getParent();
    if (parent1 == null) {
        return false;
    }
    PsiElement parent2 = parent1.getParent();
    if (parent2 == null) {
        return false;
    }
    if (parent2 instanceof GrCommandArgumentList) {
        parent2 = parent2.getParent();
    }
    if (parent2 instanceof GrApplicationStatement) {
        GrApplicationStatement call = (GrApplicationStatement) parent2;
        GrExpression propertyExpression = call.getInvokedExpression();
        if (propertyExpression instanceof GrReferenceExpression) {
            GrReferenceExpression propertyRef = (GrReferenceExpression) propertyExpression;
            String property = propertyRef.getReferenceName();
            if ("print".equals(property) || "println".equals(property)) {
                return true;
            }
        }
    }
    return false;
}
Also used : GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) 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) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 3 with GrApplicationStatement

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

the class GradleDslElement method create.

/**
   * Creates the {@link GroovyPsiElement} by adding this element to the .gradle file.
   *
   * <p>It creates a new {@link GroovyPsiElement} only when {@link #getPsiElement()} return {@code null}.
   *
   * <p>Returns the final {@link GroovyPsiElement} corresponds to this element or {@code null} when failed to create the
   * {@link GroovyPsiElement}.
   */
@Nullable
public GroovyPsiElement create() {
    GroovyPsiElement psiElement = getPsiElement();
    if (psiElement != null) {
        return psiElement;
    }
    if (myParent == null) {
        return null;
    }
    GroovyPsiElement parentPsiElement = myParent.create();
    if (parentPsiElement == null) {
        return null;
    }
    Project project = parentPsiElement.getProject();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    if (isNewEmptyBlockElement()) {
        // Avoid creation of an empty block statement.
        return null;
    }
    String statementText = myName + (isBlockElement() ? " {\n}\n" : " \"abc\", \"xyz\"");
    GrStatement statement = factory.createStatementFromText(statementText);
    if (statement instanceof GrApplicationStatement) {
        // Workaround to create an application statement.
        ((GrApplicationStatement) statement).getArgumentList().delete();
    }
    PsiElement lineTerminator = factory.createLineTerminator(1);
    PsiElement addedElement;
    if (parentPsiElement instanceof GroovyFile) {
        addedElement = parentPsiElement.addAfter(statement, parentPsiElement.getLastChild());
        parentPsiElement.addBefore(lineTerminator, addedElement);
    } else {
        addedElement = parentPsiElement.addBefore(statement, parentPsiElement.getLastChild());
        parentPsiElement.addAfter(lineTerminator, addedElement);
    }
    if (isBlockElement()) {
        GrClosableBlock closableBlock = getClosableBlock(addedElement);
        if (closableBlock != null) {
            setPsiElement(closableBlock);
        }
    } else {
        if (addedElement instanceof GrApplicationStatement) {
            setPsiElement((GrApplicationStatement) addedElement);
        }
    }
    return getPsiElement();
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GrApplicationStatement

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

the class GradleDslExpressionList method create.

@Override
@Nullable
public GroovyPsiElement create() {
    GroovyPsiElement psiElement = getPsiElement();
    if (psiElement == null) {
        if (myParent instanceof GradleDslExpressionMap) {
            // This is a list in the map element and we need to create a named argument for it.
            return createNamedArgumentList();
        }
        psiElement = super.create();
    }
    if (psiElement == null) {
        return null;
    }
    if (psiElement instanceof GrListOrMap) {
        return psiElement;
    }
    if (psiElement instanceof GrArgumentList) {
        if (!myToBeAddedExpressions.isEmpty() && ((GrArgumentList) psiElement).getAllArguments().length == 1 && !myAppendToArgumentListWithOneElement) {
            // Sometimes it's not possible to append to the arguments list with one item. eg. proguardFile "xyz".
            // Set the psiElement to null and create a new psiElement of an empty application statement.
            setPsiElement(null);
            psiElement = super.create();
        } else {
            return psiElement;
        }
    }
    if (psiElement instanceof GrApplicationStatement) {
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
        GrArgumentList argumentList = factory.createArgumentListFromText("xyz");
        // Workaround to get an empty argument list.
        argumentList.getFirstChild().delete();
        PsiElement added = psiElement.addAfter(argumentList, psiElement.getLastChild());
        if (added instanceof GrArgumentList) {
            GrArgumentList addedArgumentList = (GrArgumentList) added;
            setPsiElement(addedArgumentList);
            return addedArgumentList;
        }
    }
    return null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with GrApplicationStatement

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

the class GradleDslMethodCall method create.

@Override
@Nullable
public GroovyPsiElement create() {
    GroovyPsiElement psiElement = getPsiElement();
    if (psiElement != null) {
        return psiElement;
    }
    if (myParent == null) {
        return null;
    }
    GroovyPsiElement parentPsiElement = myParent.create();
    if (parentPsiElement == null) {
        return null;
    }
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentPsiElement.getProject());
    String statementText = (myStatementName != null ? myStatementName + " " : "") + myName + "()";
    GrStatement statement = factory.createStatementFromText(statementText);
    PsiElement addedElement = parentPsiElement.addBefore(statement, parentPsiElement.getLastChild());
    if (addedElement instanceof GrApplicationStatement) {
        GrExpression[] expressionArguments = ((GrApplicationStatement) addedElement).getArgumentList().getExpressionArguments();
        if (expressionArguments.length == 1 && expressionArguments[0] instanceof GrMethodCallExpression) {
            setPsiElement(expressionArguments[0]);
            return getPsiElement();
        }
    }
    if (addedElement instanceof GrMethodCallExpression) {
        setPsiElement((GrMethodCallExpression) addedElement);
        return getPsiElement();
    }
    return null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) 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) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

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