Search in sources :

Example 26 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class FlexPropertyReferenceProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    PsiElement parent = element.getParent();
    JSReferenceExpression invokedMethod = JSUtils.getMethodNameIfInsideCall(parent);
    List<PsiReference> result = new ArrayList<>();
    if (invokedMethod != null) {
        String invokedMethodName;
        boolean justResourceBundleRef = false;
        PsiElement qualifier;
        if ((ourMethodsWithPropertyReferences.contains(invokedMethodName = (invokedMethod.getReferencedName())) || (justResourceBundleRef = "getResourceBundle".equals(invokedMethodName))) && ((qualifier = invokedMethod.getQualifier()) instanceof JSReferenceExpression || (qualifier instanceof JSCallExpression && ((JSCallExpression) qualifier).getMethodExpression() instanceof JSReferenceExpression))) {
            final JSExpression[] args = ((JSArgumentList) parent).getArguments();
            boolean propertyRef = false;
            boolean bundleRef = false;
            if (justResourceBundleRef) {
                bundleRef = args.length > 1 && args[1] == element;
            } else {
                propertyRef = args.length > 1 && args[1] == element;
                bundleRef = args.length > 0 && args[0] == element;
                if (bundleRef && args.length == 1) {
                    // ResourceBundle.getString deprecated, without bundle name
                    bundleRef = false;
                    propertyRef = true;
                }
            }
            boolean isSoft = true;
            if (propertyRef || bundleRef) {
                PsiElement resolved = invokedMethod.resolve();
                if (resolved instanceof JSFunction) {
                    PsiElement parentClass = JSResolveUtil.findParent(resolved);
                    if (parentClass instanceof JSClass) {
                        String name = ((JSClass) parentClass).getName();
                        isSoft = name == null || (name.indexOf("ResourceManager") == -1 && name.indexOf("ResourceBundle") == -1);
                    }
                }
            }
            if (propertyRef) {
                FlexPropertiesSupport.PropertyReferenceInfoProvider<JSLiteralExpressionImpl> provider = isSoft ? ourSoftPropertyInfoProvider : ourPropertyInfoProvider;
                if (args.length > 1 && !isSoft) {
                    JSExpression bundleExpression = args[0];
                    if (bundleExpression instanceof JSReferenceExpression) {
                        PsiElement resolved = ((JSReferenceExpression) bundleExpression).resolve();
                        if (resolved instanceof JSVariable) {
                            bundleExpression = ((JSVariable) resolved).getInitializer();
                        }
                    }
                    if (bundleExpression instanceof JSLiteralExpression) {
                        final Object expressionValue = ((JSLiteralExpression) bundleExpression).getValue();
                        if (expressionValue instanceof String) {
                            provider = new FlexPropertiesSupport.PropertyReferenceInfoProvider<JSLiteralExpressionImpl>() {

                                public TextRange getReferenceRange(JSLiteralExpressionImpl element) {
                                    return getValueRange(element);
                                }

                                public String getBundleName(JSLiteralExpressionImpl element) {
                                    return (String) expressionValue;
                                }

                                public boolean isSoft(JSLiteralExpressionImpl element) {
                                    return false;
                                }
                            };
                        }
                    }
                }
                Collections.addAll(result, FlexPropertiesSupport.getPropertyReferences((JSLiteralExpressionImpl) element, provider));
            } else if (bundleRef) {
                PsiReference[] reference = FlexPropertiesSupport.getResourceBundleReference((JSLiteralExpressionImpl) element, isSoft ? ourSoftBundleInfoProvider : ourBundleInfoProvider);
                Collections.addAll(result, reference);
            }
        }
    }
    return result.toArray(new PsiReference[result.size()]);
}
Also used : PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) JSLiteralExpressionImpl(com.intellij.lang.javascript.psi.impl.JSLiteralExpressionImpl) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class FlexCssPropertyDescriptor method getDeclarations.

@NotNull
@Override
public PsiElement[] getDeclarations(@NotNull PsiElement context) {
    Map<PsiElement, PairInfo> navElement2pairInfo = new HashMap<>();
    final Project project = context.getProject();
    GlobalSearchScope scope = FlexCssUtil.getResolveScope(context);
    Set<JSClass> visited = ContainerUtil.newLinkedHashSet();
    for (String className : myClassNames) {
        Collection<JSQualifiedNamedElement> candidates = StubIndex.getElements(JSQualifiedElementIndex.KEY, className.hashCode(), project, scope, JSQualifiedNamedElement.class);
        findStyleAttributes(candidates, visited, navElement2pairInfo);
        // search in MXML files
        PsiElement jsClass = ActionScriptClassResolver.findClassByQNameStatic(className, scope);
        if (jsClass instanceof JSClass) {
            findStyleAttributesInClassOrSuper((JSClass) jsClass, visited, navElement2pairInfo);
        }
    }
    Set<JSFile> visitedFiles = ContainerUtil.newLinkedHashSet();
    for (String fileName : myFileNames) {
        Collection<VirtualFile> files = FilenameIndex.getVirtualFilesByName(project, fileName, scope);
        for (final VirtualFile file : files) {
            PsiFile psiFile = ReadAction.compute(() -> PsiManager.getInstance(project).findFile(file));
            if (psiFile instanceof JSFile) {
                findStyleAttributesInFile((JSFile) psiFile, visitedFiles, navElement2pairInfo);
            }
        }
    }
    Set<PsiElement> navPairs = navElement2pairInfo.keySet();
    Map<String, PsiElement> qName2ResultElement = new HashMap<>();
    for (PsiElement navPair : navPairs) {
        PairInfo pairInfo = navElement2pairInfo.get(navPair);
        String jsClassQName = pairInfo.myJsClassQName;
        PsiElement navPairInOtherClassWithSameQName = jsClassQName != null ? qName2ResultElement.get(jsClassQName) : null;
        if (navPairInOtherClassWithSameQName == null || navPairInOtherClassWithSameQName == navElement2pairInfo.get(navPairInOtherClassWithSameQName) && pairInfo.myPair != navPair) {
            qName2ResultElement.put(jsClassQName, navPair);
        }
    }
    Collection<PsiElement> result = qName2ResultElement.values();
    return PsiUtilCore.toPsiElementArray(result);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(com.intellij.util.containers.HashMap) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiFile(com.intellij.psi.PsiFile) JSFile(com.intellij.lang.javascript.psi.JSFile) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class ActionScriptSmartCompletionContributor method getEventsMap.

public static Map<String, String> getEventsMap(JSClass clazzToProcess) {
    if (clazzToProcess == null)
        return Collections.emptyMap();
    final Map<String, String> eventsMap = new THashMap<>();
    class EventsDataCollector extends ResolveProcessor implements ActionScriptResolveUtil.MetaDataProcessor {

        public EventsDataCollector() {
            super(null);
            setToProcessHierarchy(true);
            setToProcessMembers(false);
            setTypeContext(true);
            setLocalResolve(true);
        }

        @Override
        public boolean process(@NotNull final JSAttribute jsAttribute) {
            if ("Event".equals(jsAttribute.getName())) {
                final JSAttributeNameValuePair eventAttr = jsAttribute.getValueByName("name");
                JSAttributeNameValuePair typeAttr = jsAttribute.getValueByName("type");
                if (eventAttr != null && typeAttr != null) {
                    final String simpleValue = eventAttr.getSimpleValue();
                    if (simpleValue != null) {
                        eventsMap.put(simpleValue, typeAttr.getSimpleValue());
                    }
                }
            }
            return true;
        }

        @Override
        public boolean handleOtherElement(final PsiElement el, final PsiElement context, final Ref<PsiElement> continuePassElement) {
            return true;
        }

        @Override
        public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
            if (element instanceof JSClass) {
                ActionScriptResolveUtil.processMetaAttributesForClass(element, this, true);
            }
            return true;
        }
    }
    final EventsDataCollector eventsDataCollector = new EventsDataCollector();
    if (clazzToProcess instanceof XmlBackedJSClassImpl) {
        XmlFile file = (XmlFile) clazzToProcess.getParent().getContainingFile();
        if (file != null && JavaScriptSupportLoader.isFlexMxmFile(file)) {
            final XmlDocument xmlDocument = file.getDocument();
            final XmlTag rootTag = xmlDocument == null ? null : xmlDocument.getRootTag();
            final XmlTag[] tags = rootTag == null ? XmlTag.EMPTY : MxmlJSClass.findLanguageSubTags(rootTag, FlexPredefinedTagNames.METADATA);
            JSResolveUtil.JSInjectedFilesVisitor injectedFilesVisitor = new JSResolveUtil.JSInjectedFilesVisitor() {

                @Override
                protected void process(JSFile file) {
                    for (PsiElement element : file.getChildren()) {
                        if (element instanceof JSAttributeList) {
                            ActionScriptResolveUtil.processAttributeList(eventsDataCollector, null, (JSAttributeList) element, true, true);
                        }
                    }
                }
            };
            for (XmlTag tag : tags) {
                JSResolveUtil.processInjectedFileForTag(tag, injectedFilesVisitor);
            }
        }
    }
    clazzToProcess.processDeclarations(eventsDataCollector, ResolveState.initial(), clazzToProcess, clazzToProcess);
    return eventsMap;
}
Also used : JSAttributeList(com.intellij.lang.javascript.psi.ecmal4.JSAttributeList) XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument) NotNull(org.jetbrains.annotations.NotNull) ResolveState(com.intellij.psi.ResolveState) Ref(com.intellij.openapi.util.Ref) THashMap(gnu.trove.THashMap) JSAttribute(com.intellij.lang.javascript.psi.ecmal4.JSAttribute) JSAttributeNameValuePair(com.intellij.lang.javascript.psi.ecmal4.JSAttributeNameValuePair) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 29 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class ActionScriptSmartCompletionContributor method processContextClass.

@Override
protected int processContextClass(@NotNull JSReferenceExpression location, JSType expectedType, PsiElement parent, List<Object> variants, int qualifiedStaticVariantsStart, SinkResolveProcessor<?> processor, JSClass ourClass) {
    JSClass clazz = expectedType.resolveClass();
    if (clazz != null && !clazz.isEquivalentTo(ourClass)) {
        qualifiedStaticVariantsStart = processor.getResultSink().getResultCount();
        processStaticsOf(clazz, processor, ourClass);
    }
    if (ourClass != null && clazz != null && JSInheritanceUtil.isParentClass(ourClass, clazz, false) && !JSResolveUtil.calculateStaticFromContext(location) && JSCompletionContributor.getInstance().isDoingSmartCodeCompleteAction()) {
        variants.add(JSLookupUtilImpl.createPrioritizedLookupItem(null, "this", JSLookupPriority.SMART_PRIORITY, true, true));
    }
    if (parent instanceof JSArgumentList) {
        JSParameterItem param = JSResolveUtil.findParameterForUsedArgument(location, (JSArgumentList) parent);
        if (param instanceof JSParameter) {
            PsiElement element = JSResolveUtil.findParent(((JSParameter) param).getParent().getParent());
            if (element instanceof JSClass && !element.isEquivalentTo(ourClass) && !element.isEquivalentTo(clazz)) {
                processStaticsOf((JSClass) element, processor, ourClass);
            }
        }
    }
    return qualifiedStaticVariantsStart;
}
Also used : JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) PsiElement(com.intellij.psi.PsiElement)

Example 30 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class FlexUnitSupport method getCustomRunner.

/**
   * @return [RunWith] metadata default attribute value. Can be <code>null</code>, empty string or whatever.
   */
@Nullable
public static String getCustomRunner(JSClass clazz) {
    final JSAttribute[] attrs = clazz.getAttributeList().getAttributesByName(RUN_WITH_ATTRIBUTE);
    if (attrs.length == 0)
        return null;
    final JSAttributeNameValuePair attr = attrs[0].getValueByName(null);
    return attr == null ? null : attr.getSimpleValue();
}
Also used : JSAttribute(com.intellij.lang.javascript.psi.ecmal4.JSAttribute) JSAttributeNameValuePair(com.intellij.lang.javascript.psi.ecmal4.JSAttributeNameValuePair) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)141 PsiElement (com.intellij.psi.PsiElement)65 Nullable (org.jetbrains.annotations.Nullable)27 MxmlJSClass (com.intellij.javascript.flex.mxml.MxmlJSClass)23 NotNull (org.jetbrains.annotations.NotNull)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)22 JSFunction (com.intellij.lang.javascript.psi.JSFunction)18 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)18 Module (com.intellij.openapi.module.Module)17 XmlFile (com.intellij.psi.xml.XmlFile)17 Project (com.intellij.openapi.project.Project)16 PsiFile (com.intellij.psi.PsiFile)16 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)15 ArrayList (java.util.ArrayList)14 PsiDirectory (com.intellij.psi.PsiDirectory)11 XmlTag (com.intellij.psi.xml.XmlTag)11 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)8 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)8 XmlBackedJSClassImpl (com.intellij.lang.javascript.flex.XmlBackedJSClassImpl)7