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