void (@NotNull XmlTag root) { final List<SmartPsiElementPointer<XmlTag>> tagToClose = new ArrayList<>(); Project project = root.getProject(); final SmartPointerManager pointerManager = SmartPointerManager.getInstance(project); root.accept(new XmlRecursiveElementVisitor() { @Override public void visitXmlTag(final @NotNull XmlTag tag) { if (!isTagClosed(tag)) { tagToClose.add(pointerManager.createSmartPsiElementPointer(tag)); } } }); for (SmartPsiElementPointer<XmlTag> pointer : tagToClose) { XmlTag element = pointer.getElement(); if (element == null) continue; String elementText = element.getText(); if (!elementText.endsWith(">")) continue; PsiFile text = PsiFileFactory.getInstance(root.getProject()) .createFileFromText("dummy.html", root.getLanguage(), StringUtil.trimEnd(element.getText(), ">") + "/>", false, true); XmlTag newTag = PsiTreeUtil.findChildOfType(text, XmlTag.class); if (newTag != null) { element.replace(newTag); } } }