void () { final MyElement rootElement = createPhysicalElement( """ <?xml version='1.0' encoding='UTF-8'?> <a> <boy> </boy> <girl/> </a>"""); myCallRegistry.clear(); assertEquals(1, rootElement.getBoys().size()); assertEquals(1, rootElement.getGirls().size()); final MyElement oldBoy = rootElement.getBoys().get(0); final XmlTag tag = oldBoy.getXmlTag(); assertNotNull(tag); final int offset = tag.getTextOffset(); final int endoffset = offset + tag.getTextLength(); WriteCommandAction.runWriteCommandAction(getProject(), () -> { rootElement.getGirls().get(0).undefine(); final Document document = getDocument(DomUtil.getFile(rootElement)); PsiDocumentManager.getInstance(getProject()).doPostponedOperationsAndUnblockDocument(document); document.replaceString(offset + 1, offset + 1 + "boy".length(), "girl"); commitDocument(document); }); assertFalse(oldBoy.isValid()); assertEquals(0, rootElement.getBoys().size()); assertEquals(1, rootElement.getGirls().size()); WriteCommandAction.runWriteCommandAction(getProject(), () -> { final Document document = getDocument(DomUtil.getFile(rootElement)); document.replaceString(endoffset - "boy".length(), endoffset, "girl"); commitDocument(document); }); assertEquals(0, rootElement.getBoys().size()); assertEquals(1, rootElement.getGirls().size()); }