Search in sources :

Example 1 with CssClassValueReference

use of com.intellij.javascript.flex.css.CssClassValueReference in project intellij-plugins by JetBrains.

the class FlexCssReferenceContributor method registerReferenceProviders.

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

        public boolean isAcceptable(Object element, PsiElement context) {
            CssFunction fun = PsiTreeUtil.getParentOfType((PsiElement) element, CssFunction.class);
            String funName;
            return fun != null && (FlexReferenceContributor.CLASS_REFERENCE.equals(funName = fun.getName()) || "Embed".equals(funName));
        }

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

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull ProcessingContext context) {
            CssFunction fun = PsiTreeUtil.getParentOfType(element, CssFunction.class);
            if (fun != null && "Embed".equals(fun.getName())) {
                // TODO: remove this stuff once css function will have proper psi
                PsiElement prev = PsiTreeUtil.prevLeaf(element);
                if (prev instanceof PsiWhiteSpace)
                    prev = PsiTreeUtil.prevLeaf(prev);
                if (prev != null)
                    prev = PsiTreeUtil.prevLeaf(prev);
                if (prev instanceof PsiWhiteSpace)
                    prev = PsiTreeUtil.prevLeaf(prev);
                // prev.getText() == Embed if element is the first parameter and the name not specified
                if (prev != null && !FlexReferenceContributor.SOURCE_ATTR_NAME.equals(prev.getText()) && !"Embed".equals(prev.getText())) {
                    return PsiReference.EMPTY_ARRAY;
                }
                return ReferenceSupport.getFileRefs(element, element, 1, ReferenceSupport.LookupOptions.EMBEDDED_ASSET);
            }
            final String value = StringUtil.unquoteString(element.getText());
            JSReferenceSet refSet = new JSReferenceSet(element, value, 1, false, true);
            if (fun != null && element instanceof CssString) {
                assert FlexReferenceContributor.CLASS_REFERENCE.equals(fun.getName());
                refSet.setLocalQuickFixProvider(new LocalQuickFixProvider() {

                    @Nullable
                    @Override
                    public LocalQuickFix[] getQuickFixes() {
                        if (!JSUtils.isValidClassName(value, true)) {
                            return LocalQuickFix.EMPTY_ARRAY;
                        }
                        ActionScriptCreateClassOrInterfaceFix[] fixes = new ActionScriptCreateClassOrInterfaceFix[] { new ActionScriptCreateClassOrInterfaceFix(value, null, element), new CreateFlexComponentFix(value, element) };
                        for (ActionScriptCreateClassOrInterfaceFix fix : fixes) {
                            fix.setCreatedClassFqnConsumer(newFqn -> ElementManipulators.getManipulator(element).handleContentChange(element, newFqn));
                        }
                        return fixes;
                    }
                });
            }
            return refSet.getReferences();
        }
    });
    registrar.registerReferenceProvider(PlatformPatterns.psiElement().and(new FilterPattern(new ElementFilter() {

        public boolean isAcceptable(Object element, PsiElement context) {
            if (element instanceof CssTokenImpl || element instanceof CssString) {
                CssTermList cssTermList = PsiTreeUtil.getParentOfType((PsiElement) element, CssTermList.class);
                if (cssTermList != null) {
                    CssDeclaration cssDeclaration = PsiTreeUtil.getParentOfType(cssTermList, CssDeclaration.class);
                    if (cssDeclaration != null && cssDeclaration.getValue() == cssTermList) {
                        if (FlexCssUtil.isStyleNameProperty(cssDeclaration.getPropertyName())) {
                            PsiFile file = cssDeclaration.getContainingFile();
                            if (file != null) {
                                if (file.getFileType() == CssFileType.INSTANCE) {
                                    Module module = findModuleForPsiElement(cssDeclaration);
                                    return module != null && ModuleType.get(module) == FlexModuleType.getInstance();
                                }
                                return JavaScriptSupportLoader.isFlexMxmFile(file);
                            }
                        }
                    }
                }
            }
            return false;
        }

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

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            String styleName = CssClassValueReference.getValue(element);
            if (styleName.length() > 0) {
                return new PsiReference[] { new CssClassValueReference(element) };
            }
            return PsiReference.EMPTY_ARRAY;
        }
    });
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) CssTokenImpl(com.intellij.psi.css.impl.CssTokenImpl) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) NotNull(org.jetbrains.annotations.NotNull) LocalQuickFixProvider(com.intellij.codeInspection.LocalQuickFixProvider) FilterPattern(com.intellij.psi.filters.position.FilterPattern) ActionScriptCreateClassOrInterfaceFix(com.intellij.lang.javascript.validation.fixes.ActionScriptCreateClassOrInterfaceFix) ModuleUtilCore.findModuleForPsiElement(com.intellij.openapi.module.ModuleUtilCore.findModuleForPsiElement) CreateFlexComponentFix(com.intellij.lang.javascript.flex.actions.newfile.CreateFlexComponentFix) CssClassValueReference(com.intellij.javascript.flex.css.CssClassValueReference) JSReferenceSet(com.intellij.lang.javascript.psi.impl.JSReferenceSet) ElementFilter(com.intellij.psi.filters.ElementFilter) Module(com.intellij.openapi.module.Module)

Example 2 with CssClassValueReference

use of com.intellij.javascript.flex.css.CssClassValueReference 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

CssClassValueReference (com.intellij.javascript.flex.css.CssClassValueReference)2 Module (com.intellij.openapi.module.Module)2 ModuleUtilCore.findModuleForPsiElement (com.intellij.openapi.module.ModuleUtilCore.findModuleForPsiElement)2 ElementFilter (com.intellij.psi.filters.ElementFilter)2 FilterPattern (com.intellij.psi.filters.position.FilterPattern)2 ProcessingContext (com.intellij.util.ProcessingContext)2 NotNull (org.jetbrains.annotations.NotNull)2 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 JSAttributeNameValuePair (com.intellij.lang.javascript.psi.ecmal4.JSAttributeNameValuePair)1 JSReferenceSet (com.intellij.lang.javascript.psi.impl.JSReferenceSet)1 ActionScriptCreateClassOrInterfaceFix (com.intellij.lang.javascript.validation.fixes.ActionScriptCreateClassOrInterfaceFix)1 CssTokenImpl (com.intellij.psi.css.impl.CssTokenImpl)1