void (final @NotNull Project project, final @NotNull ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); final PsiFile containingFile = element.getContainingFile(); @NonNls String prefixToInsert = ""; @NonNls String suffixToInsert = ""; final int UNDEFINED_OFFSET = -1; int anchorOffset = UNDEFINED_OFFSET; PsiElement anchor = PsiTreeUtil.getParentOfType(element, XmlElementDecl.class, XmlAttlistDecl.class, XmlEntityDecl.class, XmlConditionalSection.class); if (anchor != null) anchorOffset = anchor.getTextRange().getStartOffset(); if (anchorOffset == UNDEFINED_OFFSET && containingFile.getLanguage() == XMLLanguage.INSTANCE) { XmlFile file = (XmlFile)containingFile; final XmlProlog prolog = file.getDocument().getProlog(); assert prolog != null; final XmlDoctype doctype = prolog.getDoctype(); final XmlMarkupDecl markupDecl; if (doctype != null) { markupDecl = doctype.getMarkupDecl(); } else { markupDecl = null; } if (doctype == null) { final XmlTag rootTag = file.getDocument().getRootTag(); prefixToInsert = "<!DOCTYPE " + ((rootTag != null) ? rootTag.getName() : "null"); suffixToInsert = ">\n"; } if (markupDecl == null) { prefixToInsert += " [\n"; suffixToInsert = "]" + suffixToInsert; if (doctype != null) { anchorOffset = doctype.getTextRange().getEndOffset() - 1; // just before last '>' } else { anchorOffset = prolog.getTextRange().getEndOffset(); } } } if (anchorOffset == UNDEFINED_OFFSET) anchorOffset = element.getTextRange().getStartOffset(); OpenFileDescriptor openDescriptor = new OpenFileDescriptor(project, containingFile.getVirtualFile(), anchorOffset); final Editor editor = FileEditorManager.getInstance(project).openTextEditor(openDescriptor, true); final TemplateManager templateManager = TemplateManager.getInstance(project); final Template t = templateManager.createTemplate("", ""); if (!prefixToInsert.isEmpty()) t.addTextSegment(prefixToInsert); t.addTextSegment("<!" + myElementDeclarationName + " " + myReference + " "); t.addEndVariable(); t.addTextSegment(">\n"); if (!suffixToInsert.isEmpty()) t.addTextSegment(suffixToInsert); templateManager.startTemplate(editor, t); }