Search in sources :

Example 1 with PsiDocCommentOwner

use of com.intellij.psi.PsiDocCommentOwner in project intellij-community by JetBrains.

the class AddJavadocIntention method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException {
    final PsiDocCommentOwner docCommentOwner = (PsiDocCommentOwner) element.getParent();
    FixDocCommentAction.generateOrFixComment(docCommentOwner, project, editor);
}
Also used : PsiDocCommentOwner(com.intellij.psi.PsiDocCommentOwner)

Example 2 with PsiDocCommentOwner

use of com.intellij.psi.PsiDocCommentOwner in project intellij-community by JetBrains.

the class JavaImplementationTextSelectioner method getTextStartOffset.

@Override
public int getTextStartOffset(@NotNull final PsiElement parent) {
    PsiElement element = parent;
    if (element instanceof PsiDocCommentOwner) {
        PsiDocComment comment = ((PsiDocCommentOwner) element).getDocComment();
        if (comment != null) {
            element = comment.getNextSibling();
            while (element instanceof PsiWhiteSpace) {
                element = element.getNextSibling();
            }
        }
    }
    if (element != null) {
        TextRange range = element.getTextRange();
        if (range != null) {
            return range.getStartOffset();
        }
        LOG.error("Range should not be null: " + element + "; " + element.getClass());
    }
    LOG.error("Element should not be null: " + parent.getText());
    return parent.getTextRange().getStartOffset();
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiDocCommentOwner(com.intellij.psi.PsiDocCommentOwner) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 3 with PsiDocCommentOwner

use of com.intellij.psi.PsiDocCommentOwner in project intellij-community by JetBrains.

the class GrDeprecatedAPIUsageInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression ref) {
            super.visitReferenceExpression(ref);
            checkRef(ref);
        }

        @Override
        public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement ref) {
            super.visitCodeReferenceElement(ref);
            checkRef(ref);
        }

        private void checkRef(GrReferenceElement ref) {
            PsiElement resolved = ref.resolve();
            if (isDeprecated(resolved)) {
                PsiElement toHighlight = getElementToHighlight(ref);
                registerError(toHighlight, GroovyBundle.message("0.is.deprecated", ref.getReferenceName()), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.LIKE_DEPRECATED);
            }
        }

        @NotNull
        public PsiElement getElementToHighlight(@NotNull GrReferenceElement refElement) {
            final PsiElement refNameElement = refElement.getReferenceNameElement();
            return refNameElement != null ? refNameElement : refElement;
        }

        private boolean isDeprecated(PsiElement resolved) {
            if (resolved instanceof PsiDocCommentOwner) {
                return ((PsiDocCommentOwner) resolved).isDeprecated();
            }
            if (resolved instanceof PsiModifierListOwner && PsiImplUtil.isDeprecatedByAnnotation((PsiModifierListOwner) resolved)) {
                return true;
            }
            return false;
        }
    };
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) PsiDocCommentOwner(com.intellij.psi.PsiDocCommentOwner) PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner) NotNull(org.jetbrains.annotations.NotNull) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PsiDocCommentOwner

use of com.intellij.psi.PsiDocCommentOwner in project markdown-doclet by Abnaxos.

the class DocCommentProcessor method processDocComment.

/**
 * Process the given `PsiDocComment` using Markdown and return a new `PsiDocComment`
 * representing the resulting HTML. The resulting `PsiDocComment` can then be passed
 * to IDEA's default QuickDoc implementation.
 *
 * @todo Error reporting ({@link com.sun.javadoc.DocErrorReporter} without {@link com.sun.javadoc.RootDoc}).
 *
 * @param docComment    The `PsiDocComment` to process.
 *
 * @return A `PsiDocComment` representing the resulting HTML.
 */
public PsiDocComment processDocComment(PsiDocComment docComment) {
    if (!isEnabled() || docComment == null) {
        return docComment;
    }
    final Map<String, URL> umlDiagrams = generateUmlDiagrams(docComment);
    Options options = new Options() {

        @Override
        protected ToHtmlSerializer createDocletSerializer() {
            return new DocletSerializer(this, getLinkRenderer()) {

                @Override
                protected void printImageTag(LinkRenderer.Rendering rendering) {
                    URL diagram = umlDiagrams.get(rendering.href);
                    if (diagram != null) {
                        super.printImageTag(mapHref(rendering, diagram.toString()));
                    } else if (!ABSOLUTE_IMG_RE.matcher(rendering.href).matches() || rendering.href.contains("{@}")) {
                        URL baseUrl = VfsUtilCore.convertToURL(file.getVirtualFile().getUrl());
                        if (baseUrl != null) {
                            try {
                                super.printImageTag(mapHref(rendering, new URL(baseUrl, rendering.href).toString()));
                            } catch (MalformedURLException e) {
                                super.printImageTag(rendering);
                            }
                        }
                    } else {
                        super.printImageTag(rendering);
                    }
                }

                private LinkRenderer.Rendering mapHref(LinkRenderer.Rendering rendering, String newHref) {
                    LinkRenderer.Rendering mapped = new LinkRenderer.Rendering(newHref, rendering.text);
                    mapped.attributes.addAll(rendering.attributes);
                    return mapped;
                }
            };
        }
    };
    PsiDocCommentOwner context = PsiTreeUtil.getParentOfType(docComment, PsiDocCommentOwner.class);
    markdownOptions.applyTo(options);
    MarkdownDoclet doclet = new MarkdownDoclet(options, null);
    MarkdownTaglets.instance().setDocErrorReporter(doclet);
    StringBuilder buf = new StringBuilder();
    StringBuilder tagBlock = new StringBuilder();
    boolean start = true;
    // List<String> tagBlockInlineTags = new ArrayList<>();
    for (PsiElement elem : docComment.getChildren()) {
        if (elem instanceof PsiDocToken) {
            IElementType tokenType = ((PsiDocToken) elem).getTokenType();
            if (tokenType == JavaDocTokenType.DOC_COMMENT_START || tokenType == JavaDocTokenType.DOC_COMMENT_END) {
                continue;
            }
        }
        if (start && elem instanceof PsiWhiteSpace) {
            continue;
        } else if (elem instanceof PsiInlineDocTag) {
            start = false;
            if (tagBlock.length() == 0) {
                // inlineTags.add(elem.getText());
                buf.append(elem.getText());
            } else {
                // tagBlockInlineTags.add(elem.getText());
                tagBlock.append(elem.getText());
            }
        } else if (elem instanceof PsiDocTag) {
            PsiDocTag docTag = (PsiDocTag) elem;
            switch(docTag.getName()) {
                case "see":
                    tagBlock.append('\n');
                    renderSeeTag(doclet, tagBlock, docTag);
                    break;
                case "param":
                case "throws":
                case "exception":
                    renderSimpleTag(doclet, tagBlock, docTag, true);
                    break;
                case "return":
                    renderSimpleTag(doclet, tagBlock, docTag, false);
                    break;
                case "todo":
                    renderTodoTag(doclet, tagBlock, docTag);
                    break;
                default:
                    tagBlock.append('\n').append(stripLead(elem.getText()));
                    break;
            }
        } else {
            start = false;
            if (tagBlock.length() == 0) {
                buf.append(elem.getText());
            } else {
                tagBlock.append(elem.getText());
            }
        }
    }
    String markdown = stripLead(buf.toString());
    Plugin.print("Markdown source", markdown);
    String docCommentText = "/**\n" + escapeAsterisks(doclet.toHtml(markdown, false)) + "\n" + escapeAsterisks(tagBlock.toString()) + "\n*/";
    Plugin.print("Processed DocComment", docCommentText);
    PsiDocComment processedDocComment = docCommentFromText(context, docCommentText);
    LinkExpander linkExpander = new LinkExpander(context, processedDocComment, docCommentText);
    processedDocComment = linkExpander.apply();
    return processedDocComment;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) Options(ch.raffael.mddoclet.Options) MalformedURLException(java.net.MalformedURLException) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) MarkdownDoclet(ch.raffael.mddoclet.MarkdownDoclet) DocletSerializer(ch.raffael.mddoclet.DocletSerializer) PsiDocCommentOwner(com.intellij.psi.PsiDocCommentOwner) PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) URL(java.net.URL) IElementType(com.intellij.psi.tree.IElementType) LinkRenderer(org.pegdown.LinkRenderer) PsiInlineDocTag(com.intellij.psi.javadoc.PsiInlineDocTag) TagRendering(ch.raffael.mddoclet.tags.TagRendering) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

PsiDocCommentOwner (com.intellij.psi.PsiDocCommentOwner)4 PsiElement (com.intellij.psi.PsiElement)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)2 DocletSerializer (ch.raffael.mddoclet.DocletSerializer)1 MarkdownDoclet (ch.raffael.mddoclet.MarkdownDoclet)1 Options (ch.raffael.mddoclet.Options)1 TagRendering (ch.raffael.mddoclet.tags.TagRendering)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiModifierListOwner (com.intellij.psi.PsiModifierListOwner)1 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)1 PsiDocToken (com.intellij.psi.javadoc.PsiDocToken)1 PsiInlineDocTag (com.intellij.psi.javadoc.PsiInlineDocTag)1 IElementType (com.intellij.psi.tree.IElementType)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 NotNull (org.jetbrains.annotations.NotNull)1 BaseInspectionVisitor (org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor)1 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1