1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Cosmetics.

This commit is contained in:
sprigogin 2011-08-05 20:00:20 -07:00
parent 1293690a4c
commit 26acd7fa6a
3 changed files with 59 additions and 61 deletions

View file

@ -29,31 +29,30 @@ import org.eclipse.text.edits.TextEditGroup;
/**
* 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 {@link IASTComment} can be removed from or added to a node.
* 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 {@link IASTComment} can be
* removed from or added to a node.
* <p>
* The initial implementation does not support nodes that implement {@link IASTPreprocessorStatement} or
* {@link IASTProblem}.
* The initial implementation does not support nodes that implement
* {@link IASTPreprocessorStatement} or {@link IASTProblem}.
* <p>
* <strong>EXPERIMENTAL</strong>. 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.
* <strong>EXPERIMENTAL</strong>. 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.
* </p>
*
* @since 5.0
* @noinstantiate This class is not intended to be instantiated by clients.
*/
public final class ASTRewrite {
/**
* Defines the positions of the comment.
*
* @since 5.3
*/
public enum CommentPosition{
public enum CommentPosition {
/**
* Comments before a statement, declaration, or definition
*/
@ -63,8 +62,8 @@ public final class ASTRewrite {
*/
trailing,
/**
* Comments before a closing brace such as they occur in namespace-, class- and method-definitions or
* at the end of a file
* Comments before a closing brace such as they occur in namespace-, class- and
* method-definitions or at the end of a file
*/
freestanding
}
@ -82,13 +81,14 @@ public final class ASTRewrite {
private final ASTModification fParentMod;
private final NodeCommentMap fCommentMap;
private enum Operation{
private enum Operation {
insertBefore,
replace,
remove
}
private ASTRewrite(IASTNode root, ASTModificationStore modStore, ASTModification parentMod, NodeCommentMap commentMap) {
private ASTRewrite(IASTNode root, ASTModificationStore modStore, ASTModification parentMod,
NodeCommentMap commentMap) {
fRoot= root;
fModificationStore= modStore;
fParentMod= parentMod;
@ -134,11 +134,11 @@ public final class ASTRewrite {
*
* @param node the node being replaced
* @param replacement the node replacing the given one
* @param editGroup the edit group in which to collect the corresponding
* text edits, or <code>null</code>
* @param editGroup the edit group in which to collect the corresponding text edits,
* or <code>null</code>
* @return 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
* @throws IllegalArgumentException if the node or the replacement is null, or if the node is
* not part of this rewriter's AST
*/
public final ASTRewrite replace(IASTNode node, IASTNode replacement, TextEditGroup editGroup) {
if (replacement == null) {
@ -158,15 +158,17 @@ public final class ASTRewrite {
* The new node can be part of a translation-unit or it is a synthetic
* (newly created) node.
* @param parent the parent the new node is added to.
* @param insertionPoint the node before which the insertion shall be done, or <code>null</code> for inserting after the last child.
* @param insertionPoint the node before which the insertion shall be done, or <code>null</code>
* for inserting after the last child.
* @param newNode the node being inserted
* @param editGroup the edit group in which to collect the corresponding
* text edits, or <code>null</code>
* @return 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.
* @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.
*/
public final ASTRewrite insertBefore(IASTNode parent, IASTNode insertionPoint, IASTNode newNode, TextEditGroup editGroup) {
public final ASTRewrite insertBefore(IASTNode parent, IASTNode insertionPoint, IASTNode newNode,
TextEditGroup editGroup) {
if (parent != fRoot) {
checkBelongsToAST(parent);
}
@ -180,8 +182,7 @@ public final class ASTRewrite {
ASTModification mod;
if (insertionPoint == null) {
mod= new ASTModification(ModificationKind.APPEND_CHILD, parent, newNode, editGroup);
}
else {
} else {
if (insertionPoint.getParent() != parent) {
throw new IllegalArgumentException();
}
@ -192,12 +193,13 @@ public final class ASTRewrite {
}
/**
* Converts all modifications recorded by this rewriter into the change object required by the
* refactoring framework.
* Converts all modifications recorded by this rewriter into the change object required by
* the refactoring framework.
* <p>
* 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.
* the resulting text edit object will accurately reflect the net cumulative affect of all those
* changes.
* </p>
*
* @return Change object describing the changes to the
@ -223,7 +225,7 @@ public final class ASTRewrite {
private void checkSupportedNode(IASTNode node, Operation op) {
if (node instanceof IASTComment) {
if(op != Operation.remove) {
if (op != Operation.remove) {
throw new IllegalArgumentException("Rewriting comments is not yet supported"); //$NON-NLS-1$
}
}
@ -235,13 +237,12 @@ public final class ASTRewrite {
}
}
/**
* Assigns the comment to the node.
*
* @param node
* @param comment
* @param pos
* @param node the node.
* @param comment the comment to be attached to the node at the given position.
* @param pos the position of the comment.
* @since 5.3
*/
public void addComment(IASTNode node, IASTComment comment, CommentPosition pos) {
@ -258,13 +259,10 @@ public final class ASTRewrite {
}
}
/**
*
* @param node
* the node
* @param pos
* the position
* @param node the node
* @param pos the position
* @return All comments assigned to the node at this position
* @since 5.3
*/
@ -276,7 +274,6 @@ public final class ASTRewrite {
return fCommentMap.getTrailingCommentsForNode(node);
case freestanding:
return fCommentMap.getFreestandingCommentsForNode(node);
}
return fCommentMap.getLeadingCommentsForNode(node);
}

View file

@ -20,7 +20,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind;
/**
* Represents a list of modifications to an ast-node. If there are nested modifications
* Represents a list of modifications to an AST node. If there are nested modifications
* to nodes introduced by insertions or replacements, these modifications are collected
* in separate modification maps. I.e. a modification map represents one level of
* modifications.
@ -28,51 +28,51 @@ import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKin
* @since 5.0
*/
public class ASTModificationMap {
private HashMap<IASTNode,List<ASTModification>> fModifications= new HashMap<IASTNode,List<ASTModification>>();
private HashMap<IASTNode, List<ASTModification>> fModifications= new HashMap<IASTNode, List<ASTModification>>();
/**
* Adds a modification to this modification map.
*/
public void addModification(ASTModification mod) {
final IASTNode targetNode = mod.getKind()==ASTModification.ModificationKind.INSERT_BEFORE ? mod.getTargetNode().getParent() :mod.getTargetNode();
final IASTNode targetNode = mod.getKind() == ASTModification.ModificationKind.INSERT_BEFORE ?
mod.getTargetNode().getParent() : mod.getTargetNode();
List<ASTModification> mods= fModifications.get(targetNode);
if (mods == null || mods.isEmpty()) {
mods= new ArrayList<ASTModification>();
mods.add(mod);
fModifications.put(targetNode, mods);
}
else {
} else {
switch (mod.getKind()) {
case REPLACE:
if (mods.get(mods.size()-1).getKind() != ModificationKind.INSERT_BEFORE ) {
if (mods.get(mods.size() - 1).getKind() != ModificationKind.INSERT_BEFORE) {
throw new IllegalArgumentException("Attempt to replace a node that has been modified"); //$NON-NLS-1$
}
mods.add(mod);
break;
case APPEND_CHILD:
if (mods.get(mods.size()-1).getKind() == ModificationKind.REPLACE) {
if (mods.get(mods.size() - 1).getKind() == ModificationKind.REPLACE) {
throw new IllegalArgumentException("Attempt to modify a node that has been replaced"); //$NON-NLS-1$
}
mods.add(mod);
break;
case INSERT_BEFORE:
int i;
for (i=mods.size()-1; i>=0; i--) {
for (i= mods.size(); --i >= 0;) {
if (mods.get(i).getKind() == ModificationKind.INSERT_BEFORE) {
break;
}
}
mods.add(i+1, mod);
mods.add(i + 1, mod);
break;
}
}
}
/**
* Returns the list of modifications for a given node. The list can contain different modifications.
* It is guaranteed that INSERT_BEFORE modifications appear first. Furthermore, if there is a
* REPLACE modification the list will not contain any other REPLACE or APPEND_CHILD modifications.
* Returns the list of modifications for a given node. The list can contain different
* modifications. It is guaranteed that INSERT_BEFORE modifications appear first. Furthermore,
* if there is a REPLACE modification the list will not contain any other REPLACE or
* APPEND_CHILD modifications.
* @return the modification list, which may be empty.
*/
public List<ASTModification> getModificationsForNode(IASTNode node) {

View file

@ -195,8 +195,9 @@ public class ChangeGenerator extends ASTVisitor {
createChange(synthNode, synthSource);
int newOffset = synthNode.getFileLocation().getNodeOffset() + synthNode.getFileLocation().getNodeLength();
sourceOffsets.put(synthNode.getFileLocation().getFileName(), Integer.valueOf(newOffset));
IASTFileLocation fileLocation = synthNode.getFileLocation();
int newOffset = fileLocation.getNodeOffset() + fileLocation.getNodeLength();
sourceOffsets.put(fileLocation.getFileName(), Integer.valueOf(newOffset));
}
private void synthTreatment(IASTTranslationUnit synthTU) {