Search in sources :

Example 6 with FilterPattern

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

the class XmlUtil method registerXmlAttributeValueReferenceProvider.

public static void registerXmlAttributeValueReferenceProvider(PsiReferenceRegistrar registrar, @Nullable @NonNls String[] attributeNames, @Nullable ElementFilter elementFilter, boolean caseSensitive, @NotNull PsiReferenceProvider provider, double priority) {
    if (attributeNames == null) {
        registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue().and(new FilterPattern(elementFilter)), provider, priority);
        return;
    }
    final StringPattern namePattern = caseSensitive ? StandardPatterns.string().oneOf(attributeNames) : StandardPatterns.string().oneOfIgnoreCase(attributeNames);
    registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue().withLocalName(namePattern).and(new FilterPattern(elementFilter)), provider, priority);
}
Also used : FilterPattern(com.intellij.psi.filters.position.FilterPattern) StringPattern(com.intellij.patterns.StringPattern)

Example 7 with FilterPattern

use of com.intellij.psi.filters.position.FilterPattern 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 8 with FilterPattern

use of com.intellij.psi.filters.position.FilterPattern 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 9 with FilterPattern

use of com.intellij.psi.filters.position.FilterPattern 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

FilterPattern (com.intellij.psi.filters.position.FilterPattern)9 ElementFilter (com.intellij.psi.filters.ElementFilter)6 NotNull (org.jetbrains.annotations.NotNull)4 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)2 LocalQuickFixProvider (com.intellij.codeInspection.LocalQuickFixProvider)2 CssClassValueReference (com.intellij.javascript.flex.css.CssClassValueReference)2 CreateFlexComponentFix (com.intellij.lang.javascript.flex.actions.newfile.CreateFlexComponentFix)2 JSAttributeNameValuePair (com.intellij.lang.javascript.psi.ecmal4.JSAttributeNameValuePair)2 JSReferenceSet (com.intellij.lang.javascript.psi.impl.JSReferenceSet)2 ActionScriptCreateClassOrInterfaceFix (com.intellij.lang.javascript.validation.fixes.ActionScriptCreateClassOrInterfaceFix)2 Module (com.intellij.openapi.module.Module)2 ModuleUtilCore.findModuleForPsiElement (com.intellij.openapi.module.ModuleUtilCore.findModuleForPsiElement)2 StringPattern (com.intellij.patterns.StringPattern)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 Nullable (org.jetbrains.annotations.Nullable)2 CssPropertyValueReference (com.intellij.javascript.flex.css.CssPropertyValueReference)1 FlexCssPropertyDescriptor (com.intellij.javascript.flex.css.FlexCssPropertyDescriptor)1 MxmlJSClass (com.intellij.javascript.flex.mxml.MxmlJSClass)1 AnnotationBackedDescriptorImpl (com.intellij.javascript.flex.mxml.schema.AnnotationBackedDescriptorImpl)1 CodeContext (com.intellij.javascript.flex.mxml.schema.CodeContext)1