use of com.intellij.psi.javadoc.PsiDocComment 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;
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class JDParser method getElementsCommentInfo.
private static 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;
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class RefactoringUtil method fixJavadocsForParams.
public static void fixJavadocsForParams(PsiMethod method, Set<PsiParameter> newParameters, Condition<Pair<PsiParameter, String>> eqCondition, Condition<String> matchedToOldParam) throws IncorrectOperationException {
final PsiDocComment docComment = method.getDocComment();
if (docComment == null)
return;
final PsiParameter[] parameters = method.getParameterList().getParameters();
final PsiDocTag[] paramTags = docComment.findTagsByName("param");
if (parameters.length > 0 && newParameters.size() < parameters.length && paramTags.length == 0)
return;
Map<PsiParameter, PsiDocTag> tagForParam = new HashMap<>();
for (PsiParameter parameter : parameters) {
boolean found = false;
for (PsiDocTag paramTag : paramTags) {
if (parameter.getName().equals(getNameOfReferencedParameter(paramTag))) {
tagForParam.put(parameter, paramTag);
found = true;
break;
}
}
if (!found) {
for (PsiDocTag paramTag : paramTags) {
final String paramName = getNameOfReferencedParameter(paramTag);
if (eqCondition.value(Pair.create(parameter, paramName))) {
tagForParam.put(parameter, paramTag);
found = true;
break;
}
}
}
if (!found && !newParameters.contains(parameter)) {
tagForParam.put(parameter, null);
}
}
List<PsiDocTag> newTags = new ArrayList<>();
for (PsiDocTag paramTag : paramTags) {
final String paramName = getNameOfReferencedParameter(paramTag);
if (!tagForParam.containsValue(paramTag) && !matchedToOldParam.value(paramName)) {
newTags.add((PsiDocTag) paramTag.copy());
}
}
for (PsiParameter parameter : parameters) {
if (tagForParam.containsKey(parameter)) {
final PsiDocTag psiDocTag = tagForParam.get(parameter);
if (psiDocTag != null) {
final PsiDocTag copy = (PsiDocTag) psiDocTag.copy();
final PsiDocTagValue valueElement = copy.getValueElement();
if (valueElement != null) {
valueElement.replace(createParamTag(parameter).getValueElement());
}
newTags.add(copy);
}
} else {
newTags.add(createParamTag(parameter));
}
}
PsiElement anchor = paramTags.length > 0 ? paramTags[0].getPrevSibling() : null;
for (PsiDocTag paramTag : paramTags) {
paramTag.delete();
}
for (PsiDocTag psiDocTag : newTags) {
anchor = anchor != null && anchor.isValid() ? docComment.addAfter(psiDocTag, anchor) : docComment.add(psiDocTag);
}
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class JavaDocCommentFixer method fixComment.
@Override
public void fixComment(@NotNull Project project, @NotNull Editor editor, @NotNull PsiComment comment) {
if (!(comment instanceof PsiDocComment)) {
return;
}
PsiDocComment docComment = (PsiDocComment) comment;
PsiJavaDocumentedElement owner = docComment.getOwner();
if (owner == null) {
return;
}
PsiFile file = comment.getContainingFile();
if (file == null) {
return;
}
JavaDocReferenceInspection referenceInspection = new JavaDocReferenceInspection();
JavaDocLocalInspection localInspection = getDocLocalInspection();
InspectionManager inspectionManager = InspectionManager.getInstance(project);
ProblemDescriptor[] referenceProblems = null;
ProblemDescriptor[] otherProblems = null;
if (owner instanceof PsiClass) {
referenceProblems = referenceInspection.checkClass(((PsiClass) owner), inspectionManager, false);
otherProblems = localInspection.checkClass(((PsiClass) owner), inspectionManager, false);
} else if (owner instanceof PsiField) {
referenceProblems = referenceInspection.checkField(((PsiField) owner), inspectionManager, false);
otherProblems = localInspection.checkField(((PsiField) owner), inspectionManager, false);
} else if (owner instanceof PsiMethod) {
referenceProblems = referenceInspection.checkMethod((PsiMethod) owner, inspectionManager, false);
otherProblems = localInspection.checkMethod((PsiMethod) owner, inspectionManager, false);
}
if (referenceProblems != null) {
fixReferenceProblems(referenceProblems, project);
}
if (otherProblems != null) {
fixCommonProblems(otherProblems, comment, editor.getDocument(), project);
}
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
ensureContentOrdered(docComment, editor.getDocument());
locateCaret(docComment, editor, file);
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class JavaDocumentationProvider method generateDocumentationContentStub.
@Override
public String generateDocumentationContentStub(PsiComment _comment) {
final PsiJavaDocumentedElement commentOwner = ((PsiDocComment) _comment).getOwner();
final Project project = _comment.getProject();
final StringBuilder builder = new StringBuilder();
final CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter) LanguageCommenters.INSTANCE.forLanguage(_comment.getLanguage());
if (commentOwner instanceof PsiMethod) {
PsiMethod psiMethod = (PsiMethod) commentOwner;
generateParametersTakingDocFromSuperMethods(project, builder, commenter, psiMethod);
final PsiTypeParameterList typeParameterList = psiMethod.getTypeParameterList();
if (typeParameterList != null) {
createTypeParamsListComment(builder, project, commenter, typeParameterList);
}
if (psiMethod.getReturnType() != null && !PsiType.VOID.equals(psiMethod.getReturnType())) {
builder.append(CodeDocumentationUtil.createDocCommentLine(RETURN_TAG, project, commenter));
builder.append(LINE_SEPARATOR);
}
final PsiJavaCodeReferenceElement[] references = psiMethod.getThrowsList().getReferenceElements();
for (PsiJavaCodeReferenceElement reference : references) {
builder.append(CodeDocumentationUtil.createDocCommentLine(THROWS_TAG, project, commenter));
builder.append(reference.getText());
builder.append(LINE_SEPARATOR);
}
} else if (commentOwner instanceof PsiClass) {
final PsiTypeParameterList typeParameterList = ((PsiClass) commentOwner).getTypeParameterList();
if (typeParameterList != null) {
createTypeParamsListComment(builder, project, commenter, typeParameterList);
}
}
return builder.length() > 0 ? builder.toString() : null;
}
Aggregations