use of com.intellij.lang.CodeDocumentationAwareCommenter in project intellij-community by JetBrains.
the class GroovyDocumentationProvider method generateDocumentationContentStub.
@Override
public String generateDocumentationContentStub(PsiComment contextComment) {
if (!(contextComment instanceof GrDocComment)) {
return null;
}
final GrDocCommentOwner owner = GrDocCommentUtil.findDocOwner((GrDocComment) contextComment);
if (owner == null)
return null;
Project project = contextComment.getProject();
final CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter) LanguageCommenters.INSTANCE.forLanguage(owner.getLanguage());
StringBuilder builder = StringBuilderSpinAllocator.alloc();
try {
if (owner instanceof GrMethod) {
final GrMethod method = (GrMethod) owner;
JavaDocumentationProvider.generateParametersTakingDocFromSuperMethods(project, builder, commenter, method);
final PsiType returnType = method.getInferredReturnType();
if ((returnType != null || method.getModifierList().hasModifierProperty(GrModifier.DEF)) && !PsiType.VOID.equals(returnType)) {
builder.append(CodeDocumentationUtil.createDocCommentLine(RETURN_TAG, project, commenter));
builder.append(LINE_SEPARATOR);
}
final PsiClassType[] references = method.getThrowsList().getReferencedTypes();
for (PsiClassType reference : references) {
builder.append(CodeDocumentationUtil.createDocCommentLine(THROWS_TAG, project, commenter));
builder.append(reference.getClassName());
builder.append(LINE_SEPARATOR);
}
} else if (owner instanceof GrTypeDefinition) {
final PsiTypeParameterList typeParameterList = ((PsiClass) owner).getTypeParameterList();
if (typeParameterList != null) {
JavaDocumentationProvider.createTypeParamsListComment(builder, project, commenter, typeParameterList);
}
}
return builder.length() > 0 ? builder.toString() : null;
} finally {
StringBuilderSpinAllocator.dispose(builder);
}
}
use of com.intellij.lang.CodeDocumentationAwareCommenter in project intellij-community by JetBrains.
the class JoinLinesHandler method tryConvertEndOfLineComment.
private static void tryConvertEndOfLineComment(Document doc, PsiElement commentElement) {
Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(commentElement.getLanguage());
if (commenter instanceof CodeDocumentationAwareCommenter) {
CodeDocumentationAwareCommenter docCommenter = (CodeDocumentationAwareCommenter) commenter;
String lineCommentPrefix = commenter.getLineCommentPrefix();
String blockCommentPrefix = commenter.getBlockCommentPrefix();
String blockCommentSuffix = commenter.getBlockCommentSuffix();
if (commentElement.getNode().getElementType() == docCommenter.getLineCommentTokenType() && blockCommentPrefix != null && blockCommentSuffix != null && lineCommentPrefix != null) {
String commentText = StringUtil.trimStart(commentElement.getText(), lineCommentPrefix);
try {
Project project = commentElement.getProject();
PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project);
PsiComment newComment = parserFacade.createBlockCommentFromText(commentElement.getLanguage(), commentText);
commentElement.replace(newComment);
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(doc);
} catch (IncorrectOperationException e) {
LOG.info("Failed to replace line comment with block comment", e);
}
}
}
}
use of com.intellij.lang.CodeDocumentationAwareCommenter 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;
}
use of com.intellij.lang.CodeDocumentationAwareCommenter in project ideavim by JetBrains.
the class SearchHelper method findMatchingBlockCommentPair.
private static int findMatchingBlockCommentPair(@NotNull PsiElement element, int pos) {
final Language language = element.getLanguage();
final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language);
final PsiComment comment = PsiTreeUtil.getParentOfType(element, PsiComment.class, false);
if (comment != null) {
final int ret = findMatchingBlockCommentPair(comment, pos, commenter.getBlockCommentPrefix(), commenter.getBlockCommentSuffix());
if (ret >= 0) {
return ret;
}
if (commenter instanceof CodeDocumentationAwareCommenter) {
final CodeDocumentationAwareCommenter docCommenter = (CodeDocumentationAwareCommenter) commenter;
return findMatchingBlockCommentPair(comment, pos, docCommenter.getDocumentationCommentPrefix(), docCommenter.getDocumentationCommentSuffix());
}
}
return -1;
}
use of com.intellij.lang.CodeDocumentationAwareCommenter in project intellij-community by JetBrains.
the class CodeDocumentationUtil method tryParseCommentContext.
static CommentContext tryParseCommentContext(@Nullable Commenter langCommenter, @NotNull CharSequence chars, int offset, int lineStartOffset) {
final boolean isInsideCommentLikeCode = langCommenter instanceof CodeDocumentationAwareCommenter;
if (!isInsideCommentLikeCode) {
return new CommentContext();
}
final CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter) langCommenter;
int commentStartOffset = CharArrayUtil.shiftForward(chars, lineStartOffset, " \t");
boolean docStart = commenter.getDocumentationCommentPrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getDocumentationCommentPrefix());
boolean cStyleStart = commenter.getBlockCommentPrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getBlockCommentPrefix());
boolean docAsterisk = commenter.getDocumentationCommentLinePrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getDocumentationCommentLinePrefix());
final int firstNonSpaceInLine = CharArrayUtil.shiftForward(chars, offset, " \t");
boolean slashSlash = commenter.getLineCommentPrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getLineCommentPrefix()) && firstNonSpaceInLine < chars.length() && chars.charAt(firstNonSpaceInLine) != '\n';
return new CommentContext(commenter, docStart, cStyleStart, docAsterisk, slashSlash, commentStartOffset);
}
Aggregations