public static void generateTag(@NotNull XmlTag newTag, Editor editor) { generateRaw(newTag); final XmlTag restored = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(newTag); if (restored == null) { LOG.error("Could not restore tag: " + newTag.getText()); } TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(restored); replaceElements(restored, builder); builder.run(editor, false); }
private static void replaceElements(XmlTag tag, TemplateBuilder builder) { for (XmlAttribute attribute : tag.getAttributes()) { XmlAttributeValue value = attribute.getValueElement(); if (value != null) { builder.replaceElement(value, TextRange.from(1, 0), new MacroCallNode(new CompleteMacro())); } } if ("<".equals(tag.getText())) { builder.replaceElement( tag, TextRange.from(1, 0), new MacroCallNode(new CompleteSmartMacro())); } else if (tag.getSubTags().length == 0) { int i = tag.getText().indexOf("></"); if (i > 0) { builder.replaceElement( tag, TextRange.from(i + 1, 0), new MacroCallNode(new CompleteMacro())); } } for (XmlTag subTag : tag.getSubTags()) { replaceElements(subTag, builder); } }
public ASTNode encodeXmlTextContents(String displayText, PsiElement text) { final PsiFile containingFile = text.getContainingFile(); CharTable charTable = SharedImplUtil.findCharTableByTree(text.getNode()); final FileElement dummyParent = DummyHolderFactory.createHolder(text.getManager(), null, charTable).getTreeElement(); final XmlTag rootTag = ((XmlFile) PsiFileFactory.getInstance(containingFile.getProject()) .createFileFromText("a.xml", "<a>" + displayText + "</a>")) .getRootTag(); assert rootTag != null; final XmlTagChild[] tagChildren = rootTag.getValue().getChildren(); final XmlTagChild child = tagChildren.length > 0 ? tagChildren[0] : null; LOG.assertTrue(child != null, "Child is null for tag: " + rootTag.getText()); final TreeElement element = (TreeElement) child.getNode(); ((TreeElement) tagChildren[tagChildren.length - 1].getNode().getTreeNext()).rawRemoveUpToLast(); dummyParent.rawAddChildren(element); TreeUtil.clearCaches(dummyParent); return element.getFirstChildNode(); }
protected static void forkResourceValue( @NotNull Project project, @NotNull XmlTag tag, @NotNull PsiFile file, @NotNull AndroidFacet facet, @Nullable PsiDirectory dir) { PsiDirectory resFolder = findRes(file); if (resFolder == null) { return; // shouldn't happen; we checked in isAvailable } String name = tag.getAttributeValue(ATTR_NAME); ResourceType type = AndroidResourceUtil.getResourceForResourceTag(tag); if (name == null || type == null) { return; // shouldn't happen; we checked in isAvailable } if (dir == null) { dir = selectFolderDir(project, resFolder.getVirtualFile(), ResourceFolderType.VALUES); } if (dir != null) { String value = PsiResourceItem.getTextContent(tag).trim(); createValueResource(file, facet, dir, name, value, type, tag.getText()); } }