## org.eclipse.emf.codegen.merge.java.facade.ast
Class CommentAwareSourceRangeComputer

    java.lang.Object
      org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer
          org.eclipse.emf.codegen.merge.java.facade.ast.CommentAwareSourceRangeComputer

    public class CommentAwareSourceRangeComputer

extends org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer

Class that computes the ranges for the nodes for use by `ASTRewrite`. The ranges for the nodes will include the comments between the nodes in order to preserve as many comments as possible when nodes are moved or removed.

The default range for any node will include the comments immediately preceding the node and the line comment at the last line of the node.

If the node and the previous node have not been marked for removal, the range will also include all leading comments up to the previous node.

If the node is marked for removal, the range for node itself will be the default range. The leading comments between the node for removal and previous node will be in the range for the previous node.

When the node is going to be removed, `markNodeForRemoval(ASTNode)` must be called. To undo the removal (in a situation where the removed node is inserted into the tree, in other words, node is moved), `unmarkNodeForRemoval(ASTNode)` must be called. All nodes must be marked as removed before `ASTRewrite.rewriteAST()` or `ASTRewrite.rewriteAST(org.eclipse.jface.text.IDocument, Map)` is called.

Note that `ASTRewrite` removes all content after the removed node up to the start of the next node. Hence, if two consecutive nodes are removed, all comments between them are lost.

See Also:
  `ASTRewrite.setTargetSourceRangeComputer(TargetSourceRangeComputer)`, `TargetSourceRangeComputer`

Nested Class Summary

Nested classes/interfaces inherited from class org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer
`org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange`

Field Summary
`protected org.eclipse.jdt.core.dom.Comment[]` `commentArray` Array of comments from compilation unit
`protected int[]` `commentEndPositions` End positions of all comments
`protected int[]` `commentStartPositions` Start positions of all comments
`protected org.eclipse.jdt.core.dom.CompilationUnit` `compilationUnit` Compilation unit used to get extended ranges of the nodes
`protected java.util.Map<org.eclipse.jdt.core.dom.ASTNode,org.eclipse.jdt.core.dom.Comment>` `includeTrailingCommentMapper` Map of nodes to the trailing `Comment` nodes that must be included in the range for the node
`protected static java.util.Set<java.lang.Integer>` `NODE_TYPES_WITH_SPECIAL_RANGE` Node types that this range computer changes range for.
`protected java.util.Set<org.eclipse.jdt.core.dom.ASTNode>` `nodesWithDefaultRange` Set of nodes that must have default range returned for them
`protected java.lang.String` `source` Original source used to create compilation unit

Constructor Summary
`CommentAwareSourceRangeComputer(org.eclipse.jdt.core.dom.CompilationUnit compilationUnit, java.lang.String source)`

Method Summary
`protected int` `addWhitespaceAfterPosition(int position)` If possible, extends given position to include any whitespace following the position.
`org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange` `computeDefaultSourceRange(org.eclipse.jdt.core.dom.ASTNode node)` Calculates the default range for the node.
`protected int` `computeEndOfTrailingComments(org.eclipse.jdt.core.dom.ASTNode node)` Finds the end position of trailing comments of the given node.
`org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange` `computeSourceRange(org.eclipse.jdt.core.dom.ASTNode node)` Calculates the range of a node as follows: For any node the range is at least the range returned by `computeDefaultSourceRange(ASTNode)`.
`protected int` `computeStartOfPrecedingComments(org.eclipse.jdt.core.dom.ASTNode node)` Finds the start position of preceding comments of the given node.
`protected int` `determineEndPositionOfLineComment(int position)` Calculate the end of the line comment that is at the same line as `position`, and with only whitespace between `position` and the start of the line comment.
`protected int` `determineEndPositionOfLineComment(int nodeEnd, int nodeExtendedEnd)` Calculate the end of the line comment that is at the same line as `nodeEnd`, and ends before `nodeExtendedEnd`.
`protected org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange` `extendRangeBackward(org.eclipse.jdt.core.dom.ASTNode nodeToAdd, org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange range)` Extends the given range to include the range of `nodeToAdd`.
`protected org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange` `extendRangeForward(org.eclipse.jdt.core.dom.ASTNode nodeToAdd, org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange range)` Extends the given range to include the range of `nodeToAdd`.
`protected int` `findFirstCommentInRangeIndex(int rangeStartPos, int rangeEndPos)` Finds index of the first comment that is completely in the given range.
`protected int` `findLastCommentInRangeIndex(int rangeStartPos, int rangeEndPos)` Finds index of the last comment in the given range.
`protected org.eclipse.jdt.core.dom.Comment` `findLeadingComment(org.eclipse.jdt.core.dom.ASTNode node, org.eclipse.jdt.core.dom.ASTNode previousNode, boolean includeHangingCommentsOnly)` Finds the furthest leading comment of `node` after the previous node.
`protected org.eclipse.jdt.core.dom.Comment` `findTrailingComments(org.eclipse.jdt.core.dom.ASTNode node, org.eclipse.jdt.core.dom.ASTNode nextNode, boolean includeHangingCommentsOnly)` Finds the furthest trailing comment of `node` before the next node.
`protected org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange` `getEnumConstantSourceRange(org.eclipse.jdt.core.dom.ASTNode node)` Specific method for enum constants.
`protected org.eclipse.jdt.core.dom.ASTNode` `getNextNode(org.eclipse.jdt.core.dom.ASTNode node)` Finds the node that follows the given node.
`protected org.eclipse.jdt.core.dom.ASTNode` `getPreviousNode(org.eclipse.jdt.core.dom.ASTNode node)` Finds the node that precedes the given node.
`protected boolean` `isWhitespace(int startPosition, int endPosition)`
`void` `markNodeForRemoval(org.eclipse.jdt.core.dom.ASTNode node)` Mark this node as the node to be deleted.
`protected boolean` `shouldHaveExtendedRange(org.eclipse.jdt.core.dom.ASTNode node)` Checks if the range should be extended for the given node.
`void` `unmarkNodeForRemoval(org.eclipse.jdt.core.dom.ASTNode node)` Un-mark the node as the node for removal.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### NODE_TYPES_WITH_SPECIAL_RANGE

    protected static final java.util.Set<java.lang.Integer> NODE_TYPES_WITH_SPECIAL_RANGE

  Node types that this range computer changes range for.

### commentArray

    protected org.eclipse.jdt.core.dom.Comment[] commentArray

  Array of comments from compilation unit

### commentStartPositions

    protected int[] commentStartPositions

  Start positions of all comments

### commentEndPositions

    protected int[] commentEndPositions

  End positions of all comments

### compilationUnit

    protected org.eclipse.jdt.core.dom.CompilationUnit compilationUnit

  Compilation unit used to get extended ranges of the nodes

### includeTrailingCommentMapper

    protected java.util.Map<org.eclipse.jdt.core.dom.ASTNode,org.eclipse.jdt.core.dom.Comment> includeTrailingCommentMapper

  Map of nodes to the trailing `Comment` nodes that must be included in the range for the node

### nodesWithDefaultRange

    protected java.util.Set<org.eclipse.jdt.core.dom.ASTNode> nodesWithDefaultRange

  Set of nodes that must have default range returned for them

### source

    protected java.lang.String source

  Original source used to create compilation unit

Constructor Detail

### CommentAwareSourceRangeComputer

    public CommentAwareSourceRangeComputer(org.eclipse.jdt.core.dom.CompilationUnit compilationUnit,
                                           java.lang.String source)

Parameters:
  `compilationUnit` - to use to get a list of comments and node positions
  `source` - original source used to create compilation unit

Method Detail

### findTrailingComments

    protected org.eclipse.jdt.core.dom.Comment findTrailingComments(org.eclipse.jdt.core.dom.ASTNode node,
                                                                    org.eclipse.jdt.core.dom.ASTNode nextNode,
                                                                    boolean includeHangingCommentsOnly)

  Finds the furthest trailing comment of `node` before the next node. If `includeHangingCommentsOnly` is set to true, only comments up to the extended range of the next node will be considered. If `includeHangingCommentsOnly` is set to false, the first comment after the default range of the previous node is returned.

  Parameters:
    `node` -
    `nextNode` -
    `includeHangingCommentsOnly` -
  Returns:
    comment, `null` if not found

### findLeadingComment

    protected org.eclipse.jdt.core.dom.Comment findLeadingComment(org.eclipse.jdt.core.dom.ASTNode node,
                                                                  org.eclipse.jdt.core.dom.ASTNode previousNode,
                                                                  boolean includeHangingCommentsOnly)

  Finds the furthest leading comment of `node` after the previous node.

  If `includeHangingCommentsOnly` is set to true, only comments up to the extended range of the previous node will be considered. If `includeHangingCommentsOnly` is set to false, the first comment after the default source range of the previous node is returned.

  Parameters:
    `node` -
    `previousNode` - the previous node
    `includeHangingCommentsOnly` -
  Returns:
    comment, `null` if not found
  See Also:
    `computeDefaultSourceRange(ASTNode)`

### findLastCommentInRangeIndex

    protected final int findLastCommentInRangeIndex(int rangeStartPos,
                                                    int rangeEndPos)

  Finds index of the last comment in the given range. Start and end position of the resulting comment must be between `rangeStartPos` and `rangeEndPos` inclusively.

  Parameters:
    `rangeStartPos` -
    `rangeEndPos` -
  Returns:
    comment index, `-1` if not found

### findFirstCommentInRangeIndex

    protected final int findFirstCommentInRangeIndex(int rangeStartPos,
                                                     int rangeEndPos)

  Finds index of the first comment that is completely in the given range. Start and end position of the resulting comment must be between `rangeStartPos` and `rangeEndPos` inclusively.

  Parameters:
    `rangeStartPos` -
    `rangeEndPos` -
  Returns:
    comment index, `-1` if not found

### extendRangeForward

    protected org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange extendRangeForward(org.eclipse.jdt.core.dom.ASTNode nodeToAdd,
                                                                                                        org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange range)

  Extends the given range to include the range of `nodeToAdd`.

  Range of `nodeToAdd` must be outside and after the given `range`. Otherwise, given `range` is returned.

  Parameters:
    `nodeToAdd` -
    `range` -
  Returns:
    new range

### extendRangeBackward

    protected org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange extendRangeBackward(org.eclipse.jdt.core.dom.ASTNode nodeToAdd,
                                                                                                         org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange range)

  Extends the given range to include the range of `nodeToAdd`.

  Range of `nodeToAdd` must be outside and before the given `range`. Otherwise, given `range` is returned.

  Parameters:
    `nodeToAdd` -
    `range` -
  Returns:
    new range

### computeStartOfPrecedingComments

    protected int computeStartOfPrecedingComments(org.eclipse.jdt.core.dom.ASTNode node)

  Finds the start position of preceding comments of the given node.

  Uses source to check if there is only whitespace between comments.

  This method uses extended start position of the node as a starting position to look for comments.

  Parameters:
    `node` -
  Returns:
    start position of the first preceding comment, or extended start position if there are no comments

### computeEndOfTrailingComments

    protected int computeEndOfTrailingComments(org.eclipse.jdt.core.dom.ASTNode node)

  Finds the end position of trailing comments of the given node.

  Uses source to check if there is only whitespace between comments.

  This method uses extended end position of the node as a starting position to look for comments.

  Parameters:
    `node` -
  Returns:
    end position of the last trailing comment, or extended end position if there are no comments

### getNextNode

    protected org.eclipse.jdt.core.dom.ASTNode getNextNode(org.eclipse.jdt.core.dom.ASTNode node)

  Finds the node that follows the given node.

  Parameters:
    `node` -
  Returns:
    next node, or `null` if not found

### getPreviousNode

    protected org.eclipse.jdt.core.dom.ASTNode getPreviousNode(org.eclipse.jdt.core.dom.ASTNode node)

  Finds the node that precedes the given node.

  Parameters:
    `node` -
  Returns:
    previous node, or `null` if not found

### markNodeForRemoval

    public void markNodeForRemoval(org.eclipse.jdt.core.dom.ASTNode node)

  Mark this node as the node to be deleted.

  The node marked for removal will use the default range.

  The leading hanging comments will be added to the range of the previous node.

  Parameters:
    `node` -
  See Also:
    `computeDefaultSourceRange(ASTNode)`, `unmarkNodeForRemoval(ASTNode)`

### unmarkNodeForRemoval

    public void unmarkNodeForRemoval(org.eclipse.jdt.core.dom.ASTNode node)

  Un-mark the node as the node for removal.

  Parameters:
    `node` -
  See Also:
    `markNodeForRemoval(ASTNode)`

### determineEndPositionOfLineComment

    protected int determineEndPositionOfLineComment(int position)

  Calculate the end of the line comment that is at the same line as `position`, and with only whitespace between `position` and the start of the line comment.

  Uses source contents and comment arrays.

  Parameters:
    `position` -
  Returns:
    original position if no such comment exist

### determineEndPositionOfLineComment

    protected int determineEndPositionOfLineComment(int nodeEnd,
                                                    int nodeExtendedEnd)

  Calculate the end of the line comment that is at the same line as `nodeEnd`, and ends before `nodeExtendedEnd`.

  Does not use source contents.

  Parameters:
    `nodeEnd` -
    `nodeExtendedEnd` - line comment must end before this position
  Returns:
    original position if no such comment exist

### isWhitespace

    protected boolean isWhitespace(int startPosition,
                                   int endPosition)

  Parameters:
    `startPosition` -
    `endPosition` -
  Returns:
    `true` if there is only whitespace between start and end position

### getEnumConstantSourceRange

    protected org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange getEnumConstantSourceRange(org.eclipse.jdt.core.dom.ASTNode node)

  Specific method for enum constants. Returns range that includes preceding comments, extended range, trailing comments, and whitespace following the comments.

  Using extended range as defined above is important to keep all the comments when enum constants are moved (e.g. removed and then inserted).

  Such extended range also allows keeping right separating new line characters between constants, e.g. including trailing whitespace prevents putting constants at the end of line comments on the same line (https://bugs.eclipse.org/bugs/show_bug.cgi?id=165703).

  Parameters:
    `node` -
  Returns:
    new extended range

### addWhitespaceAfterPosition

    protected int addWhitespaceAfterPosition(int position)

  If possible, extends given position to include any whitespace following the position.

  Parameters:
    `position` -

### computeDefaultSourceRange

    public org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange computeDefaultSourceRange(org.eclipse.jdt.core.dom.ASTNode node)

  Calculates the default range for the node.

  The default range starts at an extended start position and ends at the non-extended end of the node. If the node has a line comment on the last line of the node, this comment is included in the default range.

  Parameters:
    `node` -
  Returns:
    range for the node
  See Also:
    `CompilationUnit.getExtendedStartPosition(ASTNode)`, `ASTNode.getStartPosition()`

### shouldHaveExtendedRange

    protected boolean shouldHaveExtendedRange(org.eclipse.jdt.core.dom.ASTNode node)

  Checks if the range should be extended for the given node.

  Range should be extended for all nodes in `nodesWithDefaultRange` except for enum constants and for nodes that should use the default range (i.e. removed nodes).

  Parameters:
    `node` -
  Returns:
    `true` if range should be extended, `false` otherwise

### computeSourceRange

    public org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange computeSourceRange(org.eclipse.jdt.core.dom.ASTNode node)

  Calculates the range of a node as follows:

  For any node the range is at least the range returned by `computeDefaultSourceRange(ASTNode)`.

  If the node should have an extended range (as defined by `shouldHaveExtendedRange(ASTNode)`), the range is extended to include all leading comments up to the previous node, and all hanging trailing comments if the next node has been removed.

  Overrides:
    `computeSourceRange` in class `org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer`
  See Also:
    `computeDefaultSourceRange(ASTNode)`, `shouldHaveExtendedRange(ASTNode)`, `TargetSourceRangeComputer.computeSourceRange(org.eclipse.jdt.core.dom.ASTNode)`

Copyright 2001-2006 IBM Corporation and others. All Rights Reserved.