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 * 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 * descriptions of modifications to nodes and translates these descriptions into text edits that can
* applied to the original source. This is all done without actually modifying the original AST. The rewrite * then be applied to the original source. This is all done without actually modifying the original
* infrastructure tries to generate minimal text changes, preserve existing comments and indentation, and * AST. The rewrite infrastructure tries to generate minimal text changes, preserve existing
* follow code formatter settings. A {@link IASTComment} can be removed from or added to a node. * comments and indentation, and follow code formatter settings. A {@link IASTComment} can be
* removed from or added to a node.
* <p> * <p>
* The initial implementation does not support nodes that implement {@link IASTPreprocessorStatement} or * The initial implementation does not support nodes that implement
* {@link IASTProblem}. * {@link IASTPreprocessorStatement} or {@link IASTProblem}.
* <p> * <p>
* <strong>EXPERIMENTAL</strong>. This class or interface has been added as part of a work in progress. There * <strong>EXPERIMENTAL</strong>. This class or interface has been added as part of a work in
* is no guarantee that this API will work or that it will remain the same. Please do not use this API without * progress. There is no guarantee that this API will work or that it will remain the same.
* consulting with the CDT team. * Please do not use this API without consulting with the CDT team.
* </p> * </p>
* *
* @since 5.0 * @since 5.0
* @noinstantiate This class is not intended to be instantiated by clients. * @noinstantiate This class is not intended to be instantiated by clients.
*/ */
public final class ASTRewrite { public final class ASTRewrite {
/** /**
* Defines the positions of the comment. * Defines the positions of the comment.
* *
* @since 5.3 * @since 5.3
*/ */
public enum CommentPosition { public enum CommentPosition {
/** /**
* Comments before a statement, declaration, or definition * Comments before a statement, declaration, or definition
*/ */
@ -63,8 +62,8 @@ public final class ASTRewrite {
*/ */
trailing, trailing,
/** /**
* Comments before a closing brace such as they occur in namespace-, class- and method-definitions or * Comments before a closing brace such as they occur in namespace-, class- and
* at the end of a file * method-definitions or at the end of a file
*/ */
freestanding freestanding
} }
@ -88,7 +87,8 @@ public final class ASTRewrite {
remove remove
} }
private ASTRewrite(IASTNode root, ASTModificationStore modStore, ASTModification parentMod, NodeCommentMap commentMap) { private ASTRewrite(IASTNode root, ASTModificationStore modStore, ASTModification parentMod,
NodeCommentMap commentMap) {
fRoot= root; fRoot= root;
fModificationStore= modStore; fModificationStore= modStore;
fParentMod= parentMod; fParentMod= parentMod;
@ -134,11 +134,11 @@ public final class ASTRewrite {
* *
* @param node the node being replaced * @param node the node being replaced
* @param replacement the node replacing the given one * @param replacement the node replacing the given one
* @param editGroup the edit group in which to collect the corresponding * @param editGroup the edit group in which to collect the corresponding text edits,
* text edits, or <code>null</code> * or <code>null</code>
* @return a rewriter for further rewriting the replacement node. * @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 * @throws IllegalArgumentException if the node or the replacement is null, or if the node is
* part of this rewriter's AST * not part of this rewriter's AST
*/ */
public final ASTRewrite replace(IASTNode node, IASTNode replacement, TextEditGroup editGroup) { public final ASTRewrite replace(IASTNode node, IASTNode replacement, TextEditGroup editGroup) {
if (replacement == null) { 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 * The new node can be part of a translation-unit or it is a synthetic
* (newly created) node. * (newly created) node.
* @param parent the parent the new node is added to. * @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 newNode the node being inserted
* @param editGroup the edit group in which to collect the corresponding * @param editGroup the edit group in which to collect the corresponding
* text edits, or <code>null</code> * text edits, or <code>null</code>
* @return a rewriter for further rewriting the inserted node. * @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 * @throws IllegalArgumentException if the parent or the newNode is null, or if the parent is
* part of this rewriter's AST, or the insertionPoint is not a child of the parent. * 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) { if (parent != fRoot) {
checkBelongsToAST(parent); checkBelongsToAST(parent);
} }
@ -180,8 +182,7 @@ public final class ASTRewrite {
ASTModification mod; ASTModification mod;
if (insertionPoint == null) { if (insertionPoint == null) {
mod= new ASTModification(ModificationKind.APPEND_CHILD, parent, newNode, editGroup); mod= new ASTModification(ModificationKind.APPEND_CHILD, parent, newNode, editGroup);
} } else {
else {
if (insertionPoint.getParent() != parent) { if (insertionPoint.getParent() != parent) {
throw new IllegalArgumentException(); 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 * Converts all modifications recorded by this rewriter into the change object required by
* refactoring framework. * the refactoring framework.
* <p> * <p>
* Calling this methods does not discard the modifications on record. Subsequence modifications * 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, * 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> * </p>
* *
* @return Change object describing the changes to the * @return Change object describing the changes to the
@ -235,13 +237,12 @@ public final class ASTRewrite {
} }
} }
/** /**
* Assigns the comment to the node. * Assigns the comment to the node.
* *
* @param node * @param node the node.
* @param comment * @param comment the comment to be attached to the node at the given position.
* @param pos * @param pos the position of the comment.
* @since 5.3 * @since 5.3
*/ */
public void addComment(IASTNode node, IASTComment comment, CommentPosition pos) { public void addComment(IASTNode node, IASTComment comment, CommentPosition pos) {
@ -258,13 +259,10 @@ public final class ASTRewrite {
} }
} }
/** /**
* *
* @param node * @param node the node
* the node * @param pos the position
* @param pos
* the position
* @return All comments assigned to the node at this position * @return All comments assigned to the node at this position
* @since 5.3 * @since 5.3
*/ */
@ -276,7 +274,6 @@ public final class ASTRewrite {
return fCommentMap.getTrailingCommentsForNode(node); return fCommentMap.getTrailingCommentsForNode(node);
case freestanding: case freestanding:
return fCommentMap.getFreestandingCommentsForNode(node); return fCommentMap.getFreestandingCommentsForNode(node);
} }
return fCommentMap.getLeadingCommentsForNode(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; 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 * to nodes introduced by insertions or replacements, these modifications are collected
* in separate modification maps. I.e. a modification map represents one level of * in separate modification maps. I.e. a modification map represents one level of
* modifications. * modifications.
@ -28,21 +28,20 @@ import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKin
* @since 5.0 * @since 5.0
*/ */
public class ASTModificationMap { 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. * Adds a modification to this modification map.
*/ */
public void addModification(ASTModification mod) { 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); List<ASTModification> mods= fModifications.get(targetNode);
if (mods == null || mods.isEmpty()) { if (mods == null || mods.isEmpty()) {
mods= new ArrayList<ASTModification>(); mods= new ArrayList<ASTModification>();
mods.add(mod); mods.add(mod);
fModifications.put(targetNode, mods); fModifications.put(targetNode, mods);
} } else {
else {
switch (mod.getKind()) { switch (mod.getKind()) {
case REPLACE: case REPLACE:
if (mods.get(mods.size() - 1).getKind() != ModificationKind.INSERT_BEFORE) { if (mods.get(mods.size() - 1).getKind() != ModificationKind.INSERT_BEFORE) {
@ -58,7 +57,7 @@ public class ASTModificationMap {
break; break;
case INSERT_BEFORE: case INSERT_BEFORE:
int i; int i;
for (i=mods.size()-1; i>=0; i--) { for (i= mods.size(); --i >= 0;) {
if (mods.get(i).getKind() == ModificationKind.INSERT_BEFORE) { if (mods.get(i).getKind() == ModificationKind.INSERT_BEFORE) {
break; break;
} }
@ -70,9 +69,10 @@ public class ASTModificationMap {
} }
/** /**
* Returns the list of modifications for a given node. The list can contain different modifications. * Returns the list of modifications for a given node. The list can contain different
* It is guaranteed that INSERT_BEFORE modifications appear first. Furthermore, if there is a * modifications. It is guaranteed that INSERT_BEFORE modifications appear first. Furthermore,
* REPLACE modification the list will not contain any other REPLACE or APPEND_CHILD modifications. * 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. * @return the modification list, which may be empty.
*/ */
public List<ASTModification> getModificationsForNode(IASTNode node) { public List<ASTModification> getModificationsForNode(IASTNode node) {

View file

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