Search in sources :

Example 11 with PsiDocTag

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

the class JavadocTypedHandler method isAppropriatePlace.

private static boolean isAppropriatePlace(Editor editor, PsiFile file) {
    FileViewProvider provider = file.getViewProvider();
    int offset = editor.getCaretModel().getOffset();
    final PsiElement elementAtCaret;
    if (offset < editor.getDocument().getTextLength()) {
        elementAtCaret = provider.findElementAt(offset);
    } else {
        elementAtCaret = provider.findElementAt(editor.getDocument().getTextLength() - 1);
    }
    PsiElement element = elementAtCaret;
    while (element instanceof PsiWhiteSpace || element != null && containsOnlyWhiteSpaces(element.getText())) {
        element = element.getPrevSibling();
    }
    if (element == null) {
        return false;
    }
    if (element instanceof PsiDocParamRef) {
        element = element.getParent();
    }
    if (element instanceof PsiDocTag) {
        PsiDocTag tag = (PsiDocTag) element;
        if ("param".equals(tag.getName()) && isTypeParamBracketClosedAfterParamTag(tag, offset)) {
            return false;
        }
    }
    // The contents of inline tags is not HTML, so the paired tag completion isn't appropriate there.
    if (PsiTreeUtil.getParentOfType(element, PsiInlineDocTag.class, false) != null) {
        return false;
    }
    ASTNode node = element.getNode();
    return node != null && (JavaDocTokenType.ALL_JAVADOC_TOKENS.contains(node.getElementType()) || JavaDocElementType.ALL_JAVADOC_ELEMENTS.contains(node.getElementType()));
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) PsiInlineDocTag(com.intellij.psi.javadoc.PsiInlineDocTag) ASTNode(com.intellij.lang.ASTNode) PsiDocParamRef(com.intellij.psi.impl.source.javadoc.PsiDocParamRef)

Example 12 with PsiDocTag

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

the class JavadocHelper method parse.

/**
   * Returns information about all lines that contain javadoc parameters and are adjacent to the one that holds given offset.  
   *
   * @param psiFile       PSI holder for the document exposed the given editor
   * @param editor        target editor
   * @param offset        target offset that identifies anchor line to check
   * @return              pair like (javadoc info for the line identified by the given offset; list of javadoc parameter infos for
   *                      adjacent lines if any
   */
@SuppressWarnings("MethodMayBeStatic")
@NotNull
public Pair<JavadocParameterInfo, List<JavadocParameterInfo>> parse(@NotNull PsiFile psiFile, @NotNull Editor editor, int offset) {
    List<JavadocParameterInfo> result = new ArrayList<>();
    PsiDocumentManager.getInstance(psiFile.getProject()).commitDocument(editor.getDocument());
    final PsiElement elementAtCaret = psiFile.findElementAt(offset);
    if (elementAtCaret == null) {
        return EMPTY;
    }
    PsiDocTag tag = PsiTreeUtil.getParentOfType(elementAtCaret, PsiDocTag.class);
    if (tag == null) {
        // Due to javadoc PSI specifics.
        if (elementAtCaret instanceof PsiWhiteSpace) {
            for (PsiElement e = elementAtCaret.getPrevSibling(); e != null && tag == null; e = e.getPrevSibling()) {
                tag = PsiTreeUtil.getParentOfType(e, PsiDocTag.class, false);
                if (e instanceof PsiWhiteSpace || (e instanceof PsiDocToken && ((PsiDocToken) e).getTokenType() == JavaDocTokenType.DOC_COMMENT_LEADING_ASTERISKS)) {
                    continue;
                }
                break;
            }
        }
    }
    if (tag == null) {
        return EMPTY;
    }
    JavadocParameterInfo anchorInfo = parse(tag, editor);
    if (anchorInfo == null) {
        return EMPTY;
    }
    // Parse previous parameters.
    for (PsiElement e = tag.getPrevSibling(); e != null; e = e.getPrevSibling()) {
        JavadocParameterInfo info = parse(e, editor);
        if (info == null) {
            break;
        }
        result.add(0, info);
    }
    result.add(anchorInfo);
    // Parse subsequent parameters.
    for (PsiElement e = tag.getNextSibling(); e != null; e = e.getNextSibling()) {
        JavadocParameterInfo info = parse(e, editor);
        if (info == null) {
            break;
        }
        result.add(info);
    }
    return Pair.create(anchorInfo, result);
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) ArrayList(java.util.ArrayList) PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with PsiDocTag

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

the class JavaDocumentationProvider method generateParametersTakingDocFromSuperMethods.

public static void generateParametersTakingDocFromSuperMethods(Project project, StringBuilder builder, CodeDocumentationAwareCommenter commenter, PsiMethod psiMethod) {
    final PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
    final Map<String, String> param2Description = new HashMap<>();
    final PsiMethod[] superMethods = psiMethod.findSuperMethods();
    for (PsiMethod superMethod : superMethods) {
        final PsiDocComment comment = superMethod.getDocComment();
        if (comment != null) {
            final PsiDocTag[] params = comment.findTagsByName("param");
            for (PsiDocTag param : params) {
                final PsiElement[] dataElements = param.getDataElements();
                if (dataElements != null) {
                    String paramName = null;
                    for (PsiElement dataElement : dataElements) {
                        if (dataElement instanceof PsiDocParamRef) {
                            //noinspection ConstantConditions
                            paramName = dataElement.getReference().getCanonicalText();
                            break;
                        }
                    }
                    if (paramName != null) {
                        param2Description.put(paramName, param.getText());
                    }
                }
            }
        }
    }
    for (PsiParameter parameter : parameters) {
        String description = param2Description.get(parameter.getName());
        if (description != null) {
            builder.append(CodeDocumentationUtil.createDocCommentLine("", project, commenter));
            if (description.indexOf('\n') > -1)
                description = description.substring(0, description.lastIndexOf('\n'));
            builder.append(description);
        } else {
            builder.append(CodeDocumentationUtil.createDocCommentLine(PARAM_TAG, project, commenter));
            builder.append(parameter.getName());
        }
        builder.append(LINE_SEPARATOR);
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) HashMap(com.intellij.util.containers.HashMap) PsiDocParamRef(com.intellij.psi.impl.source.javadoc.PsiDocParamRef)

Example 14 with PsiDocTag

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

the class PsiDocCommentImpl method findTagByName.

@Override
public PsiDocTag findTagByName(String name) {
    if (getFirstChildNode().getElementType() == JavaDocElementType.DOC_COMMENT) {
        if (getFirstChildNode().getText().indexOf(name) < 0)
            return null;
    }
    for (ASTNode child = getFirstChildNode(); child != null; child = child.getTreeNext()) {
        if (child.getElementType() == DOC_TAG) {
            PsiDocTag tag = (PsiDocTag) SourceTreeToPsiMap.treeElementToPsi(child);
            final CharSequence nameText = ((LeafElement) tag.getNameElement()).getChars();
            if (nameText.length() > 0 && nameText.charAt(0) == '@' && CharArrayUtil.regionMatches(nameText, 1, name)) {
                return tag;
            }
        }
    }
    return null;
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) ASTNode(com.intellij.lang.ASTNode)

Example 15 with PsiDocTag

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

the class PsiDocCommentImpl method deleteChildInternal.

@Override
public void deleteChildInternal(@NotNull ASTNode child) {
    if (child.getElementType() == DOC_TAG) {
        if (child.getTreeNext() == null || child.getTreeNext().getElementType() != DOC_TAG) {
            ASTNode prev = child.getTreePrev();
            while (prev != null && prev.getElementType() == DOC_COMMENT_DATA) {
                prev = prev.getTreePrev();
            }
            ASTNode next = child.getTreeNext();
            while (next != null && (next.getElementType() == DOC_COMMENT_DATA || next.getElementType() == WHITE_SPACE)) {
                next = next.getTreeNext();
            }
            if (prev != null && prev.getElementType() == DOC_COMMENT_LEADING_ASTERISKS && !(next instanceof PsiDocTag)) {
                ASTNode leadingAsterisk = prev;
                if (leadingAsterisk.getTreePrev() != null) {
                    super.deleteChildInternal(leadingAsterisk.getTreePrev());
                    super.deleteChildInternal(leadingAsterisk);
                }
            } else if (prev != null && prev.getElementType() == DOC_TAG) {
                final CompositeElement compositePrev = (CompositeElement) prev;
                final ASTNode lastPrevChild = compositePrev.getLastChildNode();
                ASTNode prevChild = lastPrevChild;
                while (prevChild != null && prevChild.getElementType() == DOC_COMMENT_DATA) {
                    prevChild = prevChild.getTreePrev();
                }
                if (prevChild != null && prevChild.getElementType() == DOC_COMMENT_LEADING_ASTERISKS) {
                    ASTNode current = prevChild;
                    while (current != null) {
                        final ASTNode nextChild = current.getTreeNext();
                        compositePrev.deleteChildInternal(current);
                        current = nextChild;
                    }
                }
            } else {
                next = child.getTreeNext();
                if (next != null && next.getElementType() == WHITE_SPACE) {
                    next.getTreeParent().removeChild(next);
                }
            }
        }
    }
    super.deleteChildInternal(child);
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) ASTNode(com.intellij.lang.ASTNode)

Aggregations

PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)40 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)14 PsiDocTagValue (com.intellij.psi.javadoc.PsiDocTagValue)7 ASTNode (com.intellij.lang.ASTNode)5 ArrayList (java.util.ArrayList)5 MethodJavaDocHelper (com.intellij.refactoring.util.javadoc.MethodJavaDocHelper)4 NotNull (org.jetbrains.annotations.NotNull)4 PsiDocParamRef (com.intellij.psi.impl.source.javadoc.PsiDocParamRef)3 PsiDocToken (com.intellij.psi.javadoc.PsiDocToken)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 TextRange (com.intellij.openapi.util.TextRange)2 PsiInlineDocTag (com.intellij.psi.javadoc.PsiInlineDocTag)2 HashMap (com.intellij.util.containers.HashMap)2 TIntProcedure (gnu.trove.TIntProcedure)2 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 QuickFix (com.intellij.codeInspection.QuickFix)1 Annotation (com.intellij.lang.annotation.Annotation)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1