Search in sources :

Example 1 with JSOffsetBasedImplicitElement

use of com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement in project intellij-plugins by JetBrains.

the class AngularIndexUtil method multiResolveAngularNamedDefinitionIndex.

public static ResolveResult[] multiResolveAngularNamedDefinitionIndex(@NotNull final Project project, @NotNull final ID<String, AngularNamedItemDefinition> INDEX, @NotNull final String id, @NotNull final Condition<VirtualFile> filter, boolean dirtyResolve) {
    final FileBasedIndex instance = FileBasedIndex.getInstance();
    Collection<VirtualFile> files = instance.getContainingFiles(INDEX, id, GlobalSearchScope.allScope(project));
    if (files.isEmpty())
        return ResolveResult.EMPTY_ARRAY;
    final List<VirtualFile> filtered = ContainerUtil.filter(files, filter);
    if (filtered.isEmpty()) {
        if (!dirtyResolve)
            return ResolveResult.EMPTY_ARRAY;
    } else {
        files = filtered;
    }
    final List<JSImplicitElement> elements = new ArrayList<>();
    for (VirtualFile file : files) {
        final List<AngularNamedItemDefinition> values = instance.getValues(INDEX, id, GlobalSearchScope.fileScope(project, file));
        for (AngularNamedItemDefinition value : values) {
            JSQualifiedNameImpl qName = JSQualifiedNameImpl.fromQualifiedName(id);
            JSImplicitElementImpl.Builder elementBuilder = new JSImplicitElementImpl.Builder(qName, null);
            final PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
            if (psiFile != null) {
                elements.add(new JSOffsetBasedImplicitElement(elementBuilder, (int) value.getStartOffset(), psiFile));
            }
        }
    }
    final List<ResolveResult> list = ContainerUtil.map(elements, JS_IMPLICIT_TO_RESOLVE_RESULT);
    return list.toArray(new ResolveResult[list.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JSQualifiedNameImpl(com.intellij.lang.javascript.psi.JSQualifiedNameImpl) JSImplicitElementImpl(com.intellij.lang.javascript.psi.stubs.impl.JSImplicitElementImpl) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) JSOffsetBasedImplicitElement(com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement) JSImplicitElement(com.intellij.lang.javascript.psi.stubs.JSImplicitElement) JSResolveResult(com.intellij.lang.javascript.psi.resolve.JSResolveResult) ResolveResult(com.intellij.psi.ResolveResult) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex)

Example 2 with JSOffsetBasedImplicitElement

use of com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement in project intellij-plugins by JetBrains.

the class AngularUiRouterTest method emptyViewNavigatesToFilesDefaultView.

private void emptyViewNavigatesToFilesDefaultView(PsiFile file, String str) {
    final PsiElement inObj = getElement(file, str);
    Assert.assertTrue(inObj.getParent() instanceof JSLiteralExpression);
    Assert.assertTrue(inObj.getParent().getParent() instanceof JSProperty);
    final JSProperty templateUrl = (JSProperty) inObj.getParent().getParent();
    Assert.assertEquals("templateUrl", templateUrl.getName());
    final PsiElement parentProperty = templateUrl.getParent().getParent();
    Assert.assertTrue(parentProperty instanceof JSProperty);
    final PsiReference reference = parentProperty.getReference();
    Assert.assertNotNull(reference);
    Assert.assertEquals("", reference.getCanonicalText());
    final PsiElement resolve = reference.resolve();
    Assert.assertNotNull(resolve);
    Assert.assertEquals(StringUtil.unquoteString(str), resolve.getContainingFile().getName());
    Assert.assertEquals("", ((JSPsiNamedElementBase) resolve).getName());
    final PsiElement element = ((JSOffsetBasedImplicitElement) resolve).getElementAtOffset();
    Assert.assertEquals("ui-view", element.getText());
}
Also used : JSLiteralExpression(com.intellij.lang.javascript.psi.JSLiteralExpression) JSOffsetBasedImplicitElement(com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement) JSProperty(com.intellij.lang.javascript.psi.JSProperty)

Example 3 with JSOffsetBasedImplicitElement

use of com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement 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 4 with JSOffsetBasedImplicitElement

use of com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement in project intellij-plugins by JetBrains.

the class AngularUiRouterDiagramBuilder method readTemplateFromFile.

@NotNull
static Template readTemplateFromFile(@NotNull Project project, @NotNull String url, PsiElement templateElement) {
    final PsiFile templateFile = templateElement.getContainingFile();
    final Map<String, SmartPsiElementPointer<PsiElement>> placeholders = new HashMap<>();
    final Set<String> placeholdersSet = new HashSet<>();
    final FileBasedIndex instance = FileBasedIndex.getInstance();
    final GlobalSearchScope scope = GlobalSearchScope.fileScope(project, templateFile.getVirtualFile());
    instance.processAllKeys(AngularUiRouterViewsIndex.UI_ROUTER_VIEWS_CACHE_INDEX, view -> {
        placeholdersSet.add(view);
        return true;
    }, scope, null);
    final SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(project);
    for (String key : placeholdersSet) {
        instance.processValues(AngularUiRouterViewsIndex.UI_ROUTER_VIEWS_CACHE_INDEX, key, null, (file, value) -> {
            final JSImplicitElementImpl.Builder builder = new JSImplicitElementImpl.Builder(JSQualifiedNameImpl.fromQualifiedName(key), null);
            final JSOffsetBasedImplicitElement implicitElement = new JSOffsetBasedImplicitElement(builder, (int) value.getStartOffset(), templateFile);
            if (templateElement instanceof PsiFile || PsiTreeUtil.isAncestor(templateElement, implicitElement, false)) {
                placeholders.put(key, smartPointerManager.createSmartPsiElementPointer(implicitElement));
            }
            return true;
        }, scope);
    }
    final Template template = new Template(url, smartPointerManager.createSmartPsiElementPointer(templateElement));
    template.setViewPlaceholders(placeholders);
    return template;
}
Also used : JSImplicitElementImpl(com.intellij.lang.javascript.psi.stubs.impl.JSImplicitElementImpl) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) JSOffsetBasedImplicitElement(com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with JSOffsetBasedImplicitElement

use of com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement in project intellij-plugins by JetBrains.

the class ActionScriptTypeEvaluator method addTypeFromElementResolveResult.

@Override
protected boolean addTypeFromElementResolveResult(PsiElement resolveResult, boolean hasSomeType) {
    if (resolveResult instanceof JSOffsetBasedImplicitElement && JavaScriptSupportLoader.isFlexMxmFile(resolveResult.getContainingFile())) {
        resolveResult = ((JSOffsetBasedImplicitElement) resolveResult).getElementAtOffset();
    }
    if (resolveResult instanceof XmlToken) {
        final XmlToken xmlToken = (XmlToken) resolveResult;
        final XmlAttribute xmlAttribute = PsiTreeUtil.getParentOfType(xmlToken, XmlAttribute.class);
        final XmlTag xmlTag = PsiTreeUtil.getParentOfType(xmlToken, XmlTag.class);
        if (xmlToken.getTokenType() == XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN && xmlAttribute != null && "id".equals(xmlAttribute.getName()) && xmlTag != null && isInsideRepeaterTag(xmlTag)) {
            final PsiElement arrayClass = ActionScriptClassResolver.findClassByQNameStatic(ARRAY_CLASS_NAME, xmlToken);
            if (arrayClass != null) {
                final String arrayType = new JSTagContextBuilder(resolveResult, null).typeName;
                JSTypeSource source = JSTypeSourceFactory.createTypeSource(resolveResult);
                JSType type;
                if (arrayType != null) {
                    JSType baseType = JSNamedType.createType(arrayType, source, JSContext.INSTANCE);
                    type = new JSArrayTypeImpl(baseType, source);
                } else {
                    type = new JSPrimitiveArrayType(source, JSTypeContext.INSTANCE);
                }
                addType(type, arrayClass);
            }
        } else {
            final XmlTag tag = PsiTreeUtil.getParentOfType(resolveResult, XmlTag.class, false);
            final JSClass clazz = JSResolveUtil.getClassFromTagNameInMxml(tag);
            if (clazz != null) {
                final String name = clazz.getQualifiedName();
                if (name != null) {
                    addType(name, clazz);
                }
            }
        }
        return hasSomeType;
    }
    return super.addTypeFromElementResolveResult(resolveResult, hasSomeType);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) JSPrimitiveArrayType(com.intellij.lang.javascript.psi.types.primitives.JSPrimitiveArrayType) JSOffsetBasedImplicitElement(com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

JSOffsetBasedImplicitElement (com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement)5 JSImplicitElementImpl (com.intellij.lang.javascript.psi.stubs.impl.JSImplicitElementImpl)2 FileBasedIndex (com.intellij.util.indexing.FileBasedIndex)2 AbstractExternalFilter (com.intellij.codeInsight.documentation.AbstractExternalFilter)1 XmlBackedJSClassImpl (com.intellij.lang.javascript.flex.XmlBackedJSClassImpl)1 JSLiteralExpression (com.intellij.lang.javascript.psi.JSLiteralExpression)1 JSProperty (com.intellij.lang.javascript.psi.JSProperty)1 JSQualifiedNameImpl (com.intellij.lang.javascript.psi.JSQualifiedNameImpl)1 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)1 JSResolveResult (com.intellij.lang.javascript.psi.resolve.JSResolveResult)1 JSImplicitElement (com.intellij.lang.javascript.psi.stubs.JSImplicitElement)1 JSPrimitiveArrayType (com.intellij.lang.javascript.psi.types.primitives.JSPrimitiveArrayType)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 ResolveResult (com.intellij.psi.ResolveResult)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlTag (com.intellij.psi.xml.XmlTag)1 XmlToken (com.intellij.psi.xml.XmlToken)1