Example usage for org.eclipse.jdt.core.dom TextElement setSourceRange

List of usage examples for org.eclipse.jdt.core.dom TextElement setSourceRange

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom TextElement setSourceRange.

Prototype

public final void setSourceRange(int startPosition, int length) 

Source Link

Document

Sets the source range of the original source file where the source fragment corresponding to this node was found.

Usage

From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java

License:Open Source License

/**
 * Sets new text for JavaDoc {@link TagElement}, i.e. {@link TextElement}.
 * /*from   w  w  w .  j  a va2s  .co m*/
 * @param declaration
 *          the {@link BodyDeclaration} to update {@link Javadoc}.
 * @param tagName
 *          the name of tag, such as <code>"myTag"</code>, with leading <code>"@"</code>.
 * @param tagText
 *          the text to set for tag, with leading space, or <code>null</code> if tag should be
 *          removed.
 * 
 * @return the {@link TagElement} that has single {@link TextElement} fragment with
 *         <code>tagText</code> as text; or <code>null</code> if tag was removed.
 */
public TagElement setJavadocTagText(BodyDeclaration declaration, String tagName, String tagText)
        throws Exception {
    Assert.isNotNull(tagName);
    Assert.isLegal(tagName.length() != 0, "Empty name of tag.");
    Assert.isLegal(tagName.startsWith("@"), "Tag name should start with '@'.");
    Javadoc javadoc = declaration.getJavadoc();
    // update/add tag
    if (tagText != null) {
        // update existing JavaDoc
        if (javadoc != null) {
            // try to find existing tag
            for (TagElement tagElement : DomGenerics.tags(javadoc)) {
                if (tagName.equals(tagElement.getTagName())) {
                    setJavadocTagText_replaceFragments(tagElement, tagText);
                    return tagElement;
                }
            }
            // add new tag
            {
                List<TagElement> tags = DomGenerics.tags(javadoc);
                // prepare position for new TagElement
                int position;
                {
                    int javadocPosition = javadoc.getStartPosition();
                    String prefix = getWhitespaceToLeft(javadocPosition, false);
                    String endOfLine = getGeneration().getEndOfLine();
                    if (tags.isEmpty()) {
                        position = javadocPosition + "/**".length();
                    } else {
                        position = AstNodeUtils.getSourceEnd(tags.get(tags.size() - 1));
                    }
                    String newCommentLine = endOfLine + prefix + " * ";
                    replaceSubstring(position, 0, newCommentLine);
                    position += newCommentLine.length();
                }
                // replace source
                String tagSource = tagName + tagText;
                replaceSubstring(position, 0, tagSource);
                // add TagElement
                TagElement tagElement = javadoc.getAST().newTagElement();
                tagElement.setSourceRange(position, tagSource.length());
                tagElement.setTagName(tagName);
                tags.add(tagElement);
                // add TextElement
                TextElement textElement = javadoc.getAST().newTextElement();
                textElement.setSourceRange(position + tagName.length(), tagText.length());
                textElement.setText(tagText);
                DomGenerics.fragments(tagElement).add(textElement);
                // new TagElement
                return tagElement;
            }
        } else {
            javadoc = setJavadoc(declaration, new String[] { tagName + tagText });
            return DomGenerics.tags(javadoc).get(0);
        }
    }
    // remove tag
    if (javadoc != null) {
        for (Iterator<TagElement> I = DomGenerics.tags(javadoc).iterator(); I.hasNext();) {
            TagElement tagElement = I.next();
            if (tagName.equals(tagElement.getTagName())) {
                int begin = AstNodeUtils.getSourceBegin(tagElement);
                int end = AstNodeUtils.getSourceEnd(tagElement);
                end = indexOfAnyBut(" \t\r\n*", end + 1);
                // check for removing last line
                if (getChar(end) == '/') {
                    begin = indexOfAnyButBackward("*", begin);
                }
                // replace source
                replaceSubstring(begin, end - begin, "");
                // remove tag
                I.remove();
                // remove JavaDoc is empty
                if (DomGenerics.tags(javadoc).isEmpty()) {
                    setJavadoc(declaration, null);
                }
                // done
                break;
            }
        }
    }
    return null;
}

From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java

License:Open Source License

private void setJavadocTagText_replaceFragments(TagElement tagElement, String tagText) throws Exception {
    List<ASTNode> fragments = DomGenerics.fragments(tagElement);
    // replace source
    int fragmentsPosition;
    if (!fragments.isEmpty()) {
        ASTNode firstFragment = fragments.get(0);
        fragmentsPosition = AstNodeUtils.getSourceBegin(firstFragment);
        int fragmentsLength = AstNodeUtils.getSourceEnd(tagElement) - fragmentsPosition;
        replaceSubstring(fragmentsPosition, fragmentsLength, tagText);
    } else {//  w  w  w.  j a  v  a2  s .c  om
        fragmentsPosition = AstNodeUtils.getSourceEnd(tagElement);
        replaceSubstring(fragmentsPosition, 0, tagText);
        AstNodeUtils.setSourceLength(tagElement, tagElement.getLength() + tagText.length());
    }
    // replace fragments
    fragments.clear();
    TextElement textElement = tagElement.getAST().newTextElement();
    textElement.setSourceRange(fragmentsPosition, tagText.length());
    textElement.setText(tagText);
    fragments.add(textElement);
}