boolean (HtmlFileImpl target, VirtualFile droppedFile, Editor editor, String tagTemplate) { final String tagText = prepareTagText(target, tagTemplate, getRelativePath(target, droppedFile)); final int offset = editor.getCaretModel().getOffset(); PsiElement element = target.findElementAt(offset); if (element == null || tagText == null) return false; final XmlElement parent = PsiTreeUtil.getParentOfType(element, XmlTag.class, XmlDocument.class); PsiElement candidate = null; for (PsiElement psiElement : parent.getChildren()) { if (psiElement.getTextRange().getEndOffset() >= offset) { candidate = psiElement; break; } } if (parent instanceof XmlTag && candidate != null) { final ASTNode startEnd = XmlChildRole.START_TAG_END_FINDER.findChild(parent.getNode()); if (startEnd != null && startEnd.getTextRange().getEndOffset() > candidate.getTextRange().getEndOffset()) { candidate = startEnd.getPsi(); } final ASTNode endStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(parent.getNode()); if (endStart != null && endStart.getTextRange().getStartOffset() <= candidate.getTextRange().getStartOffset()) { candidate = endStart.getPsi().getPrevSibling(); } } PsiElement insertionPoint = candidate; WriteCommandAction.writeCommandAction(target.getProject(), target).run(() -> { XmlTag tag = XmlElementFactory.getInstance(target.getProject()).createHTMLTagFromText(tagText); tag = (XmlTag)parent.addAfter(tag, insertionPoint); final ImageInfo info = HtmlReferenceProvider.SizeReference.getImageInfo(tag); if (info != null && info.width != 0 && info.height != 0) { tag.setAttribute("height", String.valueOf(info.height)); tag.setAttribute("width", String.valueOf(info.width)); } }); return true; }