Search in sources :

Example 11 with ElementFilter

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

Example 12 with ElementFilter

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

the class AngularJSReferencesContributor method xmlAttributePattern.

private static PsiElementPattern.Capture<XmlAttributeValue> xmlAttributePattern(@NotNull final String attributeName) {
    return PlatformPatterns.psiElement(XmlAttributeValue.class).and(new FilterPattern(new ElementFilter() {

        @Override
        public boolean isAcceptable(Object element, @Nullable PsiElement context) {
            final XmlAttributeValue attributeValue = (XmlAttributeValue) element;
            final PsiElement parent = attributeValue.getParent();
            if (parent instanceof XmlAttribute && attributeName.equals(((XmlAttribute) parent).getName())) {
                return AngularIndexUtil.hasAngularJS(attributeValue.getProject());
            }
            return false;
        }

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

Example 13 with ElementFilter

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

the class AngularJSReferencesContributor method uiViewPattern.

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

        @Override
        public boolean isAcceptable(Object element, @Nullable PsiElement context) {
            if (!(element instanceof PsiElement))
                return false;
            if (element instanceof JSLiteralExpression || element instanceof LeafPsiElement && ((LeafPsiElement) element).getNode().getElementType() == JSTokenTypes.STRING_LITERAL) {
                if (!(((PsiElement) element).getParent() instanceof JSProperty))
                    return false;
                // started typing property, variant
                PsiElement current = moveUpChain((PsiElement) element, JSLiteralExpression.class, JSReferenceExpression.class, JSProperty.class);
                if (!(current instanceof JSProperty) || !acceptablePropertyValue((JSProperty) current))
                    return false;
                current = current.getParent();
                if (current != null && checkParentViewsObject(current))
                    return AngularIndexUtil.hasAngularJS(current.getProject());
            }
            return false;
        }

        private boolean acceptablePropertyValue(JSProperty element) {
            return element.getNameIdentifier() != null && StringUtil.isQuotedString(element.getNameIdentifier().getText()) && (element.getValue() instanceof JSObjectLiteralExpression || element.getValue() instanceof JSReferenceExpression || element.getValue() == null);
        }

        private PsiElement moveUpChain(@Nullable final PsiElement element, @NotNull final Class<? extends PsiElement>... clazz) {
            PsiElement current = element;
            for (Class<? extends PsiElement> aClass : clazz) {
                current = current != null && aClass.isInstance(current.getParent()) ? current.getParent() : current;
            }
            return current;
        }

        private boolean checkParentViewsObject(final PsiElement mustBeObject) {
            if (mustBeObject instanceof JSObjectLiteralExpression) {
                final PsiElement viewsProperty = mustBeObject.getParent();
                if (viewsProperty instanceof JSProperty && "views".equals(((JSProperty) viewsProperty).getName())) {
                    // by now will not go further todo other cases
                    return true;
                }
            }
            return false;
        }

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

Example 14 with ElementFilter

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

the class DartPackagePathReferenceContributor method registerReferenceProviders.

@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
    DartPackagePathReferenceProvider provider = new DartPackagePathReferenceProvider();
    String[] htmlAttrs = new String[] { "href", "src" };
    ElementFilter htmlFilter = DartPackagePathReferenceProvider.getFilter();
    XmlUtil.registerXmlAttributeValueReferenceProvider(registrar, htmlAttrs, htmlFilter, false, provider, PsiReferenceRegistrar.HIGHER_PRIORITY);
}
Also used : ElementFilter(com.intellij.psi.filters.ElementFilter)

Example 15 with ElementFilter

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

the class ActionScriptReferenceContributor method registerReferenceProviders.

@Override
public void registerReferenceProviders(@NotNull final PsiReferenceRegistrar registrar) {
    registrar.registerReferenceProvider(psiElement(JSLiteralExpression.class).and(new FilterPattern(new ElementFilter() {

        public boolean isAcceptable(Object element, PsiElement context) {
            PsiElement parent = ((JSLiteralExpression) element).getParent();
            if (parent instanceof JSArgumentList) {
                JSExpression[] arguments = ((JSArgumentList) parent).getArguments();
                if (arguments.length > 0 && arguments[0] == element) {
                    parent = parent.getParent();
                    if (parent instanceof JSCallExpression) {
                        JSExpression invokedMethod = ((JSCallExpression) parent).getMethodExpression();
                        if (invokedMethod instanceof JSReferenceExpression) {
                            String methodName = ((JSReferenceExpression) invokedMethod).getReferencedName();
                            if (SET_STYLE_METHOD_NAME.equals(methodName)) {
                                Module module = findModuleForPsiElement(parent);
                                return module != null && ModuleType.get(module) == FlexModuleType.getInstance();
                            }
                        }
                    }
                }
            }
            return false;
        }

        public boolean isClassAcceptable(Class hintClass) {
            return true;
        }
    })), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            String value = element.getText();
            if (FlexCssUtil.inQuotes(value)) {
                return new PsiReference[] { new CssPropertyValueReference(element) };
            }
            return PsiReference.EMPTY_ARRAY;
        }
    });
    registrar.registerReferenceProvider(psiElement(JSLiteralExpression.class).and(new FilterPattern(new ElementFilter() {

        public boolean isAcceptable(Object element, PsiElement context) {
            PsiElement parent = ((JSLiteralExpression) element).getParent();
            if (parent instanceof JSAssignmentExpression) {
                PsiElement assignee = parent.getChildren()[0];
                if (assignee instanceof JSDefinitionExpression) {
                    JSExpression expression = ((JSDefinitionExpression) assignee).getExpression();
                    if (expression instanceof JSReferenceExpression) {
                        String refName = ((JSReferenceExpression) expression).getReferencedName();
                        if (refName != null && FlexCssUtil.isStyleNameProperty(refName)) {
                            Module module = findModuleForPsiElement(parent);
                            return module != null && ModuleType.get(module) == FlexModuleType.getInstance();
                        }
                    }
                }
            }
            return false;
        }

        public boolean isClassAcceptable(Class hintClass) {
            return true;
        }
    })), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            String value = element.getText();
            if (FlexCssUtil.inQuotes(value)) {
                return new PsiReference[] { new CssClassValueReference(element) };
            }
            return PsiReference.EMPTY_ARRAY;
        }
    });
    registrar.registerReferenceProvider(psiElement(JSLiteralExpression.class).and(new FilterPattern(new ElementFilter() {

        public boolean isAcceptable(Object element, PsiElement context) {
            PsiElement parent = ((JSLiteralExpression) element).getParent();
            if (parent instanceof JSArgumentList) {
                final JSExpression[] arguments = ((JSArgumentList) parent).getArguments();
                if (arguments.length > 0 && arguments[0] == element) {
                    parent = parent.getParent();
                    if (parent instanceof JSCallExpression) {
                        final JSExpression invokedMethod = ((JSCallExpression) parent).getMethodExpression();
                        if (invokedMethod instanceof JSReferenceExpression) {
                            final String methodName = ((JSReferenceExpression) invokedMethod).getReferencedName();
                            if (methodName != null && FlexCssUtil.isStyleNameMethod(methodName)) {
                                Module module = findModuleForPsiElement(parent);
                                return module != null && ModuleType.get(module) == FlexModuleType.getInstance();
                            }
                        }
                    }
                }
            }
            return false;
        }

        public boolean isClassAcceptable(Class hintClass) {
            return true;
        }
    })), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            String value = element.getText();
            if (FlexCssUtil.inQuotes(value)) {
                return new PsiReference[] { new CssClassValueReference(element) };
            }
            return PsiReference.EMPTY_ARRAY;
        }
    });
    registrar.registerReferenceProvider(psiElement(JSLiteralExpression.class), new FlexPropertyReferenceProvider());
    registrar.registerReferenceProvider(psiElement(JSAttributeNameValuePair.class), new FlexAttributeReferenceProvider());
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) CssClassValueReference(com.intellij.javascript.flex.css.CssClassValueReference) NotNull(org.jetbrains.annotations.NotNull) FilterPattern(com.intellij.psi.filters.position.FilterPattern) CssPropertyValueReference(com.intellij.javascript.flex.css.CssPropertyValueReference) ElementFilter(com.intellij.psi.filters.ElementFilter) JSAttributeNameValuePair(com.intellij.lang.javascript.psi.ecmal4.JSAttributeNameValuePair) Module(com.intellij.openapi.module.Module) ModuleUtilCore.findModuleForPsiElement(com.intellij.openapi.module.ModuleUtilCore.findModuleForPsiElement)

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