Search in sources :

Example 1 with ClassFilter

use of com.intellij.psi.filters.ClassFilter in project intellij-community by JetBrains.

the class XmlElementDescriptorImpl method doCollectAttlistDeclarations.

private static XmlAttlistDecl[] doCollectAttlistDeclarations(XmlElement xmlElement) {
    final List<XmlAttlistDecl> result = new ArrayList<>();
    XmlUtil.processXmlElements(xmlElement, new FilterElementProcessor(new ClassFilter(XmlAttlistDecl.class), result), false, false, XmlUtil.getContainingFile(xmlElement));
    return result.toArray(new XmlAttlistDecl[result.size()]);
}
Also used : FilterElementProcessor(com.intellij.psi.scope.processor.FilterElementProcessor) ClassFilter(com.intellij.psi.filters.ClassFilter) ArrayList(java.util.ArrayList)

Example 2 with ClassFilter

use of com.intellij.psi.filters.ClassFilter in project intellij-community by JetBrains.

the class XmlNSDescriptorImpl method doBuildDeclarationMap.

// Read-only calculation
private CachedValue<Map<String, XmlElementDescriptor>> doBuildDeclarationMap() {
    return CachedValuesManager.getManager(myElement.getProject()).createCachedValue(() -> {
        final List<XmlElementDecl> result = new ArrayList<>();
        myElement.processElements(new FilterElementProcessor(new ClassFilter(XmlElementDecl.class), result), getDeclaration());
        final Map<String, XmlElementDescriptor> ret = new LinkedHashMap<>((int) (result.size() * 1.5));
        Set<PsiFile> dependencies = new THashSet<>(1);
        dependencies.add(myDescriptorFile);
        for (final XmlElementDecl xmlElementDecl : result) {
            final String name = xmlElementDecl.getName();
            if (name != null) {
                if (!ret.containsKey(name)) {
                    ret.put(name, new XmlElementDescriptorImpl(xmlElementDecl));
                    // if element descriptor was produced from entity reference use proper dependency
                    PsiElement dependingElement = xmlElementDecl.getUserData(XmlElement.DEPENDING_ELEMENT);
                    if (dependingElement != null) {
                        PsiFile dependingElementContainingFile = dependingElement.getContainingFile();
                        if (dependingElementContainingFile != null)
                            dependencies.add(dependingElementContainingFile);
                    }
                }
            }
        }
        return new CachedValueProvider.Result<>(ret, dependencies.toArray());
    }, false);
}
Also used : THashSet(gnu.trove.THashSet) FilterElementProcessor(com.intellij.psi.scope.processor.FilterElementProcessor) ClassFilter(com.intellij.psi.filters.ClassFilter) PsiFile(com.intellij.psi.PsiFile) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement)

Example 3 with ClassFilter

use of com.intellij.psi.filters.ClassFilter in project intellij-community by JetBrains.

the class JavaClassNameCompletionContributor method addAllClasses.

public static void addAllClasses(@NotNull CompletionParameters parameters, final boolean filterByScope, @NotNull final PrefixMatcher matcher, @NotNull final Consumer<LookupElement> consumer) {
    final PsiElement insertedElement = parameters.getPosition();
    if (JavaCompletionContributor.ANNOTATION_NAME.accepts(insertedElement)) {
        MultiMap<String, PsiClass> annoMap = getAllAnnotationClasses(insertedElement, matcher);
        Processor<PsiClass> processor = new LimitedAccessibleClassPreprocessor(parameters, filterByScope, anno -> {
            JavaPsiClassReferenceElement item = AllClassesGetter.createLookupItem(anno, JAVA_CLASS_INSERT_HANDLER);
            item.addLookupStrings(getClassNameWithContainers(anno));
            consumer.consume(item);
        });
        for (String name : CompletionUtil.sortMatching(matcher, annoMap.keySet())) {
            if (!ContainerUtil.process(annoMap.get(name), processor))
                break;
        }
        return;
    }
    final ElementFilter filter = IN_EXTENDS_IMPLEMENTS.accepts(insertedElement) ? new ExcludeDeclaredFilter(new ClassFilter(PsiClass.class)) : IN_TYPE_PARAMETER.accepts(insertedElement) ? new ExcludeDeclaredFilter(new ClassFilter(PsiTypeParameter.class)) : TrueFilter.INSTANCE;
    final boolean inJavaContext = parameters.getPosition() instanceof PsiIdentifier;
    final boolean afterNew = AFTER_NEW.accepts(insertedElement);
    if (afterNew) {
        final PsiExpression expr = PsiTreeUtil.getContextOfType(insertedElement, PsiExpression.class, true);
        for (final ExpectedTypeInfo info : ExpectedTypesProvider.getExpectedTypes(expr, true)) {
            final PsiType type = info.getType();
            final PsiClass psiClass = PsiUtil.resolveClassInType(type);
            if (psiClass != null && psiClass.getName() != null) {
                consumer.consume(createClassLookupItem(psiClass, inJavaContext));
            }
            final PsiType defaultType = info.getDefaultType();
            if (!defaultType.equals(type)) {
                final PsiClass defClass = PsiUtil.resolveClassInType(defaultType);
                if (defClass != null && defClass.getName() != null) {
                    consumer.consume(createClassLookupItem(defClass, true));
                }
            }
        }
    }
    final boolean pkgContext = JavaCompletionUtil.inSomePackage(insertedElement);
    AllClassesGetter.processJavaClasses(parameters, matcher, filterByScope, new Consumer<PsiClass>() {

        @Override
        public void consume(PsiClass psiClass) {
            processClass(psiClass, null, "");
        }

        private void processClass(PsiClass psiClass, @Nullable Set<PsiClass> visited, String prefix) {
            boolean isInnerClass = StringUtil.isNotEmpty(prefix);
            if (isInnerClass && isProcessedIndependently(psiClass)) {
                return;
            }
            if (filter.isAcceptable(psiClass, insertedElement)) {
                if (!inJavaContext) {
                    JavaPsiClassReferenceElement element = AllClassesGetter.createLookupItem(psiClass, AllClassesGetter.TRY_SHORTENING);
                    element.setLookupString(prefix + element.getLookupString());
                    consumer.consume(element);
                } else {
                    Condition<PsiClass> condition = eachClass -> filter.isAcceptable(eachClass, insertedElement) && AllClassesGetter.isAcceptableInContext(insertedElement, eachClass, filterByScope, pkgContext);
                    for (JavaPsiClassReferenceElement element : createClassLookupItems(psiClass, afterNew, JAVA_CLASS_INSERT_HANDLER, condition)) {
                        element.setLookupString(prefix + element.getLookupString());
                        JavaConstructorCallElement.wrap(element, insertedElement).forEach(consumer::consume);
                    }
                }
            } else {
                String name = psiClass.getName();
                if (name != null) {
                    PsiClass[] innerClasses = psiClass.getInnerClasses();
                    if (innerClasses.length > 0) {
                        if (visited == null)
                            visited = ContainerUtil.newHashSet();
                        for (PsiClass innerClass : innerClasses) {
                            if (visited.add(innerClass)) {
                                processClass(innerClass, visited, prefix + name + ".");
                            }
                        }
                    }
                }
            }
        }

        private boolean isProcessedIndependently(PsiClass psiClass) {
            String innerName = psiClass.getName();
            return innerName != null && matcher.prefixMatches(innerName);
        }
    });
}
Also used : Condition(com.intellij.openapi.util.Condition) ExcludeDeclaredFilter(com.intellij.psi.filters.element.ExcludeDeclaredFilter) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) ClassFilter(com.intellij.psi.filters.ClassFilter) ElementFilter(com.intellij.psi.filters.ElementFilter)

Example 4 with ClassFilter

use of com.intellij.psi.filters.ClassFilter in project intellij-community by JetBrains.

the class PsiImportStaticReferenceElementImpl method processVariants.

@Override
public void processVariants(@NotNull PsiScopeProcessor processor) {
    FilterScopeProcessor proc = new FilterScopeProcessor(new ClassFilter(PsiModifierListOwner.class), processor);
    PsiScopesUtil.resolveAndWalk(proc, this, null, true);
}
Also used : ClassFilter(com.intellij.psi.filters.ClassFilter) FilterScopeProcessor(com.intellij.psi.scope.processor.FilterScopeProcessor)

Example 5 with ClassFilter

use of com.intellij.psi.filters.ClassFilter in project intellij-community by JetBrains.

the class ApplicationLoader method registerMetaData.

private static void registerMetaData() {
    final MetaDataRegistrar registrar = MetaDataRegistrar.getInstance();
    registrar.registerMetaData(new AndFilter(new NamespaceFilter(RNG_NAMESPACE), new ClassFilter(XmlDocument.class)), RngNsDescriptor.class);
    registrar.registerMetaData(new ClassFilter(RncDocument.class), RngNsDescriptor.class);
    registrar.registerMetaData(new ElementFilter() {

        @Override
        public boolean isAcceptable(Object element, PsiElement context) {
            if (element instanceof XmlTag) {
                final XmlTag tag = (XmlTag) element;
                final DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
                return domElement instanceof RngDefine;
            }
            return false;
        }

        @Override
        public boolean isClassAcceptable(Class hintClass) {
            return XmlTag.class.isAssignableFrom(hintClass);
        }
    }, RngDefineMetaData.class);
}
Also used : AndFilter(com.intellij.psi.filters.AndFilter) RngDefine(org.intellij.plugins.relaxNG.xml.dom.RngDefine) DomElement(com.intellij.util.xml.DomElement) NamespaceFilter(com.intellij.psi.filters.position.NamespaceFilter) MetaDataRegistrar(com.intellij.psi.meta.MetaDataRegistrar) ClassFilter(com.intellij.psi.filters.ClassFilter) ElementFilter(com.intellij.psi.filters.ElementFilter) PsiElement(com.intellij.psi.PsiElement) RncDocument(org.intellij.plugins.relaxNG.compact.psi.impl.RncDocument) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

ClassFilter (com.intellij.psi.filters.ClassFilter)6 PsiElement (com.intellij.psi.PsiElement)3 FilterElementProcessor (com.intellij.psi.scope.processor.FilterElementProcessor)3 ElementFilter (com.intellij.psi.filters.ElementFilter)2 ArrayList (java.util.ArrayList)2 ExpectedTypeInfo (com.intellij.codeInsight.ExpectedTypeInfo)1 Condition (com.intellij.openapi.util.Condition)1 PsiFile (com.intellij.psi.PsiFile)1 AndFilter (com.intellij.psi.filters.AndFilter)1 ExcludeDeclaredFilter (com.intellij.psi.filters.element.ExcludeDeclaredFilter)1 NamespaceFilter (com.intellij.psi.filters.position.NamespaceFilter)1 MetaDataRegistrar (com.intellij.psi.meta.MetaDataRegistrar)1 FilterScopeProcessor (com.intellij.psi.scope.processor.FilterScopeProcessor)1 XmlTag (com.intellij.psi.xml.XmlTag)1 DomElement (com.intellij.util.xml.DomElement)1 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)1 THashSet (gnu.trove.THashSet)1 RncDocument (org.intellij.plugins.relaxNG.compact.psi.impl.RncDocument)1 RngDefine (org.intellij.plugins.relaxNG.xml.dom.RngDefine)1