Java Code Examples for com.intellij.psi.util.PsiTreeUtil#getChildrenOfAnyType()

The following examples show how to use com.intellij.psi.util.PsiTreeUtil#getChildrenOfAnyType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqlsXmlUtil.java    From NutzCodeInsight with Apache License 2.0 6 votes vote down vote up
public static List<PsiField> getExtendsClassFields(PsiClass psiClass) {
    List<PsiField> psiFields = new ArrayList<>();
    List<PsiReferenceList> childrenOfAnyType = PsiTreeUtil.getChildrenOfAnyType(psiClass, PsiReferenceList.class);
    for (PsiReferenceList psiReferenceList : childrenOfAnyType) {
        PsiJavaCodeReferenceElement[] referenceElements = psiReferenceList.getReferenceElements();
        for (PsiJavaCodeReferenceElement referenceElement : referenceElements) {
            String qualifiedName = referenceElement.getQualifiedName();
            GlobalSearchScope globalSearchScope = GlobalSearchScope.projectScope(psiClass.getProject());
            Collection<PsiClass> psiClasses = JavaShortClassNameIndex.getInstance().get(referenceElement.getReferenceName(), psiClass.getProject(), globalSearchScope);
            for (PsiClass aClass : psiClasses) {
                if (aClass.getQualifiedName().equals(qualifiedName)) {
                    psiFields.addAll(Arrays.asList(aClass.getFields()));
                    psiFields.addAll(getExtendsClassFields(aClass));
                }
            }
        }
    }
    return psiFields;
}
 
Example 2
Source File: SqlsXmlUtil.java    From NutzCodeInsight with Apache License 2.0 6 votes vote down vote up
/**
 * JavaSuperClassNameOccurenceIndex.getInstance().get("ISqlDaoExecuteService", project, moduleWithDependenciesScope)
 *
 * @param project
 * @return
 */
public static List<String> addExecuteService(Project project) {
    if (CollectionUtils.isEmpty(SERVICE_METHOD_List)) {
        List<String> arry = new ArrayList<>();
        Collection<PsiClass> iSqlDaoExecuteService = JavaShortClassNameIndex.getInstance().get("ISqlDaoExecuteService", project, GlobalSearchScope.everythingScope(project));
        for (PsiClass psiClass : iSqlDaoExecuteService) {
            if (SERVICE_ISQL_DAO_EXECUTE_SERVICE.equals(psiClass.getQualifiedName())) {
                List<PsiMethod> methods = PsiTreeUtil.getChildrenOfAnyType(psiClass, PsiMethod.class);
                for (PsiMethod method : methods) {
                    arry.add(method.getName() + "(");
                }
            }
        }
        SERVICE_METHOD_List = arry;
    }
    return SERVICE_METHOD_List;
}
 
Example 3
Source File: SqlsXmlUtil.java    From NutzCodeInsight with Apache License 2.0 6 votes vote down vote up
public static String findXmlFileName(PsiClass psiClass) {
    List<PsiModifierList> modifierLists = PsiTreeUtil.getChildrenOfAnyType(psiClass, PsiModifierList.class);
    if (CollectionUtils.isNotEmpty(modifierLists)) {
        List<PsiAnnotation> annotations = PsiTreeUtil.getChildrenOfAnyType(modifierLists.get(0), PsiAnnotation.class);
        for (PsiAnnotation annotation : annotations) {
            if (ANNOTATION_SQLS_XML.equals(annotation.getQualifiedName())) {
                List<JvmAnnotationAttribute> attributes = annotation.getAttributes();
                for (JvmAnnotationAttribute attribute : attributes) {
                    String attributeName = attribute.getAttributeName();
                    if ("value".equals(attributeName)) {
                        return ((PsiNameValuePairImpl) attribute).getLiteralValue();
                    }
                }
                if (CollectionUtils.isEmpty(attributes)) {
                    String name = psiClass.getContainingFile().getName();
                    name = name.substring(0, name.length() - 4);
                    return name + "xml";
                }
            }
        }
    }
    return "xml";
}
 
Example 4
Source File: SqlsXmlUtil.java    From NutzCodeInsight with Apache License 2.0 6 votes vote down vote up
public static PsiElement getXmlIdBindPsiElement(PsiMethodCallExpression psiMethodCallExpression, List<String> keys, String id) {
    for (String key : keys) {
        String text = psiMethodCallExpression.getText();
        if (text.startsWith(key)
                || text.startsWith("super." + key)
                || text.startsWith("this." + key)) {
            PsiExpressionList psiExpressionList = PsiTreeUtil.getChildOfAnyType(psiMethodCallExpression, PsiExpressionList.class);
            if (Objects.nonNull(psiExpressionList)) {
                List<PsiLiteralExpression> psiLiteralExpressions = PsiTreeUtil.getChildrenOfAnyType(psiExpressionList, PsiLiteralExpression.class);
                for (PsiLiteralExpression psiLiteralExpression : psiLiteralExpressions) {
                    if (!psiLiteralExpression.getText().contains(" ") && id.equals(((PsiLiteralExpressionImpl) psiLiteralExpression).getInnerText())) {
                        return psiLiteralExpression;
                    }
                }
            }
        }
    }
    return null;
}
 
Example 5
Source File: YamlHelper.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * test:
 *   DEBUG_WEB_1: 1
 *   DEBUG_WEB_2: 1
 */
@NotNull
public static Map<String, YAMLValue> getYamlArrayKeyMap(@NotNull YAMLMapping yamlHash) {
    Map<String, YAMLValue> keys = new HashMap<>();

    for(YAMLKeyValue yamlKeyValue: PsiTreeUtil.getChildrenOfAnyType(yamlHash, YAMLKeyValue.class)) {
        String keyText = yamlKeyValue.getKeyText();
        if (StringUtils.isNotBlank(keyText)) {
            YAMLValue value = yamlKeyValue.getValue();
            if (value != null) {
                keys.put(keyText, value);
            }
        }
    }

    return keys;
}
 
Example 6
Source File: SqlsXmlUtil.java    From NutzCodeInsight with Apache License 2.0 5 votes vote down vote up
public static boolean hasExecuteService(PsiClass psiClass) {
    List<PsiReferenceList> childrenOfAnyType = PsiTreeUtil.getChildrenOfAnyType(psiClass, PsiReferenceList.class);
    for (PsiReferenceList psiReferenceList : childrenOfAnyType) {
        List<PsiJavaCodeReferenceElement> referenceElements = PsiTreeUtil.getChildrenOfAnyType(psiReferenceList, PsiJavaCodeReferenceElement.class);
        for (PsiJavaCodeReferenceElement referenceElement : referenceElements) {
            if (SERVICE_ISQL_DAO_EXECUTE_SERVICE.equals(referenceElement.getQualifiedName())) {
                return true;
            }
        }
    }
    return false;
}
 
Example 7
Source File: SqlsXmlUtil.java    From NutzCodeInsight with Apache License 2.0 5 votes vote down vote up
public static List<PsiElement> findXmlPsiElement(Project project, Collection<VirtualFile> virtualFiles, String key) {
    List<PsiElement> result = new ArrayList<>();
    for (VirtualFile virtualFile : virtualFiles) {
        PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
        if (psiFile instanceof XmlFileImpl) {
            XmlFileImpl xmlFile = (XmlFileImpl) psiFile;
            PsiElement navigationElement = xmlFile.getNavigationElement();
            if (Objects.nonNull(navigationElement)) {
                if (Objects.nonNull(navigationElement.getChildren())) {
                    PsiElement child = navigationElement.getChildren()[0];
                    if (Objects.nonNull(child)) {
                        if (Objects.nonNull(child.getChildren()) && child.getChildren().length > 0) {
                            PsiElement child1 = child.getChildren()[1];
                            if (Objects.nonNull(child1)) {
                                PsiElement[] psiElements = child1.getChildren();
                                if (Objects.nonNull(psiElements) && psiElements.length > 0) {
                                    List<XmlTag> xmlTags = PsiTreeUtil.getChildrenOfAnyType(child1, XmlTag.class);
                                    for (XmlTag xmlTag : xmlTags) {
                                        if ("sql".equals(xmlTag.getName())) {
                                            XmlAttribute xmlAttribute = xmlTag.getAttribute("id");
                                            String id = xmlAttribute.getValue();
                                            if (key.equals(id)) {
                                                result.add(xmlAttribute.getValueElement());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return result;
}
 
Example 8
Source File: TwigBlockIndexExtension.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, Set<String>, FileContent> getIndexer() {
    return fileContent -> {
        Map<String, Set<String>> blocks = new HashMap<>();

        PsiFile psiFile = fileContent.getPsiFile();
        if(psiFile instanceof TwigFile) {
            for (TwigBlock twigBlock : TwigUtil.getBlocksInFile((TwigFile) psiFile)) {
                // we only index file scope
                // {% embed 'foo.html.twig' %}{% block foo %}{% endembed %}
                PsiElement embedStatement = PsiElementUtils.getParentOfType(twigBlock.getTarget(), TwigElementTypes.EMBED_STATEMENT);
                if(embedStatement == null) {
                    blocks.putIfAbsent("block", new HashSet<>());
                    blocks.get("block").add(twigBlock.getName());
                }
            }

            for(PsiElement psiElement : PsiTreeUtil.getChildrenOfAnyType(psiFile, TwigExtendsTag.class, TwigCompositeElement.class)) {
                if(psiElement instanceof TwigCompositeElement) {
                    // {% use 'foo.html.twig' %}
                    if(psiElement.getNode().getElementType() == TwigElementTypes.TAG) {
                        psiElement.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
                            @Override
                            public void visitElement(PsiElement element) {
                                if(TwigPattern.getTwigTagUseNamePattern().accepts(element) && PsiElementUtils.getParentOfType(element, TwigElementTypes.EMBED_STATEMENT) == null) {
                                    String templateName = TwigUtil.normalizeTemplateName(PsiElementUtils.trimQuote(element.getText()));
                                    if(StringUtils.isNotBlank(templateName)) {
                                        blocks.putIfAbsent("use", new HashSet<>());
                                        blocks.get("use").add(templateName);
                                    }
                                }

                                super.visitElement(element);
                            }
                        });
                    }
                }
            }
        }

        return blocks;
    };
}