Search in sources :

Example 16 with TagElement

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

the class JavadocTagsSubProcessor method findParamTag.

public static TagElement findParamTag(Javadoc javadoc, String arg) {
    List<TagElement> tags = javadoc.tags();
    int nTags = tags.size();
    for (int i = 0; i < nTags; i++) {
        TagElement curr = tags.get(i);
        String currName = curr.getTagName();
        if (TagElement.TAG_PARAM.equals(currName)) {
            String argument = getArgument(curr);
            if (arg.equals(argument)) {
                return curr;
            }
        }
    }
    return null;
}
Also used : TagElement(org.eclipse.jdt.core.dom.TagElement)

Example 17 with TagElement

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

the class JavadocTagsSubProcessor method insertTag.

public static void insertTag(ListRewrite rewriter, TagElement newElement, Set<String> sameKindLeadingNames, TextEditGroup groupDescription) {
    List<? extends ASTNode> tags = rewriter.getRewrittenList();
    String insertedTagName = newElement.getTagName();
    ASTNode after = null;
    int tagRanking = getTagRanking(insertedTagName);
    for (int i = tags.size() - 1; i >= 0; i--) {
        TagElement curr = (TagElement) tags.get(i);
        String tagName = curr.getTagName();
        if (tagName == null || tagRanking > getTagRanking(tagName)) {
            after = curr;
            break;
        }
        if (sameKindLeadingNames != null && isSameTag(insertedTagName, tagName)) {
            String arg = getArgument(curr);
            if (arg != null && sameKindLeadingNames.contains(arg)) {
                after = curr;
                break;
            }
        }
    }
    if (after != null) {
        rewriter.insertAfter(newElement, after, groupDescription);
    } else {
        rewriter.insertFirst(newElement, groupDescription);
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement)

Example 18 with TagElement

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

the class JavadocUtil method addParamJavadoc.

/**
	 * Adds a "param" javadoc tag for a new last parameter if necessary.
	 * @param parameterName
	 * @param methodDeclaration
	 * @param astRewrite
	 * @param javaProject
	 * @param groupDescription
	 */
public static void addParamJavadoc(String parameterName, MethodDeclaration methodDeclaration, ASTRewrite astRewrite, IJavaProject javaProject, TextEditGroup groupDescription) {
    if (!shouldAddParamJavadoc(methodDeclaration))
        return;
    ListRewrite tagsRewrite = astRewrite.getListRewrite(methodDeclaration.getJavadoc(), Javadoc.TAGS_PROPERTY);
    HashSet<String> leadingNames = new HashSet<String>();
    for (Iterator<SingleVariableDeclaration> iter = methodDeclaration.parameters().iterator(); iter.hasNext(); ) {
        SingleVariableDeclaration curr = iter.next();
        leadingNames.add(curr.getName().getIdentifier());
    }
    TagElement parameterTag = createParamTag(parameterName, astRewrite.getAST(), javaProject);
    JavadocTagsSubProcessor.insertTag(tagsRewrite, parameterTag, leadingNames, groupDescription);
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) TagElement(org.eclipse.jdt.core.dom.TagElement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) HashSet(java.util.HashSet)

Example 19 with TagElement

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

the class TypeChangeCorrectionProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
    ASTNode declNode = null;
    CompilationUnit newRoot = fAstRoot;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
    } else {
        newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
    }
    if (declNode != null) {
        AST ast = declNode.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ImportRewrite imports = createImportRewrite(newRoot);
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(newRoot, declNode.getStartPosition(), imports);
        Type type = imports.addImport(fNewType, ast, context, fTypeLocation);
        if (declNode instanceof MethodDeclaration) {
            MethodDeclaration methodDecl = (MethodDeclaration) declNode;
            Type origReturnType = methodDecl.getReturnType2();
            rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
            DimensionRewrite.removeAllChildren(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
            TypeAnnotationRewrite.removePureTypeAnnotations(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY, rewrite, null);
            // add javadoc tag
            Javadoc javadoc = methodDecl.getJavadoc();
            if (javadoc != null && origReturnType != null && origReturnType.isPrimitiveType() && ((PrimitiveType) origReturnType).getPrimitiveTypeCode() == PrimitiveType.VOID) {
                TagElement returnTag = JavadocTagsSubProcessor.findTag(javadoc, TagElement.TAG_RETURN, null);
                if (returnTag == null) {
                    returnTag = ast.newTagElement();
                    returnTag.setTagName(TagElement.TAG_RETURN);
                    TextElement commentStart = ast.newTextElement();
                    returnTag.fragments().add(commentStart);
                    ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
                    JavadocTagsSubProcessor.insertTag(tagsRewriter, returnTag, null);
                }
            }
        } else if (declNode instanceof AnnotationTypeMemberDeclaration) {
            AnnotationTypeMemberDeclaration methodDecl = (AnnotationTypeMemberDeclaration) declNode;
            rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.TYPE_PROPERTY, type, null);
        } else if (declNode instanceof VariableDeclarationFragment) {
            ASTNode parent = declNode.getParent();
            if (parent instanceof FieldDeclaration) {
                FieldDeclaration fieldDecl = (FieldDeclaration) parent;
                if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) {
                    // split
                    VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
                    FieldDeclaration newField = ast.newFieldDeclaration(placeholder);
                    newField.setType(type);
                    AbstractTypeDeclaration typeDecl = (AbstractTypeDeclaration) fieldDecl.getParent();
                    ListRewrite listRewrite = rewrite.getListRewrite(typeDecl, typeDecl.getBodyDeclarationsProperty());
                    if (fieldDecl.fragments().indexOf(declNode) == 0) {
                        // if it as the first in the list-> insert before
                        listRewrite.insertBefore(newField, parent, null);
                    } else {
                        listRewrite.insertAfter(newField, parent, null);
                    }
                } else {
                    rewrite.set(fieldDecl, FieldDeclaration.TYPE_PROPERTY, type, null);
                    DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
                    TypeAnnotationRewrite.removePureTypeAnnotations(fieldDecl, FieldDeclaration.MODIFIERS2_PROPERTY, rewrite, null);
                }
            } else if (parent instanceof VariableDeclarationStatement) {
                VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent;
                if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) {
                    // split
                    VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
                    VariableDeclarationStatement newStat = ast.newVariableDeclarationStatement(placeholder);
                    newStat.setType(type);
                    ListRewrite listRewrite = rewrite.getListRewrite(varDecl.getParent(), Block.STATEMENTS_PROPERTY);
                    if (varDecl.fragments().indexOf(declNode) == 0) {
                        // if it as the first in the list-> insert before
                        listRewrite.insertBefore(newStat, parent, null);
                    } else {
                        listRewrite.insertAfter(newStat, parent, null);
                    }
                } else {
                    rewrite.set(varDecl, VariableDeclarationStatement.TYPE_PROPERTY, type, null);
                    DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
                }
            } else if (parent instanceof VariableDeclarationExpression) {
                VariableDeclarationExpression varDecl = (VariableDeclarationExpression) parent;
                rewrite.set(varDecl, VariableDeclarationExpression.TYPE_PROPERTY, type, null);
                DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
            }
        } else if (declNode instanceof SingleVariableDeclaration) {
            SingleVariableDeclaration variableDeclaration = (SingleVariableDeclaration) declNode;
            rewrite.set(variableDeclaration, SingleVariableDeclaration.TYPE_PROPERTY, type, null);
            DimensionRewrite.removeAllChildren(declNode, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
            TypeAnnotationRewrite.removePureTypeAnnotations(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY, rewrite, null);
        }
        return rewrite;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) 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) AnnotationTypeMemberDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 20 with TagElement

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

the class JavadocTagsSubProcessor method findThrowsTag.

public static TagElement findThrowsTag(Javadoc javadoc, String arg) {
    List<TagElement> tags = javadoc.tags();
    int nTags = tags.size();
    for (int i = 0; i < nTags; i++) {
        TagElement curr = tags.get(i);
        String currName = curr.getTagName();
        if (TagElement.TAG_THROWS.equals(currName) || TagElement.TAG_EXCEPTION.equals(currName)) {
            String argument = getArgument(curr);
            if (arg.equals(argument)) {
                return curr;
            }
        }
    }
    return null;
}
Also used : TagElement(org.eclipse.jdt.core.dom.TagElement)

Aggregations

TagElement (org.eclipse.jdt.core.dom.TagElement)59 ASTNode (org.eclipse.jdt.core.dom.ASTNode)29 Javadoc (org.eclipse.jdt.core.dom.Javadoc)25 AST (org.eclipse.jdt.core.dom.AST)17 TextElement (org.eclipse.jdt.core.dom.TextElement)16 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)15 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)15 Type (org.eclipse.jdt.core.dom.Type)14 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)14 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)13 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)12 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)12 SimpleName (org.eclipse.jdt.core.dom.SimpleName)11 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)11 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)8 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)8 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)7 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)7 ContextSensitiveImportRewriteContext (org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)7 ArrayList (java.util.ArrayList)6