Search in sources :

Example 1 with CompleteMacro

use of com.intellij.codeInsight.template.macro.CompleteMacro in project intellij-community by JetBrains.

the class XmlTagInsertHandler method completeAttribute.

private static void completeAttribute(Template template, boolean htmlCode) {
    template.addTextSegment(" ");
    template.addVariable(new MacroCallNode(new CompleteMacro()), true);
    template.addTextSegment("=" + XmlAttributeInsertHandler.getAttributeQuote(htmlCode));
    template.addEndVariable();
    template.addTextSegment(XmlAttributeInsertHandler.getAttributeQuote(htmlCode));
}
Also used : MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) CompleteMacro(com.intellij.codeInsight.template.macro.CompleteMacro)

Example 2 with CompleteMacro

use of com.intellij.codeInsight.template.macro.CompleteMacro 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 3 with CompleteMacro

use of com.intellij.codeInsight.template.macro.CompleteMacro in project intellij-community by JetBrains.

the class AddWithParamFix method invoke.

public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException {
    final RunResult<SmartPsiElementPointer<XmlTag>> result = new WriteAction<SmartPsiElementPointer<XmlTag>>() {

        protected void run(@NotNull Result<SmartPsiElementPointer<XmlTag>> result) throws Throwable {
            final XmlTag withParamTag = RefactoringUtil.addWithParam(myTag);
            withParamTag.setAttribute("name", myName != null ? myName : "dummy");
            withParamTag.setAttribute("select", "dummy");
            result.setResult(SmartPointerManager.getInstance(project).createSmartPsiElementPointer(withParamTag));
        }
    }.execute();
    final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
    final Document doc = psiDocumentManager.getDocument(file);
    assert doc != null;
    psiDocumentManager.doPostponedOperationsAndUnblockDocument(doc);
    final XmlTag withParamTag = result.getResultObject().getElement();
    assert withParamTag != null;
    final TemplateBuilderImpl builder = new TemplateBuilderImpl(withParamTag);
    final XmlAttribute selectAttr = withParamTag.getAttribute("select", null);
    assert selectAttr != null;
    PsiElement dummy = XsltSupport.getAttValueToken(selectAttr);
    builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
    if (myName == null) {
        final XmlAttribute nameAttr = withParamTag.getAttribute("name", null);
        assert nameAttr != null;
        dummy = XsltSupport.getAttValueToken(nameAttr);
        builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
    }
    moveTo(editor, withParamTag);
    new WriteAction() {

        @SuppressWarnings({ "RawUseOfParameterizedType" })
        protected void run(@NotNull Result result) throws Throwable {
            PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
            final TemplateManager mgr = TemplateManager.getInstance(myTag.getProject());
            mgr.startTemplate(editor, builder.buildInlineTemplate());
        }
    }.execute();
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) WriteAction(com.intellij.openapi.application.WriteAction) Document(com.intellij.openapi.editor.Document) CompleteMacro(com.intellij.codeInsight.template.macro.CompleteMacro) Result(com.intellij.openapi.application.Result) RunResult(com.intellij.openapi.application.RunResult) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TemplateManager(com.intellij.codeInsight.template.TemplateManager) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) XmlTag(com.intellij.psi.xml.XmlTag)

Example 4 with CompleteMacro

use of com.intellij.codeInsight.template.macro.CompleteMacro in project intellij-community by JetBrains.

the class CreateVariableFix method invoke.

public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    editor = editor instanceof EditorWindow ? ((EditorWindow) editor).getDelegate() : editor;
    XmlTag tag = PsiTreeUtil.getContextOfType(myReference, XmlTag.class, true);
    if (tag == null)
        return;
    XmlTag xmlTag = tag.createChildTag("variable", XsltSupport.XSLT_NS, null, false);
    xmlTag.setAttribute("name", myReference.getReferencedName());
    xmlTag.setAttribute("select", "dummy");
    final XmlAttribute select = xmlTag.getAttribute("select", null);
    assert select != null;
    final PsiElement dummy = XsltSupport.getAttValueToken(select);
    assert dummy != null;
    final TemplateBuilderImpl builder = createTemplateBuilder(xmlTag);
    builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
    builder.setEndVariableAfter(select);
    final Template template = builder.buildTemplate();
    template.addTextSegment("\n");
    template.setToIndent(true);
    final XmlTag insertionPoint = findVariableInsertionPoint(tag);
    moveTo(editor, insertionPoint);
    TemplateManager.getInstance(project).startTemplate(editor, template);
}
Also used : TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) XmlAttribute(com.intellij.psi.xml.XmlAttribute) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) CompleteMacro(com.intellij.codeInsight.template.macro.CompleteMacro) PsiElement(com.intellij.psi.PsiElement) EditorWindow(com.intellij.injected.editor.EditorWindow) XmlTag(com.intellij.psi.xml.XmlTag) Template(com.intellij.codeInsight.template.Template)

Example 5 with CompleteMacro

use of com.intellij.codeInsight.template.macro.CompleteMacro in project intellij-community by JetBrains.

the class XmlTagInsertHandler method completeTagTail.

private static void completeTagTail(Template template, XmlElementDescriptor descriptor, PsiFile file, XmlTag context, boolean firstLevel) {
    boolean completeIt = !firstLevel || descriptor.getAttributesDescriptors(null).length == 0;
    switch(descriptor.getContentType()) {
        case XmlElementDescriptor.CONTENT_TYPE_UNKNOWN:
            return;
        case XmlElementDescriptor.CONTENT_TYPE_EMPTY:
            if (completeIt) {
                template.addTextSegment("/>");
            }
            break;
        case XmlElementDescriptor.CONTENT_TYPE_MIXED:
            if (completeIt) {
                template.addTextSegment(">");
                if (firstLevel) {
                    template.addEndVariable();
                } else {
                    template.addVariable(new MacroCallNode(new CompleteMacro()), true);
                }
                addTagEnd(template, descriptor, context);
            }
            break;
        default:
            if (!addRequiredSubTags(template, descriptor, file, context)) {
                if (completeIt) {
                    template.addTextSegment(">");
                    template.addEndVariable();
                    addTagEnd(template, descriptor, context);
                }
            }
            break;
    }
}
Also used : MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) CompleteMacro(com.intellij.codeInsight.template.macro.CompleteMacro)

Aggregations

MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)5 CompleteMacro (com.intellij.codeInsight.template.macro.CompleteMacro)5 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)2 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 XmlTag (com.intellij.psi.xml.XmlTag)2 Template (com.intellij.codeInsight.template.Template)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 InspectionProfile (com.intellij.codeInspection.InspectionProfile)1 XmlEntitiesInspection (com.intellij.codeInspection.htmlInspections.XmlEntitiesInspection)1 EditorWindow (com.intellij.injected.editor.EditorWindow)1 Result (com.intellij.openapi.application.Result)1 RunResult (com.intellij.openapi.application.RunResult)1 WriteAction (com.intellij.openapi.application.WriteAction)1 Document (com.intellij.openapi.editor.Document)1 PsiElement (com.intellij.psi.PsiElement)1 HtmlTag (com.intellij.psi.html.HtmlTag)1 Nullable (org.jetbrains.annotations.Nullable)1