Search in sources :

Example 1 with CodeDocumentationAwareCommenter

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);
    }
}
Also used : Project(com.intellij.openapi.project.Project) CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Example 2 with CodeDocumentationAwareCommenter

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);
            }
        }
    }
}
Also used : CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter) Project(com.intellij.openapi.project.Project) CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter) Commenter(com.intellij.lang.Commenter) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 3 with CodeDocumentationAwareCommenter

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;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) Project(com.intellij.openapi.project.Project) CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter)

Example 4 with CodeDocumentationAwareCommenter

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;
}
Also used : CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter) PsiComment(com.intellij.psi.PsiComment) Language(com.intellij.lang.Language) CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter) Commenter(com.intellij.lang.Commenter)

Example 5 with CodeDocumentationAwareCommenter

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);
}
Also used : CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter)

Aggregations

CodeDocumentationAwareCommenter (com.intellij.lang.CodeDocumentationAwareCommenter)5 Project (com.intellij.openapi.project.Project)3 Commenter (com.intellij.lang.Commenter)2 Language (com.intellij.lang.Language)1 PsiComment (com.intellij.psi.PsiComment)1 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)1 GrDocCommentOwner (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner)1 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)1 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)1