Search in sources :

Example 1 with GroovyElementVisitor

use of org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor 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)

Example 2 with GroovyElementVisitor

use of org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor in project intellij-community by JetBrains.

the class GroovyBlockGenerator method getSpockTable.

private static List<LeafPsiElement> getSpockTable(GrStatement statement) {
    List<LeafPsiElement> result = new ArrayList<>();
    statement.accept(new GroovyElementVisitor() {

        @Override
        public void visitBinaryExpression(@NotNull GrBinaryExpression expression) {
            if (isTablePart(expression)) {
                result.add((LeafPsiElement) expression.getOperationToken());
                expression.acceptChildren(this);
            }
        }
    });
    result.sort((l1, l2) -> l1.getStartOffset() - l2.getStartOffset());
    return result;
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GroovyElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor) ArrayList(java.util.ArrayList)

Example 3 with GroovyElementVisitor

use of org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor in project intellij-community by JetBrains.

the class GroovyImportUtil method processFile.

public static void processFile(@Nullable final PsiFile file, @Nullable final Set<String> importedClasses, @Nullable final Set<String> staticallyImportedMembers, @Nullable final Set<GrImportStatement> usedImports, @Nullable final Set<GrImportStatement> unresolvedOnDemandImports, @Nullable final Set<String> implicitlyImported, @Nullable final Set<String> innerClasses, @Nullable final Map<String, String> aliased, @Nullable final Map<String, String> annotations) {
    if (!(file instanceof GroovyFile))
        return;
    final Set<String> unresolvedReferenceNames = ContainerUtil.newLinkedHashSet();
    file.accept(new PsiRecursiveElementWalkingVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (!(element instanceof GrImportStatement) && !(element instanceof GrPackageDefinition)) {
                super.visitElement(element);
            }
            if (element instanceof GrReferenceElement) {
                visitRefElement((GrReferenceElement) element);
            }
        }

        private void visitRefElement(GrReferenceElement refElement) {
            if (refElement.isQualified())
                return;
            final String refName = refElement.getReferenceName();
            if ("super".equals(refName))
                return;
            final GroovyResolveResult[] resolveResults = refElement.multiResolve(false);
            if (resolveResults.length == 0 && refName != null) {
                if (PsiTreeUtil.getParentOfType(refElement, GrImportStatement.class) == null) {
                    unresolvedReferenceNames.add(refName);
                }
            }
            for (GroovyResolveResult resolveResult : resolveResults) {
                final PsiElement context = resolveResult.getCurrentFileResolveContext();
                final PsiElement resolved = resolveResult.getElement();
                if (resolved == null)
                    return;
                if (context instanceof GrImportStatement) {
                    final GrImportStatement importStatement = (GrImportStatement) context;
                    if (usedImports != null && isImportUsed(refElement, resolved)) {
                        usedImports.add(importStatement);
                    }
                    if (GroovyImportHelper.isImplicitlyImported(resolved, refName, (GroovyFile) file)) {
                        addImplicitClass(resolved);
                    }
                    if (!importStatement.isAliasedImport() && !isAnnotatedImport(importStatement)) {
                        String importedName = null;
                        if (importStatement.isOnDemand()) {
                            if (importStatement.isStatic()) {
                                if (resolved instanceof PsiMember) {
                                    final PsiMember member = (PsiMember) resolved;
                                    final PsiClass clazz = member.getContainingClass();
                                    if (clazz != null) {
                                        final String classQName = clazz.getQualifiedName();
                                        if (classQName != null) {
                                            final String name = member.getName();
                                            if (name != null) {
                                                importedName = classQName + "." + name;
                                            }
                                        }
                                    }
                                }
                            } else {
                                importedName = getTargetQualifiedName(resolved);
                            }
                        } else {
                            final GrCodeReferenceElement importReference = importStatement.getImportReference();
                            if (importReference != null) {
                                importedName = PsiUtil.getQualifiedReferenceText(importReference);
                            }
                        }
                        if (importedName == null)
                            return;
                        final String importRef = getImportReferenceText(importStatement);
                        if (importStatement.isAliasedImport()) {
                            if (aliased != null) {
                                aliased.put(importRef, importedName);
                            }
                            return;
                        }
                        if (importStatement.isStatic()) {
                            if (staticallyImportedMembers != null) {
                                staticallyImportedMembers.add(importedName);
                            }
                        } else {
                            if (importedClasses != null) {
                                importedClasses.add(importedName);
                            }
                            if (resolved instanceof PsiClass && ((PsiClass) resolved).getContainingClass() != null && innerClasses != null) {
                                innerClasses.add(importedName);
                            }
                        }
                    }
                } else if (context == null && !(refElement.getParent() instanceof GrImportStatement) && refElement.getQualifier() == null) {
                    addImplicitClass(resolved);
                }
            }
        }

        private void addImplicitClass(PsiElement element) {
            final String qname = getTargetQualifiedName(element);
            if (qname != null) {
                if (implicitlyImported != null) {
                    implicitlyImported.add(qname);
                }
                if (importedClasses != null) {
                    importedClasses.add(qname);
                }
            }
        }

        /**
       * checks if import for implicitly imported class is needed
       */
        private boolean isImportUsed(GrReferenceElement refElement, PsiElement resolved) {
            if (GroovyImportHelper.isImplicitlyImported(resolved, refElement.getReferenceName(), (GroovyFile) file)) {
                final ClassResolverProcessor processor = new ClassResolverProcessor(refElement.getReferenceName(), refElement, ClassHint.RESOLVE_KINDS_CLASS);
                GroovyImportHelper.processImports(ResolveState.initial(), null, refElement, processor, ((GroovyFile) file).getImportStatements(), ON_DEMAND, null);
                if (!processor.hasCandidates()) {
                    return false;
                }
            }
            return true;
        }
    });
    final Set<GrImportStatement> importsToCheck = ContainerUtil.newLinkedHashSet(PsiUtil.getValidImportStatements((GroovyFile) file));
    for (GrImportStatement anImport : importsToCheck) {
        if (usedImports != null && usedImports.contains(anImport))
            continue;
        final GrCodeReferenceElement ref = anImport.getImportReference();
        assert ref != null : "invalid import!";
        if (ref.resolve() == null) {
            if (anImport.isOnDemand()) {
                if (usedImports != null) {
                    usedImports.add(anImport);
                }
                if (unresolvedOnDemandImports != null) {
                    unresolvedOnDemandImports.add(anImport);
                }
            } else {
                String importedName = anImport.getImportedName();
                if (importedName != null && unresolvedReferenceNames.contains(importedName)) {
                    if (usedImports != null) {
                        usedImports.add(anImport);
                    }
                    final String symbolName = getImportReferenceText(anImport);
                    if (anImport.isAliasedImport()) {
                        if (aliased != null) {
                            aliased.put(symbolName, importedName);
                        }
                    } else {
                        if (anImport.isStatic()) {
                            if (staticallyImportedMembers != null) {
                                staticallyImportedMembers.add(symbolName);
                            }
                        } else {
                            if (importedClasses != null) {
                                importedClasses.add(symbolName);
                            }
                        }
                    }
                }
            }
        }
    }
    if (annotations != null) {
        ((GroovyFile) file).acceptChildren(new GroovyElementVisitor() {

            @Override
            public void visitImportStatement(@NotNull GrImportStatement importStatement) {
                final String annotationText = importStatement.getAnnotationList().getText();
                if (!StringUtil.isEmptyOrSpaces(annotationText)) {
                    final String importRef = getImportReferenceText(importStatement);
                    annotations.put(importRef, annotationText);
                }
            }
        });
    }
}
Also used : GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) ClassResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.ClassResolverProcessor) GroovyElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Aggregations

GroovyElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor)3 PsiElement (com.intellij.psi.PsiElement)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 ArrayList (java.util.ArrayList)1 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)1 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)1 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)1 GrCommandArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList)1 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)1 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)1 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)1 GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)1 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)1 ClassResolverProcessor (org.jetbrains.plugins.groovy.lang.resolve.processors.ClassResolverProcessor)1