Search in sources :

Example 1 with MacroCallNode

use of com.intellij.codeInsight.template.impl.MacroCallNode 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 MacroCallNode

use of com.intellij.codeInsight.template.impl.MacroCallNode in project intellij-community by JetBrains.

the class XmlTagInsertHandler method addRequiredSubTags.

private static boolean addRequiredSubTags(Template template, XmlElementDescriptor descriptor, PsiFile file, XmlTag context) {
    if (!WebEditorOptions.getInstance().isAutomaticallyInsertRequiredSubTags())
        return false;
    List<XmlElementDescriptor> requiredSubTags = GenerateXmlTagAction.getRequiredSubTags(descriptor);
    if (!requiredSubTags.isEmpty()) {
        template.addTextSegment(">");
        template.setToReformat(true);
    }
    for (XmlElementDescriptor subTag : requiredSubTags) {
        if (subTag == null) {
            // placeholder for smart completion
            template.addTextSegment("<");
            template.addVariable(new MacroCallNode(new CompleteSmartMacro()), true);
            continue;
        }
        String qname = subTag.getName();
        if (subTag instanceof XmlElementDescriptorImpl) {
            String prefixByNamespace = context.getPrefixByNamespace(((XmlElementDescriptorImpl) subTag).getNamespace());
            if (StringUtil.isNotEmpty(prefixByNamespace)) {
                qname = prefixByNamespace + ":" + subTag.getName();
            }
        }
        template.addTextSegment("<" + qname);
        addRequiredAttributes(subTag, null, template, file);
        completeTagTail(template, subTag, file, context, false);
    }
    if (!requiredSubTags.isEmpty()) {
        addTagEnd(template, descriptor, context);
    }
    return !requiredSubTags.isEmpty();
}
Also used : MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) XmlElementDescriptorImpl(com.intellij.xml.impl.schema.XmlElementDescriptorImpl) CompleteSmartMacro(com.intellij.codeInsight.template.macro.CompleteSmartMacro)

Example 3 with MacroCallNode

use of com.intellij.codeInsight.template.impl.MacroCallNode 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 4 with MacroCallNode

use of com.intellij.codeInsight.template.impl.MacroCallNode in project intellij-community by JetBrains.

the class HighlightUtils method showRenameTemplate.

public static void showRenameTemplate(PsiElement context, PsiNameIdentifierOwner element, PsiReference... references) {
    context = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(context);
    final Project project = context.getProject();
    final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    final Editor editor = fileEditorManager.getSelectedTextEditor();
    if (editor == null) {
        return;
    }
    final TemplateBuilderImpl builder = new TemplateBuilderImpl(context);
    final Expression macroCallNode = new MacroCallNode(new SuggestVariableNameMacro());
    final PsiElement identifier = element.getNameIdentifier();
    builder.replaceElement(identifier, "PATTERN", macroCallNode, true);
    for (PsiReference reference : references) {
        builder.replaceElement(reference, "PATTERN", "PATTERN", false);
    }
    final Template template = builder.buildInlineTemplate();
    final TextRange textRange = context.getTextRange();
    final int startOffset = textRange.getStartOffset();
    editor.getCaretModel().moveToOffset(startOffset);
    final TemplateManager templateManager = TemplateManager.getInstance(project);
    templateManager.startTemplate(editor, template);
}
Also used : PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) Template(com.intellij.codeInsight.template.Template) Project(com.intellij.openapi.project.Project) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) Expression(com.intellij.codeInsight.template.Expression) TemplateManager(com.intellij.codeInsight.template.TemplateManager) SuggestVariableNameMacro(com.intellij.codeInsight.template.macro.SuggestVariableNameMacro) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 5 with MacroCallNode

use of com.intellij.codeInsight.template.impl.MacroCallNode in project intellij-community by JetBrains.

the class ForeachPostfixTemplate method setVariables.

@Override
public void setVariables(@NotNull Template template, @NotNull PsiElement element) {
    MacroCallNode type = new MacroCallNode(new IterableComponentTypeMacro());
    MacroCallNode name = new MacroCallNode(new SuggestVariableNameMacro());
    type.addParameter(new VariableNode("expr", null));
    template.addVariable("type", type, type, false);
    template.addVariable("name", name, name, true);
}
Also used : VariableNode(com.intellij.codeInsight.template.impl.VariableNode) IterableComponentTypeMacro(com.intellij.codeInsight.template.macro.IterableComponentTypeMacro) SuggestVariableNameMacro(com.intellij.codeInsight.template.macro.SuggestVariableNameMacro) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode)

Aggregations

MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)11 CompleteMacro (com.intellij.codeInsight.template.macro.CompleteMacro)5 SuggestVariableNameMacro (com.intellij.codeInsight.template.macro.SuggestVariableNameMacro)5 Template (com.intellij.codeInsight.template.Template)4 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)4 TemplateManager (com.intellij.codeInsight.template.TemplateManager)4 Project (com.intellij.openapi.project.Project)3 PsiElement (com.intellij.psi.PsiElement)3 Expression (com.intellij.codeInsight.template.Expression)2 Editor (com.intellij.openapi.editor.Editor)2 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiReference (com.intellij.psi.PsiReference)2 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 XmlTag (com.intellij.psi.xml.XmlTag)2 TypeExpression (com.intellij.codeInsight.intention.impl.TypeExpression)1 TextExpression (com.intellij.codeInsight.template.impl.TextExpression)1 VariableNode (com.intellij.codeInsight.template.impl.VariableNode)1 CompleteSmartMacro (com.intellij.codeInsight.template.macro.CompleteSmartMacro)1 IterableComponentTypeMacro (com.intellij.codeInsight.template.macro.IterableComponentTypeMacro)1