public class CommentAwareSourceRangeComputer
extends TargetSourceRangeComputer
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.
ASTRewrite#setTargetSourceRangeComputer(TargetSourceRangeComputer)
,
TargetSourceRangeComputer
Modifier and Type | Field and Description |
---|---|
protected 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 CompilationUnit |
compilationUnit
Compilation unit used to get extended ranges of the nodes
|
protected java.util.Map<ASTNode,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<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 and Description |
---|
CommentAwareSourceRangeComputer(CompilationUnit compilationUnit,
java.lang.String source) |
Modifier and Type | Method and Description |
---|---|
protected int |
addWhitespaceAfterPosition(int position)
If possible, extends given position to include any whitespace following the position.
|
SourceRange |
computeDefaultSourceRange(ASTNode node)
Calculates the default range for the node.
|
protected int |
computeEndOfTrailingComments(ASTNode node)
Finds the end position of trailing comments of the given node.
|
SourceRange |
computeSourceRange(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(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 SourceRange |
extendRangeBackward(ASTNode nodeToAdd,
SourceRange range)
Extends the given range to include the range of
nodeToAdd . |
protected SourceRange |
extendRangeForward(ASTNode nodeToAdd,
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 Comment |
findLeadingComment(ASTNode node,
ASTNode previousNode,
boolean includeHangingCommentsOnly)
Finds the farthest leading comment of
node after the previous node. |
protected Comment |
findTrailingComments(ASTNode node,
ASTNode nextNode,
boolean includeHangingCommentsOnly)
Finds the farthest trailing comment of
node before the next node. |
protected SourceRange |
getEnumConstantSourceRange(ASTNode node)
Specific method for enum constants.
|
protected ASTNode |
getNextNode(ASTNode node)
Finds the node that follows the given node.
|
protected ASTNode |
getPreviousNode(ASTNode node)
Finds the node that precedes the given node.
|
protected boolean |
isWhitespace(int startPosition,
int endPosition) |
void |
markNodeForRemoval(ASTNode node)
Mark this node as the node to be deleted.
|
protected boolean |
shouldHaveExtendedRange(ASTNode node)
Checks if the range should be extended for the given node.
|
void |
unmarkNodeForRemoval(ASTNode node)
Unmark the node as the node for removal.
|
protected static final java.util.Set<java.lang.Integer> NODE_TYPES_WITH_SPECIAL_RANGE
protected Comment[] commentArray
protected int[] commentStartPositions
protected int[] commentEndPositions
protected CompilationUnit compilationUnit
protected java.util.Map<ASTNode,Comment> includeTrailingCommentMapper
Comment
nodes that must be included
in the range for the nodeprotected java.util.Set<ASTNode> nodesWithDefaultRange
protected java.lang.String source
public CommentAwareSourceRangeComputer(CompilationUnit compilationUnit, java.lang.String source)
compilationUnit
- to use to get a list of comments and node positionssource
- original source used to create compilation unitprotected Comment findTrailingComments(ASTNode node, ASTNode nextNode, boolean includeHangingCommentsOnly)
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.node
- nextNode
- includeHangingCommentsOnly
- null
if not foundprotected Comment findLeadingComment(ASTNode node, ASTNode previousNode, boolean includeHangingCommentsOnly)
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.
node
- previousNode
- the previous nodeincludeHangingCommentsOnly
- null
if not foundcomputeDefaultSourceRange(ASTNode)
protected final int findLastCommentInRangeIndex(int rangeStartPos, int rangeEndPos)
rangeStartPos
and rangeEndPos
inclusively.rangeStartPos
- rangeEndPos
- -1
if not foundprotected final int findFirstCommentInRangeIndex(int rangeStartPos, int rangeEndPos)
rangeStartPos
and rangeEndPos
inclusively.rangeStartPos
- rangeEndPos
- -1
if not foundprotected SourceRange extendRangeForward(ASTNode nodeToAdd, SourceRange range)
nodeToAdd
.
Range of nodeToAdd
must be outside and after the given range
.
Otherwise, given range
is returned.
nodeToAdd
- range
- protected SourceRange extendRangeBackward(ASTNode nodeToAdd, SourceRange range)
nodeToAdd
.
Range of nodeToAdd
must be outside and before the given range
.
Otherwise, given range
is returned.
nodeToAdd
- range
- protected int computeStartOfPrecedingComments(ASTNode 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.
node
- protected int computeEndOfTrailingComments(ASTNode 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.
node
- protected ASTNode getNextNode(ASTNode node)
node
- null
if not foundprotected ASTNode getPreviousNode(ASTNode node)
node
- null
if not foundpublic void markNodeForRemoval(ASTNode node)
The node marked for removal will use the default range.
The leading hanging comments will be added to the range of the previous node.
node
- computeDefaultSourceRange(ASTNode)
,
unmarkNodeForRemoval(ASTNode)
public void unmarkNodeForRemoval(ASTNode node)
node
- markNodeForRemoval(ASTNode)
protected int determineEndPositionOfLineComment(int position)
position
,
and with only whitespace between position
and the start of the line comment.
Uses source contents and comment arrays.
position
- protected int determineEndPositionOfLineComment(int nodeEnd, int nodeExtendedEnd)
nodeEnd
,
and ends before nodeExtendedEnd
.
Does not use source contents.
nodeEnd
- nodeExtendedEnd
- line comment must end before this positionprotected boolean isWhitespace(int startPosition, int endPosition)
startPosition
- endPosition
- true
if there is only whitespace between start and end positionprotected SourceRange getEnumConstantSourceRange(ASTNode node)
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).
node
- protected int addWhitespaceAfterPosition(int position)
position
- public SourceRange computeDefaultSourceRange(ASTNode 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.
node
- CompilationUnit#getExtendedStartPosition(ASTNode)
,
ASTNode#getStartPosition()
protected boolean shouldHaveExtendedRange(ASTNode 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).
node
- true
if range should be extended, false
otherwisepublic SourceRange computeSourceRange(ASTNode node)
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.
computeDefaultSourceRange(ASTNode)
,
shouldHaveExtendedRange(ASTNode)
,
org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer#computeSourceRange(org.eclipse.jdt.core.dom.ASTNode)