Search in sources :

Example 11 with XmlBackedJSClassImpl

use of com.intellij.lang.javascript.flex.XmlBackedJSClassImpl in project intellij-plugins by JetBrains.

the class ClassBackedElementDescriptor method getDefaultPropertyDescriptor.

@Nullable
private AnnotationBackedDescriptor getDefaultPropertyDescriptor(final JSClass jsClass) {
    final Ref<JSAttribute> dpAttributeRef = Ref.create();
    if (jsClass instanceof XmlBackedJSClassImpl) {
        final XmlFile xmlFile = (XmlFile) jsClass.getContainingFile();
        final XmlTag rootTag = xmlFile.getRootTag();
        if (rootTag != null) {
            for (XmlTag metadataTag : rootTag.findSubTags(FlexPredefinedTagNames.METADATA, JavaScriptSupportLoader.MXML_URI3)) {
                JSResolveUtil.processInjectedFileForTag(metadataTag, new JSResolveUtil.JSInjectedFilesVisitor() {

                    @Override
                    protected void process(final JSFile file) {
                        for (PsiElement elt : file.getChildren()) {
                            if (elt instanceof JSAttributeList) {
                                final JSAttribute dpAttribute = ((JSAttributeList) elt).findAttributeByName("DefaultProperty");
                                if (dpAttributeRef.isNull() && dpAttribute != null) {
                                    dpAttributeRef.set(dpAttribute);
                                    return;
                                }
                            }
                        }
                    }
                });
            }
        }
    } else {
        final JSAttributeList attributeList = jsClass.getAttributeList();
        if (attributeList != null) {
            dpAttributeRef.set(attributeList.findAttributeByName("DefaultProperty"));
        }
    }
    final JSAttribute attribute = dpAttributeRef.get();
    if (attribute != null) {
        JSAttributeNameValuePair pair = attribute.getValueByName(null);
        if (pair != null) {
            String s = pair.getSimpleValue();
            if (s != null) {
                getAttributesDescriptors(null);
                XmlAttributeDescriptor descriptor = getAttributeDescriptor(s, null);
                if (descriptor instanceof AnnotationBackedDescriptor) {
                    return (AnnotationBackedDescriptor) descriptor;
                } else {
                    return new AnnotationBackedDescriptorImpl("any", this, true, "NonExistentClass!", null, null);
                }
            }
        }
    }
    return null;
}
Also used : XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) JSResolveUtil(com.intellij.lang.javascript.psi.resolve.JSResolveUtil) AnnotationBackedDescriptor(com.intellij.lang.javascript.flex.AnnotationBackedDescriptor) AnyXmlAttributeDescriptor(com.intellij.xml.impl.schema.AnyXmlAttributeDescriptor) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with XmlBackedJSClassImpl

use of com.intellij.lang.javascript.flex.XmlBackedJSClassImpl in project intellij-plugins by JetBrains.

the class FlexDocumentationProvider method generateDoc.

@Override
public String generateDoc(PsiElement _element, PsiElement originalElement) {
    String doc = super.generateDoc(_element, originalElement);
    if (doc != null) {
        return doc;
    }
    if (_element instanceof JSOffsetBasedImplicitElement)
        _element = ((JSOffsetBasedImplicitElement) _element).getElementAtOffset();
    XmlTag parent = null;
    if (_element instanceof XmlBackedJSClassImpl) {
        parent = ((XmlBackedJSClassImpl) _element).getParent();
    } else if (_element instanceof XmlToken) {
        parent = PsiTreeUtil.getParentOfType(_element, XmlTag.class);
    }
    if (parent != null) {
        PsiElement prev = PsiTreeUtil.prevLeaf(parent);
        while (prev instanceof PsiWhiteSpace || (prev instanceof XmlComment && !prev.getText().startsWith("<!---"))) {
            prev = PsiTreeUtil.prevLeaf(prev);
            if (prev instanceof XmlToken)
                prev = prev.getParent();
        }
        if (prev instanceof XmlComment) {
            return doGetCommentTextFromComment((PsiComment) prev, originalElement);
        }
    }
    final PsiElement elementToShowDoc = findElementToShowDoc(_element);
    AbstractExternalFilter docFilter = new AbstractExternalFilter() {

        private final RefConvertor[] myReferenceConvertors = new RefConvertor[] { new RefConvertor(ourHREFselector) {

            protected String convertReference(String origin, String link) {
                if (BrowserUtil.isAbsoluteURL(link)) {
                    return link;
                }
                String resolved = getSeeAlsoLinkResolved(elementToShowDoc, link);
                if (resolved != null) {
                    return DocumentationManagerProtocol.PSI_ELEMENT_PROTOCOL + resolved;
                }
                String originFile = ourAnchorSuffix.matcher(origin).replaceAll("");
                if (StringUtil.startsWithChar(link, '#')) {
                    return originFile + link;
                } else {
                    String originPath = originFile.contains("/") ? originFile.substring(0, originFile.lastIndexOf("/")) : originFile;
                    return doAnnihilate(originPath + "/" + link);
                }
            }
        }, new RefConvertor(ourIMGselector) {

            protected String convertReference(String root, String href) {
                if (StringUtil.startsWithChar(href, '#')) {
                    return root + href;
                }
                if (root.startsWith("file://") && SystemInfo.isWindows) {
                    root = "file:///" + root.substring("file://".length());
                }
                return doAnnihilate(ourHtmlFileSuffix.matcher(root).replaceAll("/") + href);
            }
        } };

        @Override
        protected AbstractExternalFilter.RefConvertor[] getRefConverters() {
            return myReferenceConvertors;
        }

        @Override
        public String getExternalDocInfoForElement(final String docURL, final PsiElement element) throws Exception {
            String result = super.getExternalDocInfoForElement(docURL, element);
            if (StringUtil.isNotEmpty(result)) {
                result = result.replace(DISPLAY_NAME_MARKER, ApplicationManager.getApplication().runReadAction(new Computable<CharSequence>() {

                    public CharSequence compute() {
                        return getDisplayName(element);
                    }
                }));
            }
            return result;
        }

        @Override
        protected void doBuildFromStream(String url, Reader reader, StringBuilder result) throws IOException {
            String input = StreamUtil.readTextFrom(reader);
            Matcher anchorMatcher = ourAnchorSuffix.matcher(url);
            final int startOffset;
            Pair<Pattern, Pattern> mainContentPatterns = Pair.create(ourOpeningDiv, ourClosingDiv);
            if (anchorMatcher.find()) {
                String name = anchorMatcher.group(1);
                Pattern detailPattern = ourDetailBodyDiv;
                for (Map.Entry<String, String> e : DOCUMENTED_ATTRIBUTES.entrySet()) {
                    if (name.startsWith(e.getValue())) {
                        if (!"Event".equals(e.getKey())) {
                            detailPattern = ourEventDetailTd;
                            mainContentPatterns = Pair.create(ourOpeningTd, ourClosingTd);
                        }
                        break;
                    }
                }
                name = name.replaceAll("\\)", "\\\\)").replaceAll("\\(", "\\\\(");
                Matcher m = Pattern.compile("<a name=\"" + name + "\"").matcher(input);
                if (!m.find()) {
                    return;
                }
                int offset = m.end();
                m = detailPattern.matcher(input);
                if (!m.find(offset)) {
                    return;
                }
                startOffset = m.start();
            } else {
                Matcher m = ourMainContentDiv.matcher(input);
                if (!m.find()) {
                    return;
                }
                startOffset = m.start();
            }
            TextRange description = getRangeBetweenNested(input, new TextRange(startOffset, input.length()), mainContentPatterns.first, mainContentPatterns.second);
            if (description == null) {
                return;
            }
            Matcher m = ourSeeAlsoDiv.matcher(input);
            final TextRange seeAlso;
            if (findIn(m, description)) {
                seeAlso = getRangeBetweenNested(input, new TextRange(m.start(), description.getEndOffset()), ourOpeningDiv, ourClosingDiv);
                description = new TextRange(description.getStartOffset(), m.start());
            } else {
                seeAlso = null;
            }
            String text = description.substring(input);
            text = ourDetailHeaderTable.matcher(text).replaceAll("");
            text = ourClassHeaderTable.matcher(text).replaceAll("");
            result.append(HTML).append("<PRE><b>").append(DISPLAY_NAME_MARKER);
            result.append("</b></PRE>");
            result.append(prettyPrint(text));
            if (seeAlso != null) {
                result.append("<DL><DT><b>See also:</b></DT>");
                int pos = seeAlso.getStartOffset();
                Matcher br = Pattern.compile("<br/?>", Pattern.CASE_INSENSITIVE).matcher(input);
                while (findIn(br, new TextRange(pos, seeAlso.getEndOffset()))) {
                    TextRange item = new TextRange(pos, br.start());
                    result.append("<DD>").append(makeLink(item.substring(input))).append("</DD>");
                    pos = br.end();
                }
                result.append("<DD>").append(makeLink(input.substring(pos, seeAlso.getEndOffset()))).append("</DD></DL>");
            }
            result.append(HTML_CLOSE);
        }
    };
    for (String docURL : findUrls(elementToShowDoc)) {
        try {
            String javadoc = docFilter.getExternalDocInfoForElement(docURL, elementToShowDoc);
            if (StringUtil.isNotEmpty(javadoc)) {
                return javadoc;
            }
        } catch (Exception e) {
        //try next url
        }
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) Matcher(java.util.regex.Matcher) Reader(java.io.Reader) IOException(java.io.IOException) JSOffsetBasedImplicitElement(com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement) THashMap(gnu.trove.THashMap) Map(java.util.Map) AbstractExternalFilter(com.intellij.codeInsight.documentation.AbstractExternalFilter)

Example 13 with XmlBackedJSClassImpl

use of com.intellij.lang.javascript.flex.XmlBackedJSClassImpl in project intellij-plugins by JetBrains.

the class ActionScriptImportHandler method _resolveTypeName.

// TODO _str should be JSReferenceExpression for caching!
@Nullable
private JSImportedElementResolveResult _resolveTypeName(@Nullable final String _name, @NotNull PsiElement context) {
    String name = _name;
    if (name == null)
        return null;
    JSResolveUtil.GenericSignature genericSignature = JSResolveUtil.extractGenericSignature(name);
    if (genericSignature != null) {
        name = genericSignature.elementType;
    }
    final Ref<JSImportedElementResolveResult> resultRef = new Ref<>();
    final String name1 = name;
    JSResolveUtil.walkOverStructure(context, context1 -> {
        JSImportedElementResolveResult resolved = null;
        if (context1 instanceof XmlBackedJSClassImpl) {
            XmlTag rootTag = ((XmlBackedJSClassImpl) context1).getParent();
            if (rootTag != null && name1.equals(rootTag.getLocalName())) {
                final XmlElementDescriptor descriptor = rootTag.getDescriptor();
                PsiElement element = descriptor != null ? descriptor.getDeclaration() : null;
                if (element instanceof XmlFile) {
                    element = XmlBackedJSClassFactory.getXmlBackedClass((XmlFile) element);
                }
                final String s = element instanceof JSClass ? ((JSClass) element).getQualifiedName() : rootTag.getLocalName();
                resolved = new JSImportedElementResolveResult(s);
            } else {
                resolved = resolveTypeNameUsingImports(name1, context1);
            }
        } else if (context1 instanceof JSQualifiedNamedElement) {
            if (context1 instanceof JSClass && name1.equals(context1.getName())) {
                resolved = new JSImportedElementResolveResult(((JSQualifiedNamedElement) context1).getQualifiedName());
            } else {
                resolved = resolveTypeNameUsingImports(name1, context1);
                if (resolved == null && context1.getParent() instanceof JSFile) {
                    final String qName = ((JSQualifiedNamedElement) context1).getQualifiedName();
                    final String packageName = qName == null ? "" : context1 instanceof JSPackageStatement ? qName + "." : qName.substring(0, qName.lastIndexOf('.') + 1);
                    if (packageName.length() != 0) {
                        final PsiElement byQName = JSClassResolver.findClassFromNamespace(packageName + name1, context1);
                        if (byQName instanceof JSQualifiedNamedElement) {
                            resolved = new JSImportedElementResolveResult(((JSQualifiedNamedElement) byQName).getQualifiedName());
                        }
                    }
                }
            }
        } else {
            resolved = resolveTypeNameUsingImports(name1, context1);
            PsiElement contextOfContext;
            if (resolved == null && context1 instanceof JSFile && (contextOfContext = context1.getContext()) != null) {
                XmlBackedJSClassImpl clazz = contextOfContext instanceof XmlElement ? (XmlBackedJSClassImpl) XmlBackedJSClassImpl.getContainingComponent((XmlElement) contextOfContext) : null;
                if (clazz != null) {
                    SinkResolveProcessor r = new SinkResolveProcessor(name1, new ResolveResultSink(null, name1));
                    r.setForceImportsForPlace(true);
                    boolean b = clazz.doImportFromScripts(r, clazz);
                    if (!b) {
                        PsiElement resultFromProcessor = r.getResult();
                        JSQualifiedNamedElement clazzFromComponent = resultFromProcessor instanceof JSQualifiedNamedElement ? (JSQualifiedNamedElement) resultFromProcessor : null;
                        if (clazzFromComponent != null) {
                            resolved = new JSImportedElementResolveResult(clazzFromComponent.getQualifiedName(), clazz, null);
                        }
                    }
                }
            }
        }
        if (resolved != null) {
            resultRef.set(resolved);
            return false;
        }
        if (context1 instanceof JSPackageStatement)
            return false;
        return true;
    });
    JSImportedElementResolveResult result = resultRef.get();
    if (genericSignature != null && result != null) {
        // TODO: more than one type parameter
        StringBuilder genericSignatureBuffer = new StringBuilder();
        genericSignatureBuffer.append(".<");
        genericSignatureBuffer.append(resolveTypeName(genericSignature.genericType, context).getQualifiedName());
        genericSignatureBuffer.append(">");
        result = result.appendSignature(genericSignatureBuffer.toString());
    }
    return result;
}
Also used : XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) XmlFile(com.intellij.psi.xml.XmlFile) JSPackageStatement(com.intellij.lang.javascript.psi.ecmal4.JSPackageStatement) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) Ref(com.intellij.openapi.util.Ref) XmlElement(com.intellij.psi.xml.XmlElement) JSFile(com.intellij.lang.javascript.psi.JSFile) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlBackedJSClassImpl (com.intellij.lang.javascript.flex.XmlBackedJSClassImpl)13 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)6 PsiElement (com.intellij.psi.PsiElement)6 MxmlJSClass (com.intellij.javascript.flex.mxml.MxmlJSClass)5 Nullable (org.jetbrains.annotations.Nullable)5 XmlFile (com.intellij.psi.xml.XmlFile)4 AnnotationBackedDescriptor (com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)3 Ref (com.intellij.openapi.util.Ref)3 XmlTag (com.intellij.psi.xml.XmlTag)3 JSFile (com.intellij.lang.javascript.psi.JSFile)2 JSFunction (com.intellij.lang.javascript.psi.JSFunction)2 JSAttribute (com.intellij.lang.javascript.psi.ecmal4.JSAttribute)2 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)2 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)2 JSReferenceList (com.intellij.lang.javascript.psi.ecmal4.JSReferenceList)2 JSResolveUtil (com.intellij.lang.javascript.psi.resolve.JSResolveUtil)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)2 THashMap (gnu.trove.THashMap)2 THashSet (gnu.trove.THashSet)2