Search in sources :

Example 1 with ElementFilter

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

the class JavaReferenceContributor method registerReferenceProviders.

public void registerReferenceProviders(@NotNull final PsiReferenceRegistrar registrar) {
    final JavaClassListReferenceProvider classListProvider = new JavaClassListReferenceProvider();
    registrar.registerReferenceProvider(xmlAttributeValue(), classListProvider, PsiReferenceRegistrar.LOWER_PRIORITY);
    registrar.registerReferenceProvider(xmlTag(), classListProvider, PsiReferenceRegistrar.LOWER_PRIORITY);
    final PsiReferenceProvider filePathReferenceProvider = new FilePathReferenceProvider();
    registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiLiteralExpression.class).and(new FilterPattern(new ElementFilter() {

        public boolean isAcceptable(Object element, PsiElement context) {
            PsiLiteralExpression literalExpression = (PsiLiteralExpression) context;
            final Map<String, Object> annotationParams = new HashMap<>();
            annotationParams.put(AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER, null);
            return !JavaI18nUtil.mustBePropertyKey(literalExpression, annotationParams);
        }

        public boolean isClassAcceptable(Class hintClass) {
            return true;
        }
    })), filePathReferenceProvider);
    registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiDocToken.class), CommentsReferenceContributor.COMMENTS_REFERENCE_PROVIDER_TYPE.getProvider());
}
Also used : FilterPattern(com.intellij.psi.filters.position.FilterPattern) ElementFilter(com.intellij.psi.filters.ElementFilter) PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ElementFilter

use of com.intellij.psi.filters.ElementFilter 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 3 with ElementFilter

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

the class JavaSmartCompletionContributor method completeReference.

static Set<LookupElement> completeReference(final PsiElement element, PsiJavaCodeReferenceElement reference, final ElementFilter filter, final boolean acceptClasses, final boolean acceptMembers, CompletionParameters parameters, final PrefixMatcher matcher) {
    ElementFilter checkClass = new ElementFilter() {

        @Override
        public boolean isAcceptable(Object element, PsiElement context) {
            return filter.isAcceptable(element, context);
        }

        @Override
        public boolean isClassAcceptable(Class hintClass) {
            if (ReflectionUtil.isAssignable(PsiClass.class, hintClass)) {
                return acceptClasses;
            }
            if (ReflectionUtil.isAssignable(PsiVariable.class, hintClass) || ReflectionUtil.isAssignable(PsiMethod.class, hintClass) || ReflectionUtil.isAssignable(CandidateInfo.class, hintClass)) {
                return acceptMembers;
            }
            return false;
        }
    };
    JavaCompletionProcessor.Options options = JavaCompletionProcessor.Options.DEFAULT_OPTIONS.withFilterStaticAfterInstance(parameters.getInvocationCount() <= 1);
    return JavaCompletionUtil.processJavaReference(element, reference, checkClass, options, matcher, parameters);
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) ElementFilter(com.intellij.psi.filters.ElementFilter) JavaCompletionProcessor(com.intellij.codeInsight.completion.scope.JavaCompletionProcessor)

Example 4 with ElementFilter

use of com.intellij.psi.filters.ElementFilter in project intellij-plugins by JetBrains.

the class DartPackagePathReferenceProvider method getFilter.

public static ElementFilter getFilter() {
    return new ElementFilter() {

        @Override
        public boolean isAcceptable(Object _element, PsiElement context) {
            if (!(_element instanceof PsiElement))
                return false;
            final PsiElement element = (PsiElement) _element;
            final PsiElement parentElement = element.getParent();
            final PsiFile file = element.getContainingFile().getOriginalFile();
            final VirtualFile vFile = file.getVirtualFile();
            return vFile != null && HtmlUtil.hasHtml(file) && parentElement instanceof XmlAttribute && canContainDartPackageReference(((XmlAttribute) parentElement).getParent().getLocalName(), ((XmlAttribute) parentElement).getName()) && PubspecYamlUtil.findPubspecYamlFile(element.getProject(), vFile) != null;
        }

        @Override
        public boolean isClassAcceptable(Class hintClass) {
            return true;
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ElementFilter(com.intellij.psi.filters.ElementFilter)

Example 5 with ElementFilter

use of com.intellij.psi.filters.ElementFilter in project intellij-plugins by JetBrains.

the class AngularJSReferencesContributor method moduleDependencyPattern.

private static PsiElementPattern.Capture<JSLiteralExpression> moduleDependencyPattern() {
    return PlatformPatterns.psiElement(JSLiteralExpression.class).and(new FilterPattern(new ElementFilter() {

        @Override
        public boolean isAcceptable(Object element, @Nullable PsiElement context) {
            if (element instanceof JSLiteralExpression) {
                PsiElement parent = ((PsiElement) element).getParent();
                if (!(parent instanceof JSArrayLiteralExpression))
                    return false;
                parent = parent.getParent();
                if (parent instanceof JSArgumentList && parent.getParent() instanceof JSCallExpression && ((JSArgumentList) parent).getArguments().length > 1) {
                    if (PsiTreeUtil.isAncestor(((JSArgumentList) parent).getArguments()[1], (PsiElement) element, false) && ((JSArgumentList) parent).getArguments()[1] instanceof JSArrayLiteralExpression) {
                        final JSExpression methodExpression = ((JSCallExpression) parent.getParent()).getMethodExpression();
                        if (looksLikeAngularModuleReference(methodExpression))
                            return true;
                    }
                }
            }
            return false;
        }

        @Override
        public boolean isClassAcceptable(Class hintClass) {
            return true;
        }
    }));
}
Also used : FilterPattern(com.intellij.psi.filters.position.FilterPattern) ElementFilter(com.intellij.psi.filters.ElementFilter) Nullable(org.jetbrains.annotations.Nullable) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Aggregations

ElementFilter (com.intellij.psi.filters.ElementFilter)15 FilterPattern (com.intellij.psi.filters.position.FilterPattern)6 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 CssClassValueReference (com.intellij.javascript.flex.css.CssClassValueReference)2 Module (com.intellij.openapi.module.Module)2 ModuleUtilCore.findModuleForPsiElement (com.intellij.openapi.module.ModuleUtilCore.findModuleForPsiElement)2 PsiElement (com.intellij.psi.PsiElement)2 ClassFilter (com.intellij.psi.filters.ClassFilter)2 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 XmlTag (com.intellij.psi.xml.XmlTag)2 ProcessingContext (com.intellij.util.ProcessingContext)2 ExpectedTypeInfo (com.intellij.codeInsight.ExpectedTypeInfo)1 JavaCompletionProcessor (com.intellij.codeInsight.completion.scope.JavaCompletionProcessor)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 LocalQuickFixProvider (com.intellij.codeInspection.LocalQuickFixProvider)1 CssPropertyValueReference (com.intellij.javascript.flex.css.CssPropertyValueReference)1 CreateFlexComponentFix (com.intellij.lang.javascript.flex.actions.newfile.CreateFlexComponentFix)1