Search in sources :

Example 56 with TagElement

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

the class ReturnTypeSubProcessor method addMissingReturnTypeProposals.

public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
    if (decl instanceof MethodDeclaration) {
        MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
        ReturnStatementCollector eval = new ReturnStatementCollector();
        decl.accept(eval);
        AST ast = astRoot.getAST();
        ITypeBinding typeBinding = eval.getTypeBinding(decl.getAST());
        typeBinding = Bindings.normalizeTypeBinding(typeBinding);
        if (typeBinding == null) {
            // $NON-NLS-1$
            typeBinding = ast.resolveWellKnownType("void");
        }
        if (typeBinding.isWildcardType()) {
            typeBinding = ASTResolving.normalizeWildcardType(typeBinding, true, ast);
        }
        ASTRewrite rewrite = ASTRewrite.create(ast);
        String label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
        LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE);
        ImportRewrite imports = proposal.createImportRewrite(astRoot);
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
        Type type = imports.addImport(typeBinding, ast, importRewriteContext, TypeLocation.RETURN_TYPE);
        rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
        rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);
        Javadoc javadoc = methodDeclaration.getJavadoc();
        if (javadoc != null && typeBinding != null) {
            TagElement newTag = ast.newTagElement();
            newTag.setTagName(TagElement.TAG_RETURN);
            TextElement commentStart = ast.newTextElement();
            newTag.fragments().add(commentStart);
            JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
            // $NON-NLS-1$
            proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
        }
        // $NON-NLS-1$
        String key = "return_type";
        proposal.addLinkedPosition(rewrite.track(type), true, key);
        if (typeBinding != null) {
            ITypeBinding[] bindings = ASTResolving.getRelaxingTypes(ast, typeBinding);
            for (int i = 0; i < bindings.length; i++) {
                proposal.addLinkedPositionProposal(key, bindings[i]);
            }
        }
        proposals.add(proposal);
        // change to constructor
        ASTNode parentType = ASTResolving.findParentType(decl);
        if (parentType instanceof AbstractTypeDeclaration) {
            boolean isInterface = parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
            if (!isInterface) {
                String constructorName = ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
                ASTNode nameNode = methodDeclaration.getName();
                label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
                proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) TextElement(org.eclipse.jdt.core.dom.TextElement) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) LinkedCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.LinkedCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) ReplaceCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ReplaceCorrectionProposal)

Example 57 with TagElement

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

the class CommentsCleanUp method addPeriodAtEndOfFirstLine.

private String addPeriodAtEndOfFirstLine(final Javadoc node, final String comment) {
    String beforeFirstTag = comment;
    // $NON-NLS-1$
    String afterFirstTag = "";
    Matcher m = FIRST_JAVADOC_TAG.matcher(comment);
    if (m.find()) {
        if (m.start() == 0) {
            return null;
        }
        beforeFirstTag = comment.substring(0, m.start());
        afterFirstTag = comment.substring(m.start());
    }
    Matcher matcher = JAVADOC_WITHOUT_PUNCTUATION.matcher(beforeFirstTag);
    if (matcher.matches()) {
        List<TagElement> tagElements = node.tags();
        if (tagElements.size() >= 2) {
            TagElement firstLine = tagElements.get(0);
            int relativeStart = firstLine.getStartPosition() - node.getStartPosition();
            int endOfFirstLine = relativeStart + firstLine.getLength();
            // $NON-NLS-1$
            return comment.substring(0, endOfFirstLine) + "." + comment.substring(endOfFirstLine);
        // TODO JNR do the replace here, not outside this method
        }
        // $NON-NLS-1$
        return matcher.group(1) + "." + matcher.group(2) + afterFirstTag;
    }
    return null;
}
Also used : Matcher(java.util.regex.Matcher) TagElement(org.eclipse.jdt.core.dom.TagElement)

Example 58 with TagElement

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

the class XbaseHoverDocumentationProvider method handleContentElements.

protected 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
            } 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 = rawJavaDoc.substring(previousEnd, childStart);
                String text = removeDocLineIntros(textWithStars);
                buffer.append(text);
            }
        }
        previousNode = child;
        if (child instanceof TextElement) {
            String text = ((TextElement) child).getText();
            if (skipLeadingWhitespace) {
                // $NON-NLS-1$ //$NON-NLS-2$
                text = text.replaceFirst("^\\s+", "");
            }
            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 = rawJavaDoc.substring(start, start + child.getLength());
            buffer.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)

Example 59 with TagElement

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

the class XbaseHoverDocumentationProvider method handleBlockTags.

protected void handleBlockTags(List<TagElement> tags) {
    for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext(); ) {
        TagElement tag = iter.next();
        handleBlockTagTitle(tag.getTagName());
        buffer.append(BlOCK_TAG_ENTRY_START);
        @SuppressWarnings("unchecked") List<ASTNode> fragments = tag.fragments();
        handleContentElements(fragments);
        buffer.append(BlOCK_TAG_ENTRY_END);
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement)

Example 60 with TagElement

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

the class XbaseHoverDocumentationProvider method computeDocumentation.

public String computeDocumentation(EObject object) {
    buffer = new StringBuffer();
    context = object;
    fLiteralContent = 0;
    List<String> parameterNames = initParameterNames();
    Map<String, URI> exceptionNamesToURI = initExceptionNamesToURI();
    addAnnotations(object);
    getDocumentationWithPrefix(object);
    Javadoc javadoc = getJavaDoc();
    if (javadoc == null)
        return buffer.toString();
    TagElement deprecatedTag = null;
    TagElement start = null;
    List<TagElement> parameters = new ArrayList<TagElement>();
    TagElement returnTag = null;
    List<TagElement> exceptions = new ArrayList<TagElement>();
    List<TagElement> versions = new ArrayList<TagElement>();
    List<TagElement> authors = new ArrayList<TagElement>();
    List<TagElement> sees = new ArrayList<TagElement>();
    List<TagElement> since = new ArrayList<TagElement>();
    List<TagElement> rest = new ArrayList<TagElement>();
    @SuppressWarnings("unchecked") List<TagElement> tags = javadoc.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)) {
            parameters.add(tag);
            @SuppressWarnings("unchecked") List<? extends ASTNode> fragments = tag.fragments();
            if (fragments.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);
                    }
                }
            }
        } 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);
            @SuppressWarnings("unchecked") List<? extends ASTNode> fragments = tag.fragments();
            if (fragments.size() > 0) {
                Object first = fragments.get(0);
                if (first instanceof Name) {
                    @SuppressWarnings("restriction") String name = org.eclipse.jdt.internal.corext.dom.ASTNodes.getSimpleNameIdentifier((Name) first);
                    if (exceptionNamesToURI.containsKey(name)) {
                        exceptionNamesToURI.put(name, 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 {
            rest.add(tag);
        }
    }
    boolean hasParameters = parameters.size() > 0;
    boolean hasReturnTag = returnTag != null;
    boolean hasExceptions = exceptions.size() > 0;
    if (deprecatedTag != null)
        handleDeprecatedTag(deprecatedTag);
    if (start != null) {
        @SuppressWarnings("unchecked") List<ASTNode> fragments = start.fragments();
        handleContentElements(fragments);
    }
    if (hasParameters || hasReturnTag || hasExceptions || versions.size() > 0 || authors.size() > 0 || since.size() > 0 || sees.size() > 0 || rest.size() > 0 || (buffer.length() > 0) && (parameterNames.size() > 0 || exceptionNamesToURI.size() > 0)) {
        handleSuperMethodReferences(object);
        buffer.append(BLOCK_TAG_START);
        handleParameters(object, parameters, parameterNames);
        handleReturnTag(returnTag);
        handleExceptionTags(exceptions, exceptionNamesToURI);
        handleBlockTags("Since:", since);
        handleBlockTags("Version:", versions);
        handleBlockTags("Author:", authors);
        handleBlockTags("See Also:", sees);
        handleBlockTags(rest);
        buffer.append(BLOCK_TAG_END);
    } else if (buffer.length() > 0) {
        handleSuperMethodReferences(object);
    }
    String result = buffer.toString();
    buffer = null;
    rawJavaDoc = null;
    context = null;
    return result;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) URI(org.eclipse.emf.common.util.URI) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) EObject(org.eclipse.emf.ecore.EObject)

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