Search in sources :

Example 1 with TestNGSearchScope

use of com.theoryinpractice.testng.inspection.TestNGSearchScope in project intellij-community by JetBrains.

the class TestNGRelatedFilesProvider method getItems.

@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement context) {
    PsiClass psiClass = PsiTreeUtil.getParentOfType(context, PsiClass.class, false);
    if (psiClass != null) {
        final Project project = psiClass.getProject();
        while (psiClass != null && TestNGUtil.hasTest(psiClass) && PsiClassUtil.isRunnableClass(psiClass, true)) {
            final String qName = psiClass.getQualifiedName();
            if (qName != null) {
                final String packageQName = ((PsiJavaFile) psiClass.getContainingFile()).getPackageName();
                final String packageName = StringUtil.getShortName(packageQName);
                final String[] names;
                if (packageQName.length() > 0) {
                    final String pName = packageName.length() > 0 ? packageName : packageQName;
                    names = new String[] { qName, pName };
                } else {
                    names = new String[] { qName };
                }
                final List<PsiElement> tags = new ArrayList<>();
                for (final String name : names) {
                    PsiSearchHelper.SERVICE.getInstance(project).processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() {

                        public boolean process(final PsiFile file, final int startOffset, final int endOffset) {
                            final PsiReference referenceAt = file.findReferenceAt(startOffset);
                            if (referenceAt != null) {
                                if (packageQName.endsWith(name)) {
                                    //special package tag required
                                    final XmlTag tag = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), XmlTag.class);
                                    if (tag == null || !tag.getName().equals("package")) {
                                        return true;
                                    }
                                    final XmlAttribute attribute = tag.getAttribute("name");
                                    if (attribute == null)
                                        return true;
                                    final String value = attribute.getValue();
                                    if (value == null)
                                        return true;
                                    if (!(value.equals(StringUtil.getQualifiedName(packageQName, "*")) || value.equals(packageQName)))
                                        return true;
                                }
                                tags.add(referenceAt.getElement());
                            }
                            return true;
                        }
                    }, new TestNGSearchScope(project));
                }
                if (!tags.isEmpty()) {
                    return GotoRelatedItem.createItems(tags, "TestNG");
                }
            }
            psiClass = PsiTreeUtil.getParentOfType(psiClass, PsiClass.class);
        }
    }
    return Collections.emptyList();
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiNonJavaFileReferenceProcessor(com.intellij.psi.search.PsiNonJavaFileReferenceProcessor) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) TestNGSearchScope(com.theoryinpractice.testng.inspection.TestNGSearchScope) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Project (com.intellij.openapi.project.Project)1 PsiNonJavaFileReferenceProcessor (com.intellij.psi.search.PsiNonJavaFileReferenceProcessor)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlTag (com.intellij.psi.xml.XmlTag)1 TestNGSearchScope (com.theoryinpractice.testng.inspection.TestNGSearchScope)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1