CodeInsightActionHandler () { return new CodeInsightActionHandler(){ @Override public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { final XmlDocument document = findSuitableXmlDocument(file); if (document != null) { final @NonNls StringBuilder buffer = new StringBuilder(); buffer.append("<!DOCTYPE ").append(document.getRootTag().getName()).append(" [\n"); buffer.append(XmlUtil.generateDocumentDTD(document, true)); buffer.append("]>\n"); XmlFile tempFile; try{ final XmlProlog prolog = document.getProlog(); final PsiElement childOfType = PsiTreeUtil.getChildOfType(prolog, XmlProcessingInstruction.class); if (childOfType != null) { final String text = childOfType.getText(); buffer.insert(0,text); final PsiElement nextSibling = childOfType.getNextSibling(); if (nextSibling instanceof PsiWhiteSpace) { buffer.insert(text.length(),nextSibling.getText()); } } tempFile = (XmlFile)PsiFileFactory.getInstance(file.getProject()).createFileFromText("dummy.xml", buffer.toString()); prolog.replace(tempFile.getDocument().getProlog()); } catch (IncorrectOperationException e) { LOG.error(e); } } } }; }