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

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

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ASTNode 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:com.windowtester.eclipse.ui.convert.WTConvertAPIContext.java

License:Open Source License

/**
 * Adjust the start position of nodes with start position greater than oldStart in the
 * specified root node./*from  ww w . j  a v a2s  .com*/
 * 
 * @param startNode the top most node containing nodes to be adjusted
 * @param oldStart the old start position
 * @param newStart the new start position
 */
private void adjustStartPositions(ASTNode startNode, final int oldStart, int newStart) {
    final int offset = newStart - oldStart;
    startNode.accept(new ASTVisitor() {
        public void preVisit(ASTNode node) {
            if (node.getStartPosition() >= oldStart)
                node.setSourceRange(node.getStartPosition() + offset, node.getLength());
            else if (node.getStartPosition() + node.getLength() >= oldStart)
                node.setSourceRange(node.getStartPosition(), node.getLength() + offset);
        }
    });
}

From source file:org.eclipse.ajdt.internal.ui.refactoring.ITDRenameRefactoringProvider.java

License:Open Source License

/**
 * Creates an AST for the given compilation unit with translated code.
 * The source locations of the created AST have been converted back so that they reflect the source locations
 * of the original, non-translated source.
 *//*from  w w  w . j  ava2  s  .c o m*/
public CompilationUnit createSourceConvertedAST(String contents, ICompilationUnit unit, boolean resolveBindings,
        boolean statementsRecovery, boolean bindingsRecovery, IProgressMonitor monitor) {
    AspectsConvertingParser converter = new AspectsConvertingParser(contents.toCharArray());
    converter.setUnit(unit);
    final ArrayList<Replacement> replacements = converter.convert(ConversionOptions.CODE_COMPLETION);

    ASTParser fParser = ASTParser.newParser(AST.JLS4);
    fParser.setResolveBindings(resolveBindings);
    fParser.setStatementsRecovery(statementsRecovery);
    fParser.setBindingsRecovery(bindingsRecovery);
    fParser.setProject(unit.getJavaProject());
    fParser.setSource(converter.content);
    fParser.setUnitName(unit.getElementName());
    if (unit.getOwner() != null) {
        fParser.setWorkingCopyOwner(unit.getOwner());
    }
    CompilationUnit result = (CompilationUnit) fParser.createAST(monitor);
    result.accept(new ASTVisitor() {
        @Override
        public void preVisit(ASTNode node) {
            int newStart = AspectsConvertingParser.translatePositionToBeforeChanges(node.getStartPosition(),
                    replacements);
            int newEnd = AspectsConvertingParser
                    .translatePositionToBeforeChanges(node.getStartPosition() + node.getLength(), replacements);
            node.setSourceRange(newStart, newEnd - newStart);
        }
    });
    return result;
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

/**
 * Remove potential trailing comment by settings the source end on the closing parenthesis
 *///ww  w.  j  a v  a  2s .c  o m
protected void removeLeadingAndTrailingCommentsFromLiteral(ASTNode node) {
    int start = node.getStartPosition();
    this.scanner.resetTo(start, start + node.getLength());
    int token;
    int startPosition = -1;
    try {
        while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
            switch (token) {
            case TerminalTokens.TokenNameIntegerLiteral:
            case TerminalTokens.TokenNameFloatingPointLiteral:
            case TerminalTokens.TokenNameLongLiteral:
            case TerminalTokens.TokenNameDoubleLiteral:
            case TerminalTokens.TokenNameCharacterLiteral:
                if (startPosition == -1) {
                    startPosition = this.scanner.startPosition;
                }
                int end = this.scanner.currentPosition;
                node.setSourceRange(startPosition, end - startPosition);
                return;
            case TerminalTokens.TokenNameMINUS:
                startPosition = this.scanner.startPosition;
                break;
            }
        }
    } catch (InvalidInputException e) {
        // ignore
    }
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

/**
 * Remove potential trailing comment by settings the source end on the closing parenthesis
 *//*from ww w  . j  a v  a 2 s . c  om*/
protected void removeTrailingCommentFromExpressionEndingWithAParen(ASTNode node) {
    int start = node.getStartPosition();
    this.scanner.resetTo(start, start + node.getLength());
    int token;
    int parenCounter = 0;
    try {
        while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
            switch (token) {
            case TerminalTokens.TokenNameLPAREN:
                parenCounter++;
                break;
            case TerminalTokens.TokenNameRPAREN:
                parenCounter--;
                if (parenCounter == 0) {
                    int end = this.scanner.currentPosition - 1;
                    node.setSourceRange(start, end - start + 1);
                }
            }
        }
    } catch (InvalidInputException e) {
        // ignore
    }
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

/**
 * This method is used to set the right end position for expression
 * statement. The actual AST nodes don't include the trailing semicolon.
 * This method fixes the length of the corresponding node.
 *//*from w w  w  .  j a v  a 2  s .  co m*/
protected void retrieveColonPosition(ASTNode node) {
    int start = node.getStartPosition();
    int length = node.getLength();
    int end = start + length;
    this.scanner.resetTo(end, this.compilationUnitSourceLength);
    try {
        int token;
        while ((token = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
            switch (token) {
            case TerminalTokens.TokenNameCOLON:
                node.setSourceRange(start, this.scanner.currentPosition - start);
                return;
            }
        }
    } catch (InvalidInputException e) {
        // ignore
    }
}

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

License:Open Source License

/**
 * Replaces the substring starting at the given start position with the given length by the given
 * replacement text, modifying the source ranges for the nodes in the AST in the process.
 * //from w  w w .  j a va  2s  .  co  m
 * @param oldStart
 *          the index of the first character being replaced
 * @param oldLength
 *          the number of characters being replaced
 * @param replacement
 *          the text with which they are being replaced
 */
public void replaceSubstring(final int oldStart, int oldLength, String replacement) throws Exception {
    replaceSubstring_markRemovedComments(oldStart, oldLength);
    List<Comment> commentList = getCommentList();
    // replace text
    //System.out.println("|" + m_document.get(oldStart, oldLength) + "| -> |" + replacement + "|");
    m_document.replace(oldStart, oldLength, replacement);
    // prepare positions
    final int newLength = replacement.length();
    final int difference = newLength - oldLength;
    final int oldEnd = oldStart + oldLength;
    // prepare visitor
    ASTVisitor visitor = new ASTVisitor(true) {
        @Override
        public void postVisit(ASTNode node) {
            int position = node.getStartPosition();
            int length = node.getLength();
            int end = position + length;
            // sanity checks
            {
                // we can not start replacement inside of node, but end outside
                if (position < oldStart && oldStart < end && oldEnd > end) {
                    throw new DesignerException(ICoreExceptionConstants.AST_EDITOR_REPLACE);
                }
                // we can not start replacement outside of node, but end inside
                if (position < oldEnd && oldEnd < end && oldStart < position) {
                    throw new DesignerException(ICoreExceptionConstants.AST_EDITOR_REPLACE);
                }
            }
            //
            if (end <= oldStart) {
                // before changed region: no change
            } else if (position >= oldEnd && !(node instanceof CompilationUnit)) {
                // after changed region: move
                node.setSourceRange(position + difference, length);
            } else if (position <= oldStart && position + length >= oldEnd) {
                // embraces changed region: change length
                node.setSourceRange(position, length + difference);
            }
            // special handling for AnonymousTypeDeclaration
            {
                TypeDeclaration anonymous = AnonymousTypeDeclaration.get(node);
                if (anonymous != null) {
                    anonymous.setSourceRange(node.getStartPosition(), node.getLength());
                }
            }
        }
    };
    // modify position/length for nodes in AST
    m_astUnit.accept(visitor);
    // update comments
    for (Comment comment : commentList) {
        if (!(comment instanceof Javadoc)) {
            comment.accept(visitor);
        }
    }
}

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

License:Open Source License

/**
 * Moves piece of source with given start/length to target position.
 * // w  ww.ja  v a 2  s.  c  om
 * @return the new value of target
 */
private int moveSource(int target, int start, int length) throws Exception {
    String source = getSource(start, length);
    // prepare locations
    final int b_pos = start;
    final int b_len = length;
    final int b_end = b_pos + b_len;
    final int t_pos = target;
    //
    if (b_pos > t_pos) {
        m_document.replace(b_pos, b_len, "");
        m_document.replace(t_pos, 0, source);
        // modify source ranges for AST nodes
        m_astUnit.accept(new ASTVisitor(true) {
            @Override
            public void postVisit(ASTNode node) {
                int n_pos = node.getStartPosition();
                int n_len = node.getLength();
                int n_end = n_pos + n_len;
                while (true) {
                    // node starts after source end
                    if (n_pos >= b_end) {
                        return;
                    }
                    // node ends before target
                    if (n_end <= t_pos) {
                        return;
                    }
                    // node contains source and target
                    if (n_pos < t_pos && n_end > b_end) {
                        return;
                    }
                    // node is inside of block
                    if (n_pos >= b_pos && n_end <= b_end) {
                        node.setSourceRange(t_pos + n_pos - b_pos, n_len);
                        return;
                    }
                    // node is between target and source
                    if (n_pos >= t_pos && n_end <= b_pos) {
                        node.setSourceRange(n_pos + b_len, n_len);
                        return;
                    }
                    // node contains target
                    if (n_pos < t_pos && n_end > t_pos) {
                        node.setSourceRange(n_pos, n_len + b_len);
                        return;
                    }
                    // node contains source
                    /*if (n_pos < b_pos && n_end > b_end)*/ {
                        node.setSourceRange(n_pos + b_len, n_len - b_len);
                        return;
                    }
                }
            }
        });
    } else {
        m_document.replace(t_pos, 0, source);
        m_document.replace(b_pos, b_len, "");
        // modify source ranges for AST nodes
        m_astUnit.accept(new ASTVisitor(true) {
            @Override
            public void postVisit(ASTNode node) {
                int n_pos = node.getStartPosition();
                int n_len = node.getLength();
                int n_end = n_pos + n_len;
                // node ends before source
                if (n_end <= b_pos) {
                    return;
                }
                // node starts after target
                if (n_pos >= t_pos) {
                    return;
                }
                // node contains source and target
                if (n_pos < b_pos && n_end > t_pos) {
                    return;
                }
                // node was inside of block
                if (n_pos >= b_pos && n_end <= b_end) {
                    node.setSourceRange(t_pos - b_len + n_pos - b_pos, n_len);
                    return;
                }
                // node is between source and target
                if (n_pos >= b_end && n_end <= t_pos) {
                    node.setSourceRange(n_pos - b_len, n_len);
                    return;
                }
                // node contains target
                if (n_pos < t_pos && n_end > t_pos) {
                    node.setSourceRange(n_pos - b_len, n_len + b_len);
                    return;
                }
                // node contains source
                /*if (n_pos < b_pos && n_end > b_end)*/ {
                    node.setSourceRange(n_pos, n_len - b_len);
                    return;
                }
            }
        });
    }
    // update target
    if (start < target) {
        target -= length;
    }
    return target;
}

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

License:Open Source License

/**
 * Sets the index of beginning source for given node.
 *///ww w . java2  s  .  c om
public static void setSourceBegin(ASTNode node, int begin) {
    node.setSourceRange(begin, node.getLength());
}

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

License:Open Source License

/**
 * Sets the index of beginning source for given node, with keeping end position (i.e. update also
 * length)./*from www.j  a v  a2s  .  c  om*/
 */
public static void setSourceBegin_keepEnd(ASTNode node, int begin) {
    int delta = node.getStartPosition() - begin;
    int newLength = node.getLength() + delta;
    node.setSourceRange(begin, newLength);
}

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

License:Open Source License

/**
 * Sets source length to match end position of "source" node.
 *//*from   ww w  .  j a  v a2s  .c  o m*/
public static void setSourceEnd(ASTNode targetNode, ASTNode sourceNode) {
    int begin = targetNode.getStartPosition();
    int end = getSourceEnd(sourceNode);
    targetNode.setSourceRange(begin, end - begin);
}