Search in sources :

Example 1 with XmlEntitiesInspection

use of com.intellij.codeInspection.htmlInspections.XmlEntitiesInspection in project intellij-community by JetBrains.

the class XmlTagInsertHandler method addRequiredAttributes.

@Nullable
private static StringBuilder addRequiredAttributes(XmlElementDescriptor descriptor, @Nullable XmlTag tag, Template template, PsiFile containingFile) {
    boolean htmlCode = HtmlUtil.hasHtml(containingFile) || HtmlUtil.supportsXmlTypedHandlers(containingFile);
    Set<String> notRequiredAttributes = Collections.emptySet();
    if (tag instanceof HtmlTag) {
        final InspectionProfile profile = InspectionProjectProfileManager.getInstance(tag.getProject()).getCurrentProfile();
        XmlEntitiesInspection inspection = (XmlEntitiesInspection) profile.getUnwrappedTool(XmlEntitiesInspection.REQUIRED_ATTRIBUTES_SHORT_NAME, tag);
        if (inspection != null) {
            StringTokenizer tokenizer = new StringTokenizer(inspection.getAdditionalEntries());
            notRequiredAttributes = new HashSet<>();
            while (tokenizer.hasMoreElements()) notRequiredAttributes.add(tokenizer.nextToken());
        }
    }
    XmlAttributeDescriptor[] attributes = descriptor.getAttributesDescriptors(tag);
    StringBuilder indirectRequiredAttrs = null;
    if (WebEditorOptions.getInstance().isAutomaticallyInsertRequiredAttributes()) {
        final XmlExtension extension = XmlExtension.getExtension(containingFile);
        for (XmlAttributeDescriptor attributeDecl : attributes) {
            String attributeName = attributeDecl.getName(tag);
            boolean shouldBeInserted = extension.shouldBeInserted(attributeDecl);
            if (shouldBeInserted && (tag == null || tag.getAttributeValue(attributeName) == null)) {
                if (!notRequiredAttributes.contains(attributeName)) {
                    if (!extension.isIndirectSyntax(attributeDecl)) {
                        template.addTextSegment(" " + attributeName + "=" + XmlAttributeInsertHandler.getAttributeQuote(htmlCode));
                        template.addVariable(new MacroCallNode(new CompleteMacro()), true);
                        template.addTextSegment(XmlAttributeInsertHandler.getAttributeQuote(htmlCode));
                    } else {
                        if (indirectRequiredAttrs == null)
                            indirectRequiredAttrs = new StringBuilder();
                        indirectRequiredAttrs.append("\n<jsp:attribute name=\"").append(attributeName).append("\"></jsp:attribute>\n");
                    }
                }
            } else if (shouldBeInserted && attributeDecl.isFixed() && attributeDecl.getDefaultValue() != null && !htmlCode) {
                template.addTextSegment(" " + attributeName + "=" + XmlAttributeInsertHandler.getAttributeQuote(false) + attributeDecl.getDefaultValue() + XmlAttributeInsertHandler.getAttributeQuote(false));
            }
        }
    }
    return indirectRequiredAttrs;
}
Also used : InspectionProfile(com.intellij.codeInspection.InspectionProfile) HtmlTag(com.intellij.psi.html.HtmlTag) CompleteMacro(com.intellij.codeInsight.template.macro.CompleteMacro) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) XmlEntitiesInspection(com.intellij.codeInspection.htmlInspections.XmlEntitiesInspection) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with XmlEntitiesInspection

use of com.intellij.codeInspection.htmlInspections.XmlEntitiesInspection in project intellij-community by JetBrains.

the class HtmlUtil method getEntitiesString.

@Nullable
public static String getEntitiesString(@Nullable PsiElement context, @NotNull String inspectionName) {
    if (context == null)
        return null;
    PsiFile containingFile = context.getContainingFile().getOriginalFile();
    final InspectionProfile profile = InspectionProjectProfileManager.getInstance(context.getProject()).getCurrentProfile();
    XmlEntitiesInspection inspection = (XmlEntitiesInspection) profile.getUnwrappedTool(inspectionName, containingFile);
    if (inspection != null) {
        return inspection.getAdditionalEntries();
    }
    return null;
}
Also used : InspectionProfile(com.intellij.codeInspection.InspectionProfile) PsiFile(com.intellij.psi.PsiFile) XmlEntitiesInspection(com.intellij.codeInspection.htmlInspections.XmlEntitiesInspection)

Aggregations

InspectionProfile (com.intellij.codeInspection.InspectionProfile)2 XmlEntitiesInspection (com.intellij.codeInspection.htmlInspections.XmlEntitiesInspection)2 MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)1 CompleteMacro (com.intellij.codeInsight.template.macro.CompleteMacro)1 PsiFile (com.intellij.psi.PsiFile)1 HtmlTag (com.intellij.psi.html.HtmlTag)1 Nullable (org.jetbrains.annotations.Nullable)1