/** * Internal constructor. Creates a new instance for the given AST. * Clients should use {@link #create(AST)} to create instances. * * @param ast the AST being rewritten */ protected ASTRewrite(AST ast) { this.ast= ast; this.eventStore= new RewriteEventStore(); this.nodeStore= new NodeInfoStore(ast); }
private ASTNode createTargetNode(ASTNode first, ASTNode last, boolean isMove, ASTNode replacingNode, TextEditGroup editGroup) { if (first == null || last == null) { throw new IllegalArgumentException(); } NodeInfoStore nodeStore= this.rewriter.getNodeStore(); ASTNode placeholder= nodeStore.newPlaceholderNode(first.getNodeType()); // revisit: could use list type if (placeholder == null) { throw new IllegalArgumentException("Creating a target node is not supported for nodes of type" + first.getClass().getName()); //$NON-NLS-1$ } Block internalPlaceHolder= nodeStore.createCollapsePlaceholder(); CopySourceInfo info= getRewriteStore().createRangeCopy(this.parent, this.childListProperty, first, last, isMove, internalPlaceHolder, replacingNode, editGroup); nodeStore.markAsCopyTarget(placeholder, info); return placeholder; }
private final boolean isCollapsed(ASTNode node) { return this.nodeInfos.isCollapsed(node); }
/** * Creates and returns a placeholder node for a source string that is to be inserted into * the output document at the position corresponding to the placeholder. * The string will be inserted without being reformatted beyond correcting * the indentation level. The placeholder node can either be inserted as new or * used to replace an existing node. * * @param code the string to be inserted; lines should should not have extra indentation * @param nodeType the ASTNode type that corresponds to the passed code. * @return the new placeholder node * @throws IllegalArgumentException if the code is null, or if the node * type is invalid */ public final ASTNode createStringPlaceholder(String code, int nodeType) { if (code == null) { throw new IllegalArgumentException(); } ASTNode placeholder= getNodeStore().newPlaceholderNode(nodeType); if (placeholder == null) { throw new IllegalArgumentException("String placeholder is not supported for type" + nodeType); //$NON-NLS-1$ } getNodeStore().markAsStringPlaceholder(placeholder, code); return placeholder; }
private ASTNode createTargetNode(ASTNode node, boolean isMove) { if (node == null) { throw new IllegalArgumentException(); } validateIsExistingNode(node); validateIsCorrectAST(node); CopySourceInfo info= getRewriteEventStore().markAsCopySource(node.getParent(), node.getLocationInParent(), node, isMove); ASTNode placeholder= getNodeStore().newPlaceholderNode(node.getNodeType()); if (placeholder == null) { throw new IllegalArgumentException("Creating a target node is not supported for nodes of type" + node.getClass().getName()); //$NON-NLS-1$ } getNodeStore().markAsCopyTarget(placeholder, info); return placeholder; }
/** * Marks a node as a placehoder for a plain string content. The type of the node should correspond to the * code's code content. * @param placeholder The placeholder node that acts for the string content. * @param code The string content. */ public final void markAsStringPlaceholder(ASTNode placeholder, String code) { StringPlaceholderData data= new StringPlaceholderData(); data.code= code; setPlaceholderData(placeholder, data); }
public void preVisit(ASTNode node) { Object trackData= getEventStore().getTrackedNodeData(node); if (trackData != null) { addMarker(trackData, this.result.length(), 0); } Object placeholderData= getPlaceholders().getPlaceholderData(node); if (placeholderData != null) { addMarker(placeholderData, this.result.length(), 0); } }
private void markAsMoveOrCopyTarget(ASTNode node, ASTNode newChild) { ASTNode source = (ASTNode)this.clonedNodes.get(newChild); if(source != null) { if(this.cloneDepth == 0) { PropertyLocation propertyLocation = this.eventStore.getPropertyLocation(source, RewriteEventStore.ORIGINAL); CopySourceInfo sourceInfo = this.eventStore.markAsCopySource( propertyLocation.getParent(), propertyLocation.getProperty(), source, false); this.nodeStore.markAsCopyTarget(newChild, sourceInfo); } } else if((newChild.getFlags() & ASTNode.ORIGINAL) != 0) { PropertyLocation propertyLocation = this.eventStore.getPropertyLocation(newChild, RewriteEventStore.ORIGINAL); CopySourceInfo sourceInfo = this.eventStore.markAsCopySource( propertyLocation.getParent(), propertyLocation.getProperty(), newChild, true); this.nodeStore.markAsCopyTarget(newChild, sourceInfo); } }
/** * Creates and returns a node that represents a sequence of nodes. * Each of the given nodes must be either be brand new (not part of the original AST), or * a placeholder node (for example, one created by {@link #createCopyTarget(ASTNode)} * or {@link #createStringPlaceholder(String, int)}), or another group node. * The type of the returned node is unspecified. The returned node can be used * to replace an existing node (or as an element of another group node). * When the document is rewritten, the source code for each of the given nodes is * inserted, in order, into the output document at the position corresponding to the * group (indentation is adjusted). * * @param targetNodes the nodes to go in the group * @return the new group node * @throws IllegalArgumentException if the targetNodes is <code>null</code> or empty * @since 3.1 */ public final ASTNode createGroupNode(ASTNode[] targetNodes) { if (targetNodes == null || targetNodes.length == 0) { throw new IllegalArgumentException(); } Block res= getNodeStore().createCollapsePlaceholder(); ListRewrite listRewrite= getListRewrite(res, Block.STATEMENTS_PROPERTY); for (int i= 0; i < targetNodes.length; i++) { listRewrite.insertLast(targetNodes[i], null); } return res; }
/** * Disposes this ASTRewriteWithRemove */ @SuppressWarnings("restriction") public void dispose() { getRewriteEventStore().clear(); getNodeStore().clear(); setTargetSourceRangeComputer(null); }
private ASTNode createTargetNode(ASTNode node, boolean isMove) { if (node == null) { throw new IllegalArgumentException(); } validateIsExistingNode(node); validateIsCorrectAST(node); CopySourceInfo info= getRewriteEventStore().markAsCopySource(node.getParent(), node.getLocationInParent(), node, isMove); ASTNode placeholder= getNodeStore().newPlaceholderNode(node.getNodeType()); if (placeholder == null) { throw new IllegalArgumentException("Creating a target node is not supported for nodes of type" + node.getClass().getName()); //$NON-NLS-1$ } getNodeStore().markAsCopyTarget(placeholder, info); return placeholder; }
/** * Creates and returns a placeholder node for a source string that is to be inserted into * the output document at the position corresponding to the placeholder. * The string will be inserted without being reformatted beyond correcting * the indentation level. The placeholder node can either be inserted as new or * used to replace an existing node. * * @param code the string to be inserted; lines should should not have extra indentation * @param nodeType the ASTNode type that corresponds to the passed code. * @return the new placeholder node * @throws IllegalArgumentException if the code is null, or if the node * type is invalid */ public final ASTNode createStringPlaceholder(String code, int nodeType) { if (code == null) { throw new IllegalArgumentException(); } ASTNode placeholder= getNodeStore().newPlaceholderNode(nodeType); if (placeholder == null) { throw new IllegalArgumentException("String placeholder is not supported for type" + nodeType); //$NON-NLS-1$ } getNodeStore().markAsStringPlaceholder(placeholder, code); return placeholder; }
/** * Marks a node as a copy or move target. The copy target represents a copied node at the target (copied) site. * @param target The node at the target site. Can be a placeholder node but also the source node itself. * @param copySource The info at the source site. */ public final void markAsCopyTarget(ASTNode target, CopySourceInfo copySource) { CopyPlaceholderData data= new CopyPlaceholderData(); data.copySource= copySource; setPlaceholderData(target, data); }
public void postVisit(ASTNode node) { Object placeholderData= getPlaceholders().getPlaceholderData(node); if (placeholderData != null) { fixupLength(placeholderData, this.result.length()); } Object trackData= getEventStore().getTrackedNodeData(node); if (trackData != null) { fixupLength(trackData, this.result.length()); } }
source, false); this.nodeStore.markAsCopyTarget(newChild, sourceInfo); newChild, true); this.nodeStore.markAsCopyTarget(newChild, sourceInfo);
/** * Creates and returns a node that represents a sequence of nodes. * Each of the given nodes must be either be brand new (not part of the original AST), or * a placeholder node (for example, one created by {@link #createCopyTarget(ASTNode)} * or {@link #createStringPlaceholder(String, int)}), or another group node. * The type of the returned node is unspecified. The returned node can be used * to replace an existing node (or as an element of another group node). * When the document is rewritten, the source code for each of the given nodes is * inserted, in order, into the output document at the position corresponding to the * group (indentation is adjusted). * * @param targetNodes the nodes to go in the group * @return the new group node * @throws IllegalArgumentException if the targetNodes is <code>null</code> or empty * @since 3.1 */ public final ASTNode createGroupNode(ASTNode[] targetNodes) { if (targetNodes == null || targetNodes.length == 0) { throw new IllegalArgumentException(); } Block res= getNodeStore().createCollapsePlaceholder(); ListRewrite listRewrite= getListRewrite(res, Block.STATEMENTS_PROPERTY); for (int i= 0; i < targetNodes.length; i++) { listRewrite.insertLast(targetNodes[i], null); } return res; }
/** * Disposes this ASTRewriteWithRemove */ @SuppressWarnings("restriction") public void dispose() { getRewriteEventStore().clear(); getNodeStore().clear(); setTargetSourceRangeComputer(null); }
private ASTNode createTargetNode(ASTNode first, ASTNode last, boolean isMove, ASTNode replacingNode, TextEditGroup editGroup) { if (first == null || last == null) { throw new IllegalArgumentException(); } NodeInfoStore nodeStore= this.rewriter.getNodeStore(); ASTNode placeholder= nodeStore.newPlaceholderNode(first.getNodeType()); // revisit: could use list type if (placeholder == null) { throw new IllegalArgumentException("Creating a target node is not supported for nodes of type" + first.getClass().getName()); //$NON-NLS-1$ } Block internalPlaceHolder= nodeStore.createCollapsePlaceholder(); CopySourceInfo info= getRewriteStore().createRangeCopy(this.parent, this.childListProperty, first, last, isMove, internalPlaceHolder, replacingNode, editGroup); nodeStore.markAsCopyTarget(placeholder, info); return placeholder; }
private ASTNode createTargetNode(ASTNode node, boolean isMove) { if (node == null) { throw new IllegalArgumentException(); } validateIsExistingNode(node); validateIsCorrectAST(node); CopySourceInfo info= getRewriteEventStore().markAsCopySource(node.getParent(), node.getLocationInParent(), node, isMove); ASTNode placeholder= getNodeStore().newPlaceholderNode(node.getNodeType()); if (placeholder == null) { throw new IllegalArgumentException("Creating a target node is not supported for nodes of type" + node.getClass().getName()); //$NON-NLS-1$ } getNodeStore().markAsCopyTarget(placeholder, info); return placeholder; }
/** * Creates and returns a placeholder node for a source string that is to be inserted into * the output document at the position corresponding to the placeholder. * The string will be inserted without being reformatted beyond correcting * the indentation level. The placeholder node can either be inserted as new or * used to replace an existing node. * * @param code the string to be inserted; lines should should not have extra indentation * @param nodeType the ASTNode type that corresponds to the passed code. * @return the new placeholder node * @throws IllegalArgumentException if the code is null, or if the node * type is invalid */ public final ASTNode createStringPlaceholder(String code, int nodeType) { if (code == null) { throw new IllegalArgumentException(); } ASTNode placeholder= getNodeStore().newPlaceholderNode(nodeType); if (placeholder == null) { throw new IllegalArgumentException("String placeholder is not supported for type" + nodeType); //$NON-NLS-1$ } getNodeStore().markAsStringPlaceholder(placeholder, code); return placeholder; }