|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer
org.eclipse.emf.codegen.merge.java.facade.ast.CommentAwareSourceRangeComputer
public class CommentAwareSourceRangeComputer
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.
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 farthest 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 farthest 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)
Unmark 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 |
---|
protected static final java.util.Set<java.lang.Integer> NODE_TYPES_WITH_SPECIAL_RANGE
protected org.eclipse.jdt.core.dom.Comment[] commentArray
protected int[] commentStartPositions
protected int[] commentEndPositions
protected org.eclipse.jdt.core.dom.CompilationUnit compilationUnit
protected java.util.Map<org.eclipse.jdt.core.dom.ASTNode,org.eclipse.jdt.core.dom.Comment> includeTrailingCommentMapper
Comment
nodes that must be included
in the range for the node
protected java.util.Set<org.eclipse.jdt.core.dom.ASTNode> nodesWithDefaultRange
protected java.lang.String source
Constructor Detail |
---|
public CommentAwareSourceRangeComputer(org.eclipse.jdt.core.dom.CompilationUnit compilationUnit, java.lang.String source)
compilationUnit
- to use to get a list of comments and node positionssource
- original source used to create compilation unitMethod Detail |
---|
protected org.eclipse.jdt.core.dom.Comment findTrailingComments(org.eclipse.jdt.core.dom.ASTNode node, org.eclipse.jdt.core.dom.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 org.eclipse.jdt.core.dom.Comment findLeadingComment(org.eclipse.jdt.core.dom.ASTNode node, org.eclipse.jdt.core.dom.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 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)
nodeToAdd
.
Range of nodeToAdd
must be outside and after the given range
.
Otherwise, given range
is returned.
nodeToAdd
- range
-
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)
nodeToAdd
.
Range of nodeToAdd
must be outside and before the given range
.
Otherwise, given range
is returned.
nodeToAdd
- range
-
protected int computeStartOfPrecedingComments(org.eclipse.jdt.core.dom.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(org.eclipse.jdt.core.dom.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 org.eclipse.jdt.core.dom.ASTNode getNextNode(org.eclipse.jdt.core.dom.ASTNode node)
node
-
null
if not foundprotected org.eclipse.jdt.core.dom.ASTNode getPreviousNode(org.eclipse.jdt.core.dom.ASTNode node)
node
-
null
if not foundpublic void markNodeForRemoval(org.eclipse.jdt.core.dom.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(org.eclipse.jdt.core.dom.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 position
protected boolean isWhitespace(int startPosition, int endPosition)
startPosition
- endPosition
-
true
if there is only whitespace between start and end positionprotected org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange getEnumConstantSourceRange(org.eclipse.jdt.core.dom.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 org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange computeDefaultSourceRange(org.eclipse.jdt.core.dom.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(org.eclipse.jdt.core.dom.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 org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer.SourceRange computeSourceRange(org.eclipse.jdt.core.dom.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.
computeSourceRange
in class org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer
computeDefaultSourceRange(ASTNode)
,
shouldHaveExtendedRange(ASTNode)
,
TargetSourceRangeComputer.computeSourceRange(org.eclipse.jdt.core.dom.ASTNode)
|
Copyright 2001-2006 IBM Corporation and others. All Rights Reserved. |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |