Search in sources :

Example 16 with HtmlTag

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

the class HtmlUtil method isHtmlTagContainingFile.

public static boolean isHtmlTagContainingFile(PsiElement element) {
    if (element == null) {
        return false;
    }
    final PsiFile containingFile = element.getContainingFile();
    if (containingFile != null) {
        final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false);
        if (tag instanceof HtmlTag) {
            return true;
        }
        final XmlDocument document = PsiTreeUtil.getParentOfType(element, XmlDocument.class, false);
        if (document instanceof HtmlDocumentImpl) {
            return true;
        }
        final FileViewProvider provider = containingFile.getViewProvider();
        Language language;
        if (provider instanceof TemplateLanguageFileViewProvider) {
            language = ((TemplateLanguageFileViewProvider) provider).getTemplateDataLanguage();
        } else {
            language = provider.getBaseLanguage();
        }
        return language == XHTMLLanguage.INSTANCE;
    }
    return false;
}
Also used : TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) XMLLanguage(com.intellij.lang.xml.XMLLanguage) XHTMLLanguage(com.intellij.lang.xhtml.XHTMLLanguage) HTMLLanguage(com.intellij.lang.html.HTMLLanguage) HtmlDocumentImpl(com.intellij.psi.impl.source.html.HtmlDocumentImpl) PsiFile(com.intellij.psi.PsiFile) HtmlTag(com.intellij.psi.html.HtmlTag) TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider)

Example 17 with HtmlTag

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

the class InsertRequiredAttributeFix method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") final Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    XmlTag myTag = (XmlTag) startElement;
    ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(myTag);
    final XmlElementDescriptor descriptor = myTag.getDescriptor();
    if (descriptor == null) {
        return;
    }
    final XmlAttributeDescriptor attrDescriptor = descriptor.getAttributeDescriptor(myAttrName, myTag);
    final boolean indirectSyntax = XmlExtension.getExtension(myTag.getContainingFile()).isIndirectSyntax(attrDescriptor);
    boolean insertShorthand = myTag instanceof HtmlTag && attrDescriptor != null && HtmlUtil.isBooleanAttribute(attrDescriptor, myTag);
    PsiElement anchor = SourceTreeToPsiMap.treeElementToPsi(XmlChildRole.EMPTY_TAG_END_FINDER.findChild(treeElement));
    final boolean anchorIsEmptyTag = anchor != null;
    if (anchor == null) {
        anchor = SourceTreeToPsiMap.treeElementToPsi(XmlChildRole.START_TAG_END_FINDER.findChild(treeElement));
    }
    if (anchor == null)
        return;
    final Template template = TemplateManager.getInstance(project).createTemplate("", "");
    if (indirectSyntax) {
        if (anchorIsEmptyTag)
            template.addTextSegment(">");
        template.addTextSegment("<jsp:attribute name=\"" + myAttrName + "\">");
    } else {
        template.addTextSegment(" " + myAttrName + (!insertShorthand ? "=\"" : ""));
    }
    Expression expression = new Expression() {

        final TextResult result = new TextResult("");

        @Override
        public Result calculateResult(ExpressionContext context) {
            return result;
        }

        @Override
        public Result calculateQuickResult(ExpressionContext context) {
            return null;
        }

        @Override
        public LookupElement[] calculateLookupItems(ExpressionContext context) {
            final LookupElement[] items = new LookupElement[myValues.length];
            for (int i = 0; i < items.length; i++) {
                items[i] = LookupElementBuilder.create(myValues[i]);
            }
            return items;
        }
    };
    if (!insertShorthand)
        template.addVariable(NAME_TEMPLATE_VARIABLE, expression, expression, true);
    if (indirectSyntax) {
        template.addTextSegment("</jsp:attribute>");
        template.addEndVariable();
        if (anchorIsEmptyTag)
            template.addTextSegment("</" + myTag.getName() + ">");
    } else if (!insertShorthand) {
        template.addTextSegment("\"");
    }
    final PsiElement anchor1 = anchor;
    final Runnable runnable = () -> ApplicationManager.getApplication().runWriteAction(() -> {
        int textOffset = anchor1.getTextOffset();
        if (!anchorIsEmptyTag && indirectSyntax)
            ++textOffset;
        editor.getCaretModel().moveToOffset(textOffset);
        if (anchorIsEmptyTag && indirectSyntax) {
            editor.getDocument().deleteString(textOffset, textOffset + 2);
        }
        TemplateManager.getInstance(project).startTemplate(editor, template);
    });
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
        Runnable commandRunnable = () -> CommandProcessor.getInstance().executeCommand(project, runnable, getText(), getFamilyName());
        ApplicationManager.getApplication().invokeLater(commandRunnable);
    } else {
        runnable.run();
    }
}
Also used : HtmlTag(com.intellij.psi.html.HtmlTag) LookupElement(com.intellij.codeInsight.lookup.LookupElement) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) ASTNode(com.intellij.lang.ASTNode) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 18 with HtmlTag

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

the class XmlAttributeReferenceCompletionProvider method addVariants.

private static void addVariants(final CompletionResultSet result, final XmlAttribute[] attributes, final XmlAttributeDescriptor[] descriptors, XmlAttribute attribute, @Nullable InsertHandler<LookupElement> replacementInsertHandler) {
    final XmlTag tag = attribute.getParent();
    final PsiFile file = tag.getContainingFile();
    final XmlExtension extension = XmlExtension.getExtension(file);
    final String prefix = attribute.getName().contains(":") && ((XmlAttributeImpl) attribute).getRealLocalName().length() > 0 ? attribute.getNamespacePrefix() + ":" : null;
    for (XmlAttributeDescriptor descriptor : descriptors) {
        if (isValidVariant(attribute, descriptor, attributes, extension)) {
            String name = descriptor.getName(tag);
            InsertHandler<LookupElement> insertHandler = XmlAttributeInsertHandler.INSTANCE;
            if (tag instanceof HtmlTag && HtmlUtil.isShortNotationOfBooleanAttributePreferred() && HtmlUtil.isBooleanAttribute(descriptor, tag)) {
                insertHandler = null;
            }
            if (replacementInsertHandler != null) {
                insertHandler = replacementInsertHandler;
            } else if (descriptor instanceof NamespaceAwareXmlAttributeDescriptor) {
                final String namespace = ((NamespaceAwareXmlAttributeDescriptor) descriptor).getNamespace(tag);
                if (file instanceof XmlFile && namespace != null && namespace.length() > 0 && !name.contains(":") && tag.getPrefixByNamespace(namespace) == null) {
                    insertHandler = new XmlAttributeInsertHandler(namespace);
                }
            }
            if (prefix == null || name.startsWith(prefix)) {
                if (prefix != null && name.length() > prefix.length()) {
                    name = descriptor.getName(tag).substring(prefix.length());
                }
                LookupElementBuilder element = LookupElementBuilder.create(name);
                if (descriptor instanceof PsiPresentableMetaData) {
                    element = element.withIcon(((PsiPresentableMetaData) descriptor).getIcon());
                }
                final int separator = name.indexOf(':');
                if (separator > 0) {
                    element = element.withLookupString(name.substring(separator + 1));
                }
                element = element.withCaseSensitivity(!(descriptor instanceof HtmlAttributeDescriptorImpl)).withInsertHandler(insertHandler);
                result.addElement(descriptor.isRequired() ? PrioritizedLookupElement.withPriority(element.appendTailText("(required)", true), 100) : HtmlUtil.isOwnHtmlAttribute(descriptor) ? PrioritizedLookupElement.withPriority(element, 50) : element);
            }
        }
    }
}
Also used : HtmlAttributeDescriptorImpl(com.intellij.psi.impl.source.html.dtd.HtmlAttributeDescriptorImpl) XmlFile(com.intellij.psi.xml.XmlFile) HtmlTag(com.intellij.psi.html.HtmlTag) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiPresentableMetaData(com.intellij.psi.meta.PsiPresentableMetaData) XmlExtension(com.intellij.xml.XmlExtension) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) NamespaceAwareXmlAttributeDescriptor(com.intellij.xml.NamespaceAwareXmlAttributeDescriptor) NamespaceAwareXmlAttributeDescriptor(com.intellij.xml.NamespaceAwareXmlAttributeDescriptor) XmlAttributeImpl(com.intellij.psi.impl.source.xml.XmlAttributeImpl) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiFile(com.intellij.psi.PsiFile) XmlTag(com.intellij.psi.xml.XmlTag)

Example 19 with HtmlTag

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

the class HtmlMissingClosingTagInspection method checkTag.

@Override
protected void checkTag(@NotNull XmlTag tag, @NotNull ProblemsHolder holder, boolean isOnTheFly) {
    if (!(tag instanceof HtmlTag) || !XmlHighlightVisitor.shouldBeValidated(tag)) {
        return;
    }
    final PsiElement child = tag.getLastChild();
    if (child instanceof PsiErrorElement) {
        return;
    }
    final XmlToken tagNameElement = XmlTagUtil.getStartTagNameElement(tag);
    if (tagNameElement == null) {
        return;
    }
    final String tagName = tagNameElement.getText();
    if (HtmlUtil.isSingleHtmlTag(tagName) || XmlTagUtil.getEndTagNameElement(tag) != null) {
        return;
    }
    holder.registerProblem(tagNameElement, XmlErrorMessages.message("element.missing.end.tag"), new MissingClosingTagFix(tagName));
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) HtmlTag(com.intellij.psi.html.HtmlTag) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken)

Example 20 with HtmlTag

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

the class HtmlUnknownAttributeInspectionBase method checkAttribute.

@Override
protected void checkAttribute(@NotNull final XmlAttribute attribute, @NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    final XmlTag tag = attribute.getParent();
    if (tag instanceof HtmlTag) {
        XmlElementDescriptor elementDescriptor = tag.getDescriptor();
        if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor) {
            return;
        }
        XmlAttributeDescriptor attributeDescriptor = elementDescriptor.getAttributeDescriptor(attribute);
        if (attributeDescriptor == null && !attribute.isNamespaceDeclaration()) {
            final String name = attribute.getName();
            if (!XmlUtil.attributeFromTemplateFramework(name, tag) && (!isCustomValuesEnabled() || !isCustomValue(name))) {
                boolean maySwitchToHtml5 = HtmlUtil.isCustomHtml5Attribute(name) && !HtmlUtil.hasNonHtml5Doctype(tag);
                LocalQuickFix[] quickfixes = new LocalQuickFix[maySwitchToHtml5 ? 3 : 2];
                quickfixes[0] = new AddCustomHtmlElementIntentionAction(ATTRIBUTE_KEY, name, XmlBundle.message("add.custom.html.attribute", name));
                quickfixes[1] = new RemoveAttributeIntentionAction(name);
                if (maySwitchToHtml5) {
                    quickfixes[2] = new SwitchToHtml5WithHighPriorityAction();
                }
                registerProblemOnAttributeName(attribute, XmlErrorMessages.message("attribute.is.not.allowed.here", attribute.getName()), holder, quickfixes);
            }
        }
    }
}
Also used : AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) HtmlTag(com.intellij.psi.html.HtmlTag) 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