Search in sources :

Example 11 with PsiDocTagValue

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

the class DocDataHandler method getTextFromNode.

// since doctag value may be inside doc comment we specially build text including skipped nodes
private static String getTextFromNode(final PsiElement node, String text1) {
    PsiElement nextSibling = node.getNextSibling();
    if (nextSibling instanceof PsiDocTagValue) {
        text1 += nextSibling.getText();
        nextSibling = nextSibling.getNextSibling();
        if (nextSibling instanceof PsiDocToken && ((PsiDocToken) nextSibling).getTokenType() == JavaDocTokenType.DOC_COMMENT_DATA) {
            text1 += nextSibling.getText();
        }
    }
    return text1;
}
Also used : PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) PsiElement(com.intellij.psi.PsiElement)

Example 12 with PsiDocTagValue

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

the class JavaDocCommentFixer method ensureContentOrdered.

private static void ensureContentOrdered(@NotNull PsiDocComment comment, @NotNull Document document) {
    //region Parse existing doc comment parameters.
    List<String> current = new ArrayList<>();
    Map<String, Pair<TextRange, String>> tagInfoByName = new HashMap<>();
    for (PsiDocTag tag : comment.getTags()) {
        if (!PARAM_TAG_NAME.equals(tag.getName())) {
            continue;
        }
        PsiDocTagValue valueElement = tag.getValueElement();
        if (valueElement == null) {
            continue;
        }
        String paramName = valueElement.getText();
        if (paramName != null) {
            current.add(paramName);
            tagInfoByName.put(paramName, parseTagValue(tag, document));
        }
    }
    //endregion
    //region Calculate desired parameters order
    List<String> ordered = new ArrayList<>();
    PsiJavaDocumentedElement owner = comment.getOwner();
    if ((owner instanceof PsiMethod)) {
        PsiParameter[] parameters = ((PsiMethod) owner).getParameterList().getParameters();
        for (PsiParameter parameter : parameters) {
            ordered.add(parameter.getName());
        }
    }
    if (owner instanceof PsiTypeParameterListOwner) {
        PsiTypeParameter[] typeParameters = ((PsiTypeParameterListOwner) owner).getTypeParameters();
        for (PsiTypeParameter parameter : typeParameters) {
            ordered.add(String.format("<%s>", parameter.getName()));
        }
    }
    //region Fix order if necessary.
    if (current.size() != ordered.size()) {
        // Something is wrong, stop the processing.
        return;
    }
    boolean changed = false;
    for (int i = current.size() - 1; i >= 0; i--) {
        String newTag = ordered.get(i);
        String oldTag = current.get(i);
        if (newTag.equals(oldTag)) {
            continue;
        }
        TextRange range = tagInfoByName.get(oldTag).first;
        document.replaceString(range.getStartOffset(), range.getEndOffset(), tagInfoByName.get(newTag).second);
        changed = true;
    }
    if (changed) {
        PsiDocumentManager manager = PsiDocumentManager.getInstance(comment.getProject());
        manager.commitDocument(document);
    }
//endregion
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) TextRange(com.intellij.openapi.util.TextRange) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) Pair(com.intellij.openapi.util.Pair)

Example 13 with PsiDocTagValue

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

the class JavaDocCommentFixer method parseTagValue.

@NotNull
private static Pair<TextRange, String> parseTagValue(@NotNull PsiDocTag tag, @NotNull Document document) {
    PsiDocTagValue valueElement = tag.getValueElement();
    assert valueElement != null;
    int startOffset = valueElement.getTextRange().getStartOffset();
    int endOffset = tag.getTextRange().getEndOffset();
    // Javadoc PSI is rather weird...
    CharSequence text = document.getCharsSequence();
    int i = CharArrayUtil.shiftBackward(text, endOffset - 1, " \t*");
    if (i > 0 && text.charAt(i) == '\n') {
        endOffset = i;
    }
    return Pair.create(TextRange.create(startOffset, endOffset), text.subSequence(startOffset, endOffset).toString());
}
Also used : PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with PsiDocTagValue

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

the class JavadocHelper method parse.

@Nullable
private static JavadocParameterInfo parse(@NotNull PsiElement element, @NotNull Editor editor) {
    final PsiDocTag tag = PsiTreeUtil.getParentOfType(element, PsiDocTag.class, false);
    if (tag == null || !PARAM_TEXT.equals(tag.getName())) {
        return null;
    }
    final PsiDocTagValue paramRef = PsiTreeUtil.getChildOfType(tag, PsiDocTagValue.class);
    if (paramRef == null) {
        return null;
    }
    for (PsiElement e = paramRef.getNextSibling(); e != null; e = e.getNextSibling()) {
        final ASTNode node = e.getNode();
        if (node == null) {
            break;
        }
        final IElementType elementType = node.getElementType();
        if (elementType == JavaDocTokenType.DOC_COMMENT_DATA) {
            return new JavadocParameterInfo(editor.offsetToLogicalPosition(paramRef.getTextRange().getEndOffset()), editor.offsetToLogicalPosition(e.getTextRange().getStartOffset()), editor.getDocument().getLineNumber(e.getTextRange().getEndOffset()));
        } else if (elementType == JavaDocTokenType.DOC_COMMENT_LEADING_ASTERISKS) {
            break;
        }
    }
    return new JavadocParameterInfo(editor.offsetToLogicalPosition(paramRef.getTextRange().getEndOffset()), null, editor.getDocument().getLineNumber(paramRef.getTextRange().getEndOffset()));
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) ASTNode(com.intellij.lang.ASTNode) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiDocTagValue (com.intellij.psi.javadoc.PsiDocTagValue)14 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)7 NotNull (org.jetbrains.annotations.NotNull)4 Project (com.intellij.openapi.project.Project)3 PsiDocToken (com.intellij.psi.javadoc.PsiDocToken)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 UsageInfo (com.intellij.usageView.UsageInfo)3 ASTNode (com.intellij.lang.ASTNode)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElement (com.intellij.psi.PsiElement)2 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)2 Annotation (com.intellij.lang.annotation.Annotation)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 Pair (com.intellij.openapi.util.Pair)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 PsiDocParamRef (com.intellij.psi.impl.source.javadoc.PsiDocParamRef)1 CandidateInfo (com.intellij.psi.infos.CandidateInfo)1 PsiInlineDocTag (com.intellij.psi.javadoc.PsiInlineDocTag)1 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)1