Element (@NotNull TemplateGroup template) { Element templateSetElement = new Element(TEMPLATE_SET); List<TemplateImpl> elements = template.getElements(); if (!elements.isEmpty()) { boolean isGroupAttributeAdded = false; Lazy<Map<String, TemplateContextType>> idToType = TemplateContext.getIdToType(); LiveTemplateContextsSnapshot allContexts = LiveTemplateContextService.getInstance().getSnapshot(); for (TemplateImpl t : elements) { TemplateImpl defaultTemplate = getDefaultTemplate(t); if (defaultTemplate == null || !t.equals(defaultTemplate) || !contextsEqual(allContexts, t, defaultTemplate)) { if (!isGroupAttributeAdded) { isGroupAttributeAdded = true; // add attribute only if not empty to avoid empty file (due to group attribute element will be not considered as empty) templateSetElement.setAttribute(GROUP, template.getName()); } templateSetElement.addContent(serializeTemplate(t, defaultTemplate, idToType)); } } } template.setModified(false); return templateSetElement; }