Package org.eclipse.cdt.core.dom.rewrite
Class ASTRewrite
java.lang.Object
org.eclipse.cdt.core.dom.rewrite.ASTRewrite
Infrastructure for modifying code by describing changes to AST nodes. The AST rewriter collects
descriptions of modifications to nodes and translates these descriptions into text edits that can
then be applied to the original source. This is all done without actually modifying the original
AST. The rewrite infrastructure tries to generate minimal text changes, preserve existing
comments and indentation, and follow code formatter settings. A
IASTComment
can be
removed from or added to a node.
The initial implementation does not support nodes that implement
IASTPreprocessorStatement
or IASTProblem
.
EXPERIMENTAL. This class or interface has been added as part of a work in progress. There is no guarantee that this API will work or that it will remain the same. Please do not use this API without consulting with the CDT team.
- Since:
- 5.0
- Restriction:
- This class is not intended to be instantiated by clients.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Defines the positions of the comment. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addComment
(IASTNode node, IASTComment comment, ASTRewrite.CommentPosition pos) Assigns the comment to the node.static ASTRewrite
create
(IASTTranslationUnit node) Creates a rewriter for a translation unit.final IASTNode
createLiteralNode
(String code) Creates and returns a node for a source string that is to be inserted into the output document.getComments
(IASTNode node, ASTRewrite.CommentPosition pos) Returns comments for the given node.final ASTRewrite
insertBefore
(IASTNode parent, IASTNode insertionPoint, IASTNode newNode, org.eclipse.text.edits.TextEditGroup editGroup) Inserts the given node in this rewriter.final void
Removes the given node in this rewriter.void
removeAllComments
(IASTNode node) Removes all comments of a node.void
removeComment
(IASTNode node, IASTComment comment) Removes the givencomment
from thenode
.void
removeComments
(IASTNode node, ASTRewrite.CommentPosition pos) Removes all comments of a node at a given position.final ASTRewrite
Replaces the given node in this rewriter.org.eclipse.ltk.core.refactoring.Change
Converts all modifications recorded by this rewriter into the change object required by the refactoring framework.
-
Method Details
-
create
Creates a rewriter for a translation unit. -
createLiteralNode
Creates and returns a node for a source string that is to be inserted into the output document. The string will be inserted without being reformatted beyond correcting the indentation level.- Parameters:
code
- the string to be inserted; lines should not have extra indentation- Returns:
- a synthetic node representing the literal code.
- Throws:
IllegalArgumentException
- if the code is null.
-
remove
Removes the given node in this rewriter. The ast is not modified, the rewriter just records the removal.- Parameters:
node
- the node being removededitGroup
- the edit group in which to collect the corresponding text edits, ornull
- Throws:
IllegalArgumentException
- if the node is null, the node is not part of this rewriter's AST.
-
replace
public final ASTRewrite replace(IASTNode node, IASTNode replacement, org.eclipse.text.edits.TextEditGroup editGroup) Replaces the given node in this rewriter. The ast is not modified, the rewriter just records the replacement. The replacement node can be part of a translation-unit or it is a synthetic (newly created) node.- Parameters:
node
- the node being replacedreplacement
- the node replacing the given oneeditGroup
- the edit group in which to collect the corresponding text edits, ornull
- Returns:
- a rewriter for further rewriting the replacement node.
- Throws:
IllegalArgumentException
- if the node or the replacement is null, or if the node is not part of this rewriter's AST
-
insertBefore
public final ASTRewrite insertBefore(IASTNode parent, IASTNode insertionPoint, IASTNode newNode, org.eclipse.text.edits.TextEditGroup editGroup) Inserts the given node in this rewriter. The ast is not modified, the rewriter just records the insertion. The new node can be part of a translation-unit or it is a synthetic (newly created) node.- Parameters:
parent
- the parent the new node is added to.insertionPoint
- the node before which the insertion shall be done, ornull
for inserting after the last child.newNode
- the node being insertededitGroup
- the edit group in which to collect the corresponding text edits, ornull
- Returns:
- a rewriter for further rewriting the inserted node.
- Throws:
IllegalArgumentException
- if the parent or the newNode is null, or if the parent is not part of this rewriter's AST, or the insertionPoint is not a child of the parent.
-
rewriteAST
public org.eclipse.ltk.core.refactoring.Change rewriteAST()Converts all modifications recorded by this rewriter into the change object required by the refactoring framework.Calling this methods does not discard the modifications on record. Subsequence modifications are added to the ones already on record. If this method is called again later, the resulting text edit object will accurately reflect the net cumulative affect of all those changes.
- Returns:
- Change object describing the changes to the document corresponding to the changes recorded by this rewriter
- Since:
- 5.0
-
addComment
Assigns the comment to the node.- Parameters:
node
- the node.comment
- the comment to be attached to the node at the given position.pos
- the position of the comment.- Since:
- 5.3
-
removeComment
Removes the givencomment
from thenode
. If thecomment
is not attached to the node nothing happens.- Parameters:
node
- the node.comment
- the comment to be removed from the node.- Since:
- 6.6
-
removeComments
Removes all comments of a node at a given position.- Parameters:
node
- the node.pos
- the position of the comments to be removed from the node.- Since:
- 6.6
-
removeAllComments
Removes all comments of a node.- Parameters:
node
- the node.- Since:
- 6.6
-
getComments
Returns comments for the given node.- Parameters:
node
- the nodepos
- the position of the comments- Returns:
- All comments assigned to the node at this position
- Since:
- 5.3
-