Search in sources :

Example 1 with PsiDocToken

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

the class DocValuesIterator method advanceToNext.

// since doctag value may be inside doc comment we specially skip that nodes from list
static PsiElement advanceToNext(PsiElement e) {
    PsiElement nextSibling = e.getNextSibling();
    if (nextSibling instanceof PsiDocTagValue)
        e = nextSibling;
    nextSibling = e.getNextSibling();
    if (nextSibling instanceof PsiDocToken && ((PsiDocToken) nextSibling).getTokenType() == tokenType) {
        e = nextSibling;
    }
    return e;
}
Also used : PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PsiDocToken

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

the class DocTagSelectioner method getDocTagRange.

public static TextRange getDocTagRange(PsiDocTag e, CharSequence documentText, int minOffset) {
    TextRange range = e.getTextRange();
    int endOffset = range.getEndOffset();
    int startOffset = range.getStartOffset();
    PsiElement[] children = e.getChildren();
    for (int i = children.length - 1; i >= 0; i--) {
        PsiElement child = children[i];
        int childStartOffset = child.getTextRange().getStartOffset();
        if (childStartOffset <= minOffset) {
            break;
        }
        if (child instanceof PsiDocToken) {
            PsiDocToken token = (PsiDocToken) child;
            IElementType type = token.getTokenType();
            char[] chars = token.textToCharArray();
            int shift = CharArrayUtil.shiftForward(chars, 0, " \t\n\r");
            if (shift != chars.length && type != JavaDocTokenType.DOC_COMMENT_LEADING_ASTERISKS) {
                break;
            }
        } else if (!(child instanceof PsiWhiteSpace)) {
            break;
        }
        endOffset = Math.min(childStartOffset, endOffset);
    }
    startOffset = CharArrayUtil.shiftBackward(documentText, startOffset - 1, "* \t") + 1;
    return new TextRange(startOffset, endOffset);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 3 with PsiDocToken

use of com.intellij.psi.javadoc.PsiDocToken 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 4 with PsiDocToken

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

the class PsiDocParamRef method getReference.

@Override
public PsiReference getReference() {
    final PsiDocComment comment = PsiTreeUtil.getParentOfType(this, PsiDocComment.class);
    if (comment == null)
        return null;
    final PsiJavaDocumentedElement owner = comment.getOwner();
    if (!(owner instanceof PsiMethod) && !(owner instanceof PsiClass))
        return null;
    final ASTNode valueToken = findChildByType(JavaDocTokenType.DOC_TAG_VALUE_TOKEN);
    if (valueToken == null)
        return null;
    final String name = valueToken.getText();
    PsiElement reference = null;
    final PsiElement firstChild = getFirstChild();
    if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
        final PsiTypeParameter[] typeParameters = ((PsiTypeParameterListOwner) owner).getTypeParameters();
        for (PsiTypeParameter typeParameter : typeParameters) {
            if (typeParameter.getName().equals(name)) {
                reference = typeParameter;
            }
        }
    } else if (owner instanceof PsiMethod) {
        final PsiParameter[] parameters = ((PsiMethod) owner).getParameterList().getParameters();
        for (PsiParameter parameter : parameters) {
            if (parameter.getName().equals(name)) {
                reference = parameter;
            }
        }
    }
    final PsiElement resultReference = reference;
    return new PsiJavaReference() {

        @Override
        public PsiElement resolve() {
            return resultReference;
        }

        @Override
        @NotNull
        public String getCanonicalText() {
            return valueToken.getText();
        }

        @Override
        public PsiElement handleElementRename(String newElementName) {
            final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(getNode());
            LeafElement newElement = Factory.createSingleLeafElement(JavaDocTokenType.DOC_TAG_VALUE_TOKEN, newElementName, charTableByTree, getManager());
            replaceChild(valueToken, newElement);
            return PsiDocParamRef.this;
        }

        @Override
        public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
            if (isReferenceTo(element))
                return PsiDocParamRef.this;
            if (!(element instanceof PsiParameter)) {
                throw new IncorrectOperationException("Unsupported operation");
            }
            return handleElementRename(((PsiParameter) element).getName());
        }

        @Override
        public boolean isReferenceTo(PsiElement element) {
            if (!(element instanceof PsiNamedElement))
                return false;
            PsiNamedElement namedElement = (PsiNamedElement) element;
            if (!getCanonicalText().equals(namedElement.getName()))
                return false;
            return getManager().areElementsEquivalent(resolve(), element);
        }

        @Override
        @NotNull
        public PsiElement[] getVariants() {
            final PsiElement firstChild = getFirstChild();
            Set<String> usedNames = new HashSet<>();
            for (PsiDocTag tag : comment.getTags()) {
                if (tag.getName().equals("param")) {
                    PsiDocTagValue valueElement = tag.getValueElement();
                    if (valueElement != null) {
                        usedNames.add(valueElement.getText());
                    }
                }
            }
            PsiNamedElement[] result = PsiNamedElement.EMPTY_ARRAY;
            if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
                result = ((PsiTypeParameterListOwner) owner).getTypeParameters();
            } else if (owner instanceof PsiMethod) {
                result = ((PsiMethod) owner).getParameterList().getParameters();
            }
            List<PsiElement> filtered = new ArrayList<>();
            for (PsiNamedElement namedElement : result) {
                if (!usedNames.contains(namedElement.getName())) {
                    filtered.add(namedElement);
                }
            }
            return filtered.toArray(new PsiElement[filtered.size()]);
        }

        @Override
        public boolean isSoft() {
            return false;
        }

        @Override
        public TextRange getRangeInElement() {
            final int startOffsetInParent = valueToken.getPsi().getStartOffsetInParent();
            return new TextRange(startOffsetInParent, startOffsetInParent + valueToken.getTextLength());
        }

        @Override
        public PsiElement getElement() {
            return PsiDocParamRef.this;
        }

        @Override
        public void processVariants(@NotNull PsiScopeProcessor processor) {
            for (final PsiElement element : getVariants()) {
                if (!processor.execute(element, ResolveState.initial())) {
                    return;
                }
            }
        }

        @Override
        @NotNull
        public JavaResolveResult advancedResolve(boolean incompleteCode) {
            return resultReference == null ? JavaResolveResult.EMPTY : new CandidateInfo(resultReference, PsiSubstitutor.EMPTY);
        }

        @Override
        @NotNull
        public JavaResolveResult[] multiResolve(boolean incompleteCode) {
            return resultReference == null ? JavaResolveResult.EMPTY_ARRAY : new JavaResolveResult[] { new CandidateInfo(resultReference, PsiSubstitutor.EMPTY) };
        }
    };
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) CandidateInfo(com.intellij.psi.infos.CandidateInfo) ArrayList(java.util.ArrayList) PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) CharTable(com.intellij.util.CharTable) NotNull(org.jetbrains.annotations.NotNull) ASTNode(com.intellij.lang.ASTNode) HashSet(java.util.HashSet) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) TextRange(com.intellij.openapi.util.TextRange) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 5 with PsiDocToken

use of com.intellij.psi.javadoc.PsiDocToken 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)

Aggregations

PsiDocToken (com.intellij.psi.javadoc.PsiDocToken)7 PsiElement (com.intellij.psi.PsiElement)4 TextRange (com.intellij.openapi.util.TextRange)3 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)3 PsiDocTagValue (com.intellij.psi.javadoc.PsiDocTagValue)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 QuickFix (com.intellij.codeInspection.QuickFix)1 ASTNode (com.intellij.lang.ASTNode)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 CandidateInfo (com.intellij.psi.infos.CandidateInfo)1 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)1 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)1 IElementType (com.intellij.psi.tree.IElementType)1 CharTable (com.intellij.util.CharTable)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 HashSet (java.util.HashSet)1