1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-05 07:15:39 +02:00

Bug 537013 - Comment is replaced along with node

Provide functionality to remove a specific comment from ASTRewrite.

Change-Id: I5e0b3c521616d2dd900f59b0863e45fb01f34df8
Signed-off-by: Thomas Corbat <tcorbat@hsr.ch>
This commit is contained in:
Thomas Corbat 2018-07-13 16:08:24 +02:00
parent 1a8f399b8b
commit adbaa608cb
3 changed files with 172 additions and 1 deletions

View file

@ -13,6 +13,8 @@ package org.eclipse.cdt.core.parser.tests.rewrite.comenthandler;
import junit.framework.TestCase;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
@ -123,7 +125,76 @@ public class NodeCommentMapTest extends TestCase {
assertEquals(com2, map.getFreestandingCommentsForNode(node2).get(0));
assertEquals(com3, map.getFreestandingCommentsForNode(node1).get(1));
}
protected ASTNode initCommentMap() {
ASTNode node = new CPPASTName();
for (int i = 0; i < 3; i++) {
IASTComment leadingComment = new Comment();
map.addLeadingCommentToNode(node, leadingComment);
IASTComment trailingComment = new Comment();
map.addTrailingCommentToNode(node, trailingComment);
IASTComment freestandingComment = new Comment();
map.addFreestandingCommentToNode(node, freestandingComment);
}
return node;
}
public void testRemoveCommentFromNode() {
ASTNode node = initCommentMap();
List<IASTComment> trailingComments = map.getTrailingCommentsForNode(node);
IASTComment commentToBeRemoved = trailingComments.get(1);
map.removeCommentFromNode(node, commentToBeRemoved);
List<IASTComment> allComments = map.getAllCommentsForNode(node);
assertEquals(8, allComments.size());
assertFalse(allComments.contains(commentToBeRemoved));
assertFalse(map.getTrailingCommentsForNode(node).contains(commentToBeRemoved));
}
public void testRemoveLeadingCommentsFromNode() {
ASTNode node = initCommentMap();
map.removeLeadingCommentsFromNode(node);
List<IASTComment> leadingComments = map.getLeadingCommentsForNode(node);
assertEquals(0, leadingComments.size());
List<IASTComment> allComments = map.getAllCommentsForNode(node);
assertEquals(6, allComments.size());
}
public void testRemoveTrailingCommentsFromNode() {
ASTNode node = initCommentMap();
map.removeTrailingCommentsFromNode(node);
List<IASTComment> trailingComments = map.getTrailingCommentsForNode(node);
assertEquals(0, trailingComments.size());
List<IASTComment> allComments = map.getAllCommentsForNode(node);
assertEquals(6, allComments.size());
}
public void testRemoveFreestandingCommentsFromNode() {
ASTNode node = initCommentMap();
map.removeFreestandingCommentsFromNode(node);
List<IASTComment> freestandingComments = map.getFreestandingCommentsForNode(node);
assertEquals(0, freestandingComments.size());
List<IASTComment> allComments = map.getAllCommentsForNode(node);
assertEquals(6, allComments.size());
}
public void testAllCommentsFromNode() {
ASTNode node = initCommentMap();
map.removeAllComments(node);
List<IASTComment> allComments = map.getAllCommentsForNode(node);
assertEquals(0, allComments.size());
}
//=== Internal Comment class for testing
private static class Comment extends ASTNode implements IASTComment {
private char[] comment;

View file

@ -259,6 +259,49 @@ public final class ASTRewrite {
}
}
/**
* Removes the given <code>comment</code> from the <code>node</code>. If the
* <code>comment</code> is not attached to the node nothing happens.
*
* @param node the node.
* @param comment the comment to be removed from the node.
* @since 6.5
*/
public void removeComment(IASTNode node, IASTComment comment) {
fCommentMap.removeCommentFromNode(node, comment);
}
/**
* Removes all comments of a node at a given position.
*
* @param node the node.
* @param pos the position of the comments to be removed from the node.
* @since 6.5
*/
public void removeComments(IASTNode node, CommentPosition pos) {
switch (pos) {
case leading:
fCommentMap.removeLeadingCommentsFromNode(node);
break;
case trailing:
fCommentMap.removeTrailingCommentsFromNode(node);
break;
case freestanding:
fCommentMap.removeFreestandingCommentsFromNode(node);
break;
}
}
/**
* Removes all comments of a node.
*
* @param node the node.
* @since 6.5
*/
public void removeAllComments(IASTNode node) {
fCommentMap.removeAllComments(node);
}
/**
* Returns comments for the given node.
*

View file

@ -12,6 +12,7 @@
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -205,4 +206,60 @@ public class NodeCommentMap {
public boolean isASTCovered(IASTTranslationUnit ast) {
return coveredUnits.contains(ast);
}
/**
* Removes a given <code>comment</code> from a node, regardless of its position.
*
* @param node The key to remove the <code>comment</code> from.
* @param comment The comment is the value to be removed.
*/
public void removeCommentFromNode(IASTNode node, IASTComment comment) {
List<IASTComment> leadingComments = getLeadingCommentsForNode(node);
leadingComments.removeAll(Collections.singleton(comment));
List<IASTComment> trailingComments = getTrailingCommentsForNode(node);
trailingComments.removeAll(Collections.singleton(comment));
List<IASTComment> freestandingComments = getFreestandingCommentsForNode(node);
freestandingComments.removeAll(Collections.singleton(comment));
}
/**
* Removes all leading comments from a node.
*
* @param node The key to remove the leading comments from.
*/
public void removeLeadingCommentsFromNode(IASTNode node) {
List<IASTComment> leadingComments = getLeadingCommentsForNode(node);
leadingComments.clear();
}
/**
* Removes all trailing comments from a node.
*
* @param node The key to remove the trailing comments from.
*/
public void removeTrailingCommentsFromNode(IASTNode node) {
List<IASTComment> trailingComments = getTrailingCommentsForNode(node);
trailingComments.clear();
}
/**
* Removes all freestanding comments from a node.
*
* @param node The key to remove the freestanding comments from.
*/
public void removeFreestandingCommentsFromNode(IASTNode node) {
List<IASTComment> freestandingComments = getFreestandingCommentsForNode(node);
freestandingComments.clear();
}
/**
* Removes all comments from a node.
*
* @param node The key to remove all comments from.
*/
public void removeAllComments(IASTNode node) {
removeLeadingCommentsFromNode(node);
removeTrailingCommentsFromNode(node);
removeFreestandingCommentsFromNode(node);
}
}