Search in sources :

Example 66 with TagElement

use of org.eclipse.jdt.core.dom.TagElement in project eclipse.jdt.ls by eclipse.

the class JavadocContentAccess2 method parameterToHTML.

private void parameterToHTML() {
    String elementName = fElement.getElementName();
    List<TagElement> tags = fJavadoc.tags();
    for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext(); ) {
        TagElement tag = iter.next();
        String tagName = tag.getTagName();
        if (TagElement.TAG_PARAM.equals(tagName)) {
            List<? extends ASTNode> fragments = tag.fragments();
            int size = fragments.size();
            if (size > 0) {
                Object first = fragments.get(0);
                if (first instanceof SimpleName) {
                    String name = ((SimpleName) first).getIdentifier();
                    if (elementName.equals(name)) {
                        handleContentElements(fragments.subList(1, size));
                        return;
                    }
                } else if (size > 2 && fElement instanceof ITypeParameter && first instanceof TextElement) {
                    String firstText = ((TextElement) first).getText();
                    if ("<".equals(firstText)) {
                        // $NON-NLS-1$
                        Object second = fragments.get(1);
                        Object third = fragments.get(2);
                        if (second instanceof SimpleName && third instanceof TextElement) {
                            String name = ((SimpleName) second).getIdentifier();
                            String thirdText = ((TextElement) third).getText();
                            if (elementName.equals(name) && ">".equals(thirdText)) {
                                // $NON-NLS-1$
                                handleContentElements(fragments.subList(3, size));
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
    if (fElement instanceof ILocalVariable) {
        List<String> parameterNames = initParameterNames();
        int i = parameterNames.indexOf(elementName);
        if (i != -1) {
            CharSequence inheritedParamDescription = fJavadocLookup.getInheritedParamDescription(fMethod, i);
            handleInherited(inheritedParamDescription);
        }
    } else if (fElement instanceof ITypeParameter) {
        List<String> typeParameterNames = initTypeParameterNames();
        int i = typeParameterNames.indexOf(elementName);
        if (i != -1) {
            CharSequence inheritedTypeParamDescription = fJavadocLookup.getInheritedTypeParamDescription(fMethod, i);
            handleInherited(inheritedTypeParamDescription);
        }
    }
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) TextElement(org.eclipse.jdt.core.dom.TextElement) TagElement(org.eclipse.jdt.core.dom.TagElement) List(java.util.List) ArrayList(java.util.ArrayList)

Example 67 with TagElement

use of org.eclipse.jdt.core.dom.TagElement in project eclipse.jdt.ls by eclipse.

the class JavadocContentAccess2 method getMainDescription.

CharSequence getMainDescription() {
    if (fMainDescription == null) {
        fMainDescription = new StringBuffer();
        fBuf = fMainDescription;
        fLiteralContent = 0;
        List<TagElement> tags = fJavadoc.tags();
        for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext(); ) {
            TagElement tag = iter.next();
            String tagName = tag.getTagName();
            if (tagName == null) {
                handleContentElements(tag.fragments());
                break;
            }
        }
        fBuf = null;
    }
    return fMainDescription.length() > 0 ? fMainDescription : null;
}
Also used : TagElement(org.eclipse.jdt.core.dom.TagElement)

Example 68 with TagElement

use of org.eclipse.jdt.core.dom.TagElement in project eclipse.jdt.ls by eclipse.

the class JavadocContentAccess2 method handleContentElements.

private void handleContentElements(List<? extends ASTNode> nodes, boolean skipLeadingWhitespace) {
    ASTNode previousNode = null;
    for (Iterator<? extends ASTNode> iter = nodes.iterator(); iter.hasNext(); ) {
        ASTNode child = iter.next();
        if (previousNode != null) {
            int previousEnd = previousNode.getStartPosition() + previousNode.getLength();
            int childStart = child.getStartPosition();
            if (previousEnd > childStart) {
                // should never happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=304826
                Exception exception = new Exception(// $NON-NLS-1$
                "Illegal ASTNode positions: previousEnd=" + previousEnd + ", childStart=" + // $NON-NLS-1$
                childStart + ", element=" + // $NON-NLS-1$
                fElement.getHandleIdentifier() + ", Javadoc:\n" + // $NON-NLS-1$
                fSource);
            } else if (previousEnd != childStart) {
                // Need to preserve whitespace before a node that's not
                // directly following the previous node (e.g. on a new line)
                // due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=206518 :
                String textWithStars = fSource.substring(previousEnd, childStart);
                String text = removeDocLineIntros(textWithStars);
                fBuf.append(text);
            }
        }
        previousNode = child;
        if (child instanceof TextElement) {
            String text = ((TextElement) child).getText();
            if (JavaDocHTMLPathHandler.containsHTMLTag(text)) {
                text = JavaDocHTMLPathHandler.getValidatedHTMLSrcAttribute((TextElement) child, fElement);
            }
            if (skipLeadingWhitespace) {
                // $NON-NLS-1$ //$NON-NLS-2$
                text = text.replaceFirst("^\\s", "");
            }
            // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=233481 :
            // $NON-NLS-1$ //$NON-NLS-2$
            text = text.replaceAll("(\r\n?|\n)([ \t]*\\*)", "$1");
            handleText(text);
        } else if (child instanceof TagElement) {
            handleInlineTagElement((TagElement) child);
        } else {
            // This is unexpected. Fail gracefully by just copying the source.
            int start = child.getStartPosition();
            String text = fSource.substring(start, start + child.getLength());
            fBuf.append(removeDocLineIntros(text));
        }
    }
}
Also used : TextElement(org.eclipse.jdt.core.dom.TextElement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement) URISyntaxException(java.net.URISyntaxException) CoreException(org.eclipse.core.runtime.CoreException) JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException)

Example 69 with TagElement

use of org.eclipse.jdt.core.dom.TagElement in project eclipse.jdt.ls by eclipse.

the class JavadocContentAccess2 method getInheritedTypeParamDescription.

CharSequence getInheritedTypeParamDescription(int typeParamIndex) {
    if (fMethod != null) {
        List<String> typeParameterNames = initTypeParameterNames();
        if (fTypeParamDescriptions == null) {
            fTypeParamDescriptions = new StringBuffer[typeParameterNames.size()];
        } else {
            StringBuffer description = fTypeParamDescriptions[typeParamIndex];
            if (description != null) {
                return description.length() > 0 ? description : null;
            }
        }
        StringBuffer description = new StringBuffer();
        fTypeParamDescriptions[typeParamIndex] = description;
        fBuf = description;
        fLiteralContent = 0;
        String typeParamName = typeParameterNames.get(typeParamIndex);
        List<TagElement> tags = fJavadoc.tags();
        for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext(); ) {
            TagElement tag = iter.next();
            String tagName = tag.getTagName();
            if (TagElement.TAG_PARAM.equals(tagName)) {
                List<? extends ASTNode> fragments = tag.fragments();
                if (fragments.size() > 2) {
                    Object first = fragments.get(0);
                    Object second = fragments.get(1);
                    Object third = fragments.get(2);
                    if (first instanceof TextElement && second instanceof SimpleName && third instanceof TextElement) {
                        String firstText = ((TextElement) first).getText();
                        String thirdText = ((TextElement) third).getText();
                        if ("<".equals(firstText) && ">".equals(thirdText)) {
                            // $NON-NLS-1$ //$NON-NLS-2$
                            String name = ((SimpleName) second).getIdentifier();
                            if (name.equals(typeParamName)) {
                                handleContentElements(fragments.subList(3, fragments.size()));
                                break;
                            }
                        }
                    }
                }
            }
        }
        fBuf = null;
        return description.length() > 0 ? description : null;
    }
    return null;
}
Also used : TextElement(org.eclipse.jdt.core.dom.TextElement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) TagElement(org.eclipse.jdt.core.dom.TagElement)

Example 70 with TagElement

use of org.eclipse.jdt.core.dom.TagElement in project eclipse.jdt.ls by eclipse.

the class JavadocContentAccess2 method elementToHTML.

private void elementToHTML() {
    // After first loop, non-null entries in the following two lists are missing and need to be inherited:
    List<String> typeParameterNames = initTypeParameterNames();
    List<String> parameterNames = initParameterNames();
    List<String> exceptionNames = initExceptionNames();
    TagElement deprecatedTag = null;
    TagElement start = null;
    List<TagElement> typeParameters = new ArrayList<>();
    List<TagElement> parameters = new ArrayList<>();
    TagElement returnTag = null;
    List<TagElement> exceptions = new ArrayList<>();
    List<TagElement> versions = new ArrayList<>();
    List<TagElement> authors = new ArrayList<>();
    List<TagElement> sees = new ArrayList<>();
    List<TagElement> since = new ArrayList<>();
    List<TagElement> rest = new ArrayList<>();
    List<TagElement> apinote = new ArrayList<>(1);
    List<TagElement> tags = fJavadoc.tags();
    for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext(); ) {
        TagElement tag = iter.next();
        String tagName = tag.getTagName();
        if (tagName == null) {
            start = tag;
        } else if (TagElement.TAG_PARAM.equals(tagName)) {
            List<? extends ASTNode> fragments = tag.fragments();
            int size = fragments.size();
            if (size > 0) {
                Object first = fragments.get(0);
                if (first instanceof SimpleName) {
                    String name = ((SimpleName) first).getIdentifier();
                    int paramIndex = parameterNames.indexOf(name);
                    if (paramIndex != -1) {
                        parameterNames.set(paramIndex, null);
                    }
                    parameters.add(tag);
                } else if (size > 2 && first instanceof TextElement) {
                    String firstText = ((TextElement) first).getText();
                    if ("<".equals(firstText)) {
                        // $NON-NLS-1$
                        Object second = fragments.get(1);
                        Object third = fragments.get(2);
                        if (second instanceof SimpleName && third instanceof TextElement) {
                            String name = ((SimpleName) second).getIdentifier();
                            String thirdText = ((TextElement) third).getText();
                            if (">".equals(thirdText)) {
                                // $NON-NLS-1$
                                int paramIndex = typeParameterNames.indexOf(name);
                                if (paramIndex != -1) {
                                    typeParameterNames.set(paramIndex, null);
                                }
                                typeParameters.add(tag);
                            }
                        }
                    }
                }
            }
        } else if (TagElement.TAG_RETURN.equals(tagName)) {
            if (returnTag == null) {
                // the Javadoc tool only shows the first return tag
                returnTag = tag;
            }
        } else if (TagElement.TAG_EXCEPTION.equals(tagName) || TagElement.TAG_THROWS.equals(tagName)) {
            exceptions.add(tag);
            List<? extends ASTNode> fragments = tag.fragments();
            if (fragments.size() > 0) {
                Object first = fragments.get(0);
                if (first instanceof Name) {
                    String name = ASTNodes.getSimpleNameIdentifier((Name) first);
                    int exceptionIndex = exceptionNames.indexOf(name);
                    if (exceptionIndex != -1) {
                        exceptionNames.set(exceptionIndex, null);
                    }
                }
            }
        } else if (TagElement.TAG_SINCE.equals(tagName)) {
            since.add(tag);
        } else if (TagElement.TAG_VERSION.equals(tagName)) {
            versions.add(tag);
        } else if (TagElement.TAG_AUTHOR.equals(tagName)) {
            authors.add(tag);
        } else if (TagElement.TAG_SEE.equals(tagName)) {
            sees.add(tag);
        } else if (TagElement.TAG_DEPRECATED.equals(tagName)) {
            if (deprecatedTag == null) {
                // the Javadoc tool only shows the first deprecated tag
                deprecatedTag = tag;
            }
        } else if (TagElement.TAG_API_NOTE.equals(tagName)) {
            apinote.add(tag);
        } else {
            rest.add(tag);
        }
    }
    // TODO: @Documented annotations before header
    if (deprecatedTag != null) {
        handleDeprecatedTag(deprecatedTag);
    }
    if (start != null) {
        handleContentElements(start.fragments());
    } else if (fMethod != null) {
        CharSequence inherited = fJavadocLookup.getInheritedMainDescription(fMethod);
        // The Javadoc tool adds "Description copied from class: ..." (only for the main description).
        // We don't bother doing that.
        handleInherited(inherited);
    }
    CharSequence[] typeParameterDescriptions = new CharSequence[typeParameterNames.size()];
    boolean hasInheritedTypeParameters = inheritTypeParameterDescriptions(typeParameterNames, typeParameterDescriptions);
    boolean hasTypeParameters = typeParameters.size() > 0 || hasInheritedTypeParameters;
    CharSequence[] parameterDescriptions = new CharSequence[parameterNames.size()];
    boolean hasInheritedParameters = inheritParameterDescriptions(parameterNames, parameterDescriptions);
    boolean hasParameters = parameters.size() > 0 || hasInheritedParameters;
    CharSequence returnDescription = null;
    if (returnTag == null && needsReturnTag()) {
        returnDescription = fJavadocLookup.getInheritedReturnDescription(fMethod);
    }
    boolean hasReturnTag = returnTag != null || returnDescription != null;
    CharSequence[] exceptionDescriptions = new CharSequence[exceptionNames.size()];
    boolean hasInheritedExceptions = inheritExceptionDescriptions(exceptionNames, exceptionDescriptions);
    boolean hasExceptions = exceptions.size() > 0 || hasInheritedExceptions;
    if (hasParameters || hasTypeParameters || hasReturnTag || hasExceptions || versions.size() > 0 || authors.size() > 0 || since.size() > 0 || sees.size() > 0 || rest.size() > 0 || apinote.size() > 0 || (fBuf.length() > 0 && (parameterDescriptions.length > 0 || exceptionDescriptions.length > 0))) {
        handleSuperMethodReferences();
        fBuf.append(BLOCK_TAG_START);
        handleParameterTags(typeParameters, typeParameterNames, typeParameterDescriptions, true);
        handleParameterTags(parameters, parameterNames, parameterDescriptions, false);
        handleReturnTag(returnTag, returnDescription);
        handleExceptionTags(exceptions, exceptionNames, exceptionDescriptions);
        handleBlockTags(JavaDoc2HTMLTextReader_since_section, since);
        handleBlockTags(JavaDoc2HTMLTextReader_version_section, versions);
        handleBlockTags(JavaDoc2HTMLTextReader_author_section, authors);
        handleBlockTags(JavaDoc2HTMLTextReader_see_section, sees);
        handleBlockTags(JavaDoc2HTMLTextReader_api_note, apinote);
        handleBlockTags(rest);
        fBuf.append(BLOCK_TAG_END);
    } else if (fBuf.length() > 0) {
        handleSuperMethodReferences();
    }
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) TextElement(org.eclipse.jdt.core.dom.TextElement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

TagElement (org.eclipse.jdt.core.dom.TagElement)76 ASTNode (org.eclipse.jdt.core.dom.ASTNode)36 Javadoc (org.eclipse.jdt.core.dom.Javadoc)28 TextElement (org.eclipse.jdt.core.dom.TextElement)23 AST (org.eclipse.jdt.core.dom.AST)20 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)19 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)18 SimpleName (org.eclipse.jdt.core.dom.SimpleName)17 Type (org.eclipse.jdt.core.dom.Type)17 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)16 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)14 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)14 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)14 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)11 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)11 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)11 ArrayList (java.util.ArrayList)10 List (java.util.List)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)8