+ public void formatCommentText(@NotNull PsiElement element, @NotNull CommentFormatter formatter) {
+ CommentInfo info = getElementsCommentInfo(element);
+ JDComment comment = info != null ? parse(info, formatter) : null;
+ if (comment != null) {
+ String indent = formatter.getIndent(info.getCommentOwner());
+ String commentText = comment.generate(indent);
+ formatter.replaceCommentText(commentText, (PsiDocComment)info.psiComment);
+ }
+ }
+
+ private CommentInfo getElementsCommentInfo(@Nullable PsiElement psiElement) {
+ CommentInfo info = null;
+ if (psiElement instanceof PsiDocComment) {
+ final PsiDocComment docComment = (PsiDocComment)psiElement;
+ if (docComment.getOwner() == null && docComment.getParent() instanceof PsiJavaFile) {
+ info = CommentFormatter.getCommentInfo(docComment);
+ if (info != null) {
+ info.setCommentOwner(docComment);
+ info.setComment(docComment);
+ }
+ }
+ else {
+ return getElementsCommentInfo(psiElement.getParent());
+ }
+ }
+ else if (psiElement instanceof PsiDocCommentOwner) {
+ PsiDocCommentOwner owner = (PsiDocCommentOwner)psiElement;
+ info = CommentFormatter.getOrigCommentInfo(owner);
+ if (info != null) {
+ info.setCommentOwner(owner);
+ info.setComment(owner.getDocComment());
+ }
+ }
+ return info;
+ }
+
+ private JDComment parse(@NotNull CommentInfo info, @NotNull CommentFormatter formatter) {
+ PsiElement owner = info.getCommentOwner();
+ JDComment comment = createComment(owner, formatter);
+ if (comment == null) return null;
+
+ parse(info.comment, comment);
+ if (info.commentHeader != null) {
+ comment.setFirstCommentLine(info.commentHeader);
+ }
+ if (info.commentFooter != null) {
+ comment.setLastCommentLine(info.commentFooter);
+ }
+
+ return comment;
+ }
+
+ private JDComment createComment(@NotNull PsiElement psiElement, @NotNull CommentFormatter formatter) {
+ if (psiElement instanceof PsiClass) {
+ return new JDClassComment(formatter);
+ }
+ else if (psiElement instanceof PsiMethod) {
+ return new JDMethodComment(formatter);
+ }
+ else if (psiElement instanceof PsiField || psiElement instanceof PsiDocComment) {
+ return new JDComment(formatter);
+ }
+ return null;
+ }