Search in sources :

Example 1 with CommentFormatter

use of com.intellij.psi.impl.source.codeStyle.javadoc.CommentFormatter in project intellij-community by JetBrains.

the class FormatCommentsProcessor method formatCommentsInner.

/**
   * Formats PsiDocComments of current ASTNode element and all his children PsiDocComments
   */
@NotNull
private static TextRange formatCommentsInner(@NotNull Project project, @NotNull ASTNode element, @NotNull final TextRange markedRange) {
    TextRange resultTextRange = markedRange;
    final PsiElement elementPsi = element.getPsi();
    boolean shouldFormat = markedRange.contains(element.getTextRange());
    if (shouldFormat) {
        final ASTNode rangeAnchor;
        // its parent.
        if (elementPsi instanceof PsiDocComment) {
            rangeAnchor = element.getTreeParent();
        } else {
            rangeAnchor = element;
        }
        TextRange before = rangeAnchor.getTextRange();
        new CommentFormatter(project).processComment(element);
        int deltaRange = rangeAnchor.getTextRange().getLength() - before.getLength();
        resultTextRange = new TextRange(markedRange.getStartOffset(), markedRange.getEndOffset() + deltaRange);
    }
    // If element is out of range its children are also out of range. So in both cases formatting is finished. It's just for optimization.
    if ((shouldFormat && (elementPsi instanceof PsiMethod || elementPsi instanceof PsiField || elementPsi instanceof PsiDocComment)) || markedRange.getEndOffset() < element.getStartOffset()) {
        return resultTextRange;
    }
    ASTNode current = element.getFirstChildNode();
    while (current != null) {
        // When element is PsiClass its PsiDocComment is formatted up to this moment, so we didn't need to format it again.
        if (!(shouldFormat && current.getPsi() instanceof PsiDocComment && elementPsi instanceof PsiClass)) {
            resultTextRange = formatCommentsInner(project, current, resultTextRange);
        }
        current = current.getTreeNext();
    }
    return resultTextRange;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiMethod(com.intellij.psi.PsiMethod) PsiField(com.intellij.psi.PsiField) ASTNode(com.intellij.lang.ASTNode) PsiClass(com.intellij.psi.PsiClass) TextRange(com.intellij.openapi.util.TextRange) CommentFormatter(com.intellij.psi.impl.source.codeStyle.javadoc.CommentFormatter) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ASTNode (com.intellij.lang.ASTNode)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiClass (com.intellij.psi.PsiClass)1 PsiElement (com.intellij.psi.PsiElement)1 PsiField (com.intellij.psi.PsiField)1 PsiMethod (com.intellij.psi.PsiMethod)1 CommentFormatter (com.intellij.psi.impl.source.codeStyle.javadoc.CommentFormatter)1 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)1 NotNull (org.jetbrains.annotations.NotNull)1