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;
}
Aggregations