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));
}
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();
}
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;
}
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);
}
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);
}
Aggregations