Search in sources :

Example 11 with HtmlTag

use of com.intellij.psi.html.HtmlTag in project intellij-community by JetBrains.

the class HtmlLinkUtil method processInjectedContent.

public static void processInjectedContent(final XmlTag element, @NotNull final Processor<XmlTag> tagProcessor) {
    final PsiLanguageInjectionHost.InjectedPsiVisitor injectedPsiVisitor = new PsiLanguageInjectionHost.InjectedPsiVisitor() {

        @Override
        public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
            if (injectedPsi instanceof XmlFile) {
                final XmlDocument injectedDocument = ((XmlFile) injectedPsi).getDocument();
                if (injectedDocument != null) {
                    final XmlTag rootTag = injectedDocument.getRootTag();
                    if (rootTag != null) {
                        for (PsiElement element = rootTag; element != null; element = element.getNextSibling()) {
                            if (element instanceof XmlTag) {
                                final XmlTag tag = (XmlTag) element;
                                String tagName = tag.getLocalName();
                                if (element instanceof HtmlTag || tag.getNamespacePrefix().length() > 0)
                                    tagName = tagName.toLowerCase();
                                if (LINK.equalsIgnoreCase(tagName)) {
                                    tagProcessor.process((XmlTag) element);
                                }
                            }
                        }
                    }
                }
            }
        }
    };
    final XmlText[] texts = PsiTreeUtil.getChildrenOfType(element, XmlText.class);
    if (texts != null && texts.length > 0) {
        for (final XmlText text : texts) {
            for (PsiElement _element : text.getChildren()) {
                if (_element instanceof PsiLanguageInjectionHost) {
                    InjectedLanguageUtil.enumerate(_element, injectedPsiVisitor);
                }
            }
        }
    }
    final XmlComment[] comments = PsiTreeUtil.getChildrenOfType(element, XmlComment.class);
    if (comments != null && comments.length > 0) {
        for (final XmlComment comment : comments) {
            if (comment instanceof PsiLanguageInjectionHost) {
                InjectedLanguageUtil.enumerate(comment, injectedPsiVisitor);
            }
        }
    }
}
Also used : HtmlTag(com.intellij.psi.html.HtmlTag) NotNull(org.jetbrains.annotations.NotNull) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiFile(com.intellij.psi.PsiFile) List(java.util.List) PsiElement(com.intellij.psi.PsiElement)

Example 12 with HtmlTag

use of com.intellij.psi.html.HtmlTag in project intellij-plugins by JetBrains.

the class MxmlLanguageInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement host) {
    if (!host.getContainingFile().getLanguage().isKindOf(MxmlLanguage.INSTANCE)) {
        return;
    }
    if (host instanceof XmlAttributeValue) {
        final PsiElement attribute = host.getParent();
        final PsiElement tag = attribute.getParent();
        if (attribute instanceof XmlAttribute && tag instanceof XmlTag) {
            if (isFxPrivateTag((XmlTag) tag) || isInsideFxPrivateTag((XmlTag) tag)) {
                return;
            }
            if (host.getTextLength() == 0)
                return;
            @NonNls String attrName = ((XmlAttribute) attribute).getName();
            if ("implements".equals(attrName)) {
                TextRange range = new TextRange(1, host.getTextLength() - 1);
                registrar.startInjecting(JavaScriptSupportLoader.ECMA_SCRIPT_L4).addPlace("class Foo implements ", " {}", (PsiLanguageInjectionHost) host, range).doneInjecting();
            } else if ("source".equals(attrName) && FlexPredefinedTagNames.BINDING.equals(((XmlTag) tag).getLocalName()) && JavaScriptSupportLoader.isLanguageNamespace(((XmlTag) tag).getNamespace()) && !host.textContains('{')) {
                TextRange range = new TextRange(1, host.getTextLength() - 1);
                registrar.startInjecting(JavaScriptSupportLoader.ECMA_SCRIPT_L4).addPlace(FUNCTION_CALL_PREFIX, FUNCTION_CALL_SUFFIX, (PsiLanguageInjectionHost) host, range).doneInjecting();
            } else if (attrName.equals("expression") && "RegExpValidator".equals(((XmlTag) tag).getLocalName()) && regexpLanguage != null) {
                String hostText = host.getText();
                int startPos = hostText.indexOf('/');
                int endPos = hostText.lastIndexOf('/');
                if (startPos != -1) {
                    if (endPos > startPos) {
                        TextRange range = new TextRange(startPos + 1, endPos);
                        registrar.startInjecting(regexpLanguage).addPlace(null, null, (PsiLanguageInjectionHost) host, range).doneInjecting();
                    }
                } else {
                    injectInMxmlFile(registrar, host, ((XmlAttribute) attribute).getDescriptor(), (XmlTag) tag);
                }
            } else {
                injectInMxmlFile(registrar, host, ((XmlAttribute) attribute).getDescriptor(), (XmlTag) tag);
            }
        }
    } else if (host instanceof XmlText) {
        final PsiElement _tag = host.getParent();
        if (_tag instanceof XmlTag) {
            final XmlTag tag = (XmlTag) _tag;
            if (isFxPrivateTag(tag) || isInsideFxPrivateTag(tag) || tag instanceof HtmlTag) {
                return;
            }
            @NonNls final String localName = tag.getLocalName();
            if ((XmlBackedJSClassImpl.SCRIPT_TAG_NAME.equals(localName) || FlexPredefinedTagNames.METADATA.equals(localName)) && tag.getAttributeValue("source") == null) {
                JSLanguageInjector.injectToXmlText(registrar, host, JavaScriptSupportLoader.ECMA_SCRIPT_L4, null, null);
            } else if (FlexPredefinedTagNames.STYLE.equals(localName) && FlexUtils.isMxmlNs(tag.getNamespace()) && cssLanguage != null) {
                JSLanguageInjector.injectToXmlText(registrar, host, cssLanguage, null, null);
            } else if (tag.getSubTags().length == 0) {
                injectInMxmlFile(registrar, host, tag.getDescriptor(), tag);
            }
        }
    } else if (host instanceof XmlComment) {
        final String text = host.getText();
        final String marker = "<!---";
        if (text.startsWith(marker)) {
            final String marker2 = "-->";
            int end = text.endsWith(marker2) ? host.getTextLength() - marker2.length() : host.getTextLength();
            //if (nestedCommentStart != -1) end = nestedCommentStart;
            if (end < marker.length())
                return;
            TextRange range = new TextRange(marker.length(), end);
            registrar.startInjecting(JavaScriptSupportLoader.ECMA_SCRIPT_L4).addPlace("/***", "*/", (PsiLanguageInjectionHost) host, range).doneInjecting();
        }
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) TextRange(com.intellij.openapi.util.TextRange) HtmlTag(com.intellij.psi.html.HtmlTag) PsiElement(com.intellij.psi.PsiElement)

Example 13 with HtmlTag

use of com.intellij.psi.html.HtmlTag in project intellij-plugins by JetBrains.

the class AngularJSTagDescriptorsProvider method addTagNameVariants.

@Override
public void addTagNameVariants(final List<LookupElement> elements, @NotNull XmlTag xmlTag, String prefix) {
    if (!(xmlTag instanceof HtmlTag && AngularIndexUtil.hasAngularJS(xmlTag.getProject())))
        return;
    final Project project = xmlTag.getProject();
    Language language = xmlTag.getContainingFile().getLanguage();
    DirectiveUtil.processTagDirectives(project, directive -> {
        addLookupItem(language, elements, directive);
        return true;
    });
    if (AngularIndexUtil.hasAngularJS2(project)) {
        addLookupItem(language, elements, createDirective(xmlTag, NG_CONTAINER));
        addLookupItem(language, elements, createDirective(xmlTag, NG_CONTENT));
        addLookupItem(language, elements, createDirective(xmlTag, NG_TEMPLATE));
    }
}
Also used : Project(com.intellij.openapi.project.Project) Language(com.intellij.lang.Language) XMLLanguage(com.intellij.lang.xml.XMLLanguage) HtmlTag(com.intellij.psi.html.HtmlTag)

Example 14 with HtmlTag

use of com.intellij.psi.html.HtmlTag in project intellij-plugins by JetBrains.

the class AngularJSTagDescriptorsProvider method getDescriptor.

@Nullable
@Override
public XmlElementDescriptor getDescriptor(XmlTag xmlTag) {
    final Project project = xmlTag.getProject();
    if (!(xmlTag instanceof HtmlTag && AngularIndexUtil.hasAngularJS(project)))
        return null;
    final String tagName = xmlTag.getName();
    final String directiveName = DirectiveUtil.normalizeAttributeName(tagName);
    final XmlNSDescriptor nsDescriptor = xmlTag.getNSDescriptor(xmlTag.getNamespace(), false);
    final XmlElementDescriptor descriptor = nsDescriptor != null ? nsDescriptor.getElementDescriptor(xmlTag) : null;
    if (descriptor != null && !(descriptor instanceof AnyXmlElementDescriptor)) {
        return null;
    }
    if ((NG_CONTAINER.equals(directiveName) || NG_CONTENT.equals(directiveName) || NG_TEMPLATE.equals(directiveName)) && AngularIndexUtil.hasAngularJS2(project)) {
        return new AngularJSTagDescriptor(directiveName, createDirective(xmlTag, directiveName));
    }
    JSImplicitElement directive = DirectiveUtil.getTagDirective(directiveName, project);
    if (DirectiveUtil.isAngular2Directive(directive) && !directive.getName().equals(tagName)) {
        // we've found directive via normalized name for Angular, it should not work
        directive = null;
    }
    if (directive == null && !tagName.equals(directiveName) && AngularIndexUtil.hasAngularJS2(project)) {
        directive = DirectiveUtil.getTagDirective(tagName, project);
        if (!DirectiveUtil.isAngular2Directive(directive))
            directive = null;
    }
    return directive != null ? new AngularJSTagDescriptor(directiveName, directive) : null;
}
Also used : Project(com.intellij.openapi.project.Project) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) HtmlTag(com.intellij.psi.html.HtmlTag) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) JSImplicitElement(com.intellij.lang.javascript.psi.stubs.JSImplicitElement) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with HtmlTag

use of com.intellij.psi.html.HtmlTag in project intellij-community by JetBrains.

the class TagNameVariantCollector method processVariantsInNamespace.

private static void processVariantsInNamespace(final String namespace, final XmlTag element, final List<XmlElementDescriptor> variants, final XmlElementDescriptor elementDescriptor, final String elementNamespace, final Map<String, XmlElementDescriptor> descriptorsMap, final Set<XmlNSDescriptor> visited, XmlTag parent, final XmlExtension extension) {
    if (descriptorsMap.containsKey(namespace)) {
        final XmlElementDescriptor descriptor = descriptorsMap.get(namespace);
        if (isAcceptableNs(element, elementDescriptor, elementNamespace, namespace)) {
            for (XmlElementDescriptor containedDescriptor : descriptor.getElementsDescriptors(parent)) {
                if (containedDescriptor != null)
                    variants.add(containedDescriptor);
            }
        }
        if (element instanceof HtmlTag) {
            HtmlUtil.addHtmlSpecificCompletions(descriptor, element, variants);
        }
        visited.add(descriptor.getNSDescriptor());
    } else {
        // their element descriptors (prev if section)
        if (namespace == null)
            return;
        if (namespace.isEmpty() && !visited.isEmpty())
            return;
        XmlNSDescriptor nsDescriptor = getDescriptor(element, namespace, true, extension);
        if (nsDescriptor == null) {
            if (!descriptorsMap.isEmpty())
                return;
            nsDescriptor = getDescriptor(element, namespace, false, extension);
        }
        if (nsDescriptor != null && !visited.contains(nsDescriptor) && isAcceptableNs(element, elementDescriptor, elementNamespace, namespace)) {
            visited.add(nsDescriptor);
            final XmlElementDescriptor[] rootElementsDescriptors = nsDescriptor.getRootElementsDescriptors(PsiTreeUtil.getParentOfType(element, XmlDocument.class));
            final XmlTag parentTag = extension.getParentTagForNamespace(element, nsDescriptor);
            final XmlElementDescriptor parentDescriptor;
            if (parentTag == element.getParentTag()) {
                parentDescriptor = elementDescriptor;
            } else {
                assert parentTag != null;
                parentDescriptor = parentTag.getDescriptor();
            }
            for (XmlElementDescriptor candidateDescriptor : rootElementsDescriptors) {
                if (candidateDescriptor != null && couldContainDescriptor(parentTag, parentDescriptor, candidateDescriptor, namespace, false)) {
                    variants.add(candidateDescriptor);
                }
            }
        }
    }
}
Also used : XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) HtmlTag(com.intellij.psi.html.HtmlTag) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

HtmlTag (com.intellij.psi.html.HtmlTag)25 XmlTag (com.intellij.psi.xml.XmlTag)9 AnyXmlElementDescriptor (com.intellij.xml.impl.schema.AnyXmlElementDescriptor)8 PsiElement (com.intellij.psi.PsiElement)7 PsiFile (com.intellij.psi.PsiFile)7 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)7 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)5 ASTNode (com.intellij.lang.ASTNode)4 Language (com.intellij.lang.Language)4 TextRange (com.intellij.openapi.util.TextRange)4 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)3 XHTMLLanguage (com.intellij.lang.xhtml.XHTMLLanguage)3 XMLLanguage (com.intellij.lang.xml.XMLLanguage)3 Project (com.intellij.openapi.project.Project)3 TemplateLanguageFileViewProvider (com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider)3 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)2 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 HTMLLanguage (com.intellij.lang.html.HTMLLanguage)2 PsiErrorElement (com.intellij.psi.PsiErrorElement)2