Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Schorn2008-04-21 10:44:12 -0400
committerMarkus Schorn2008-04-21 10:44:12 -0400
commite4ecf4013653bfc63342a06e6bbd48222c94a3a2 (patch)
tree20b15cb6a710702174581bd7a3449b447f726470 /core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler
parent7686c3bdbc1b254f24c078aee912f79626684aad (diff)
downloadorg.eclipse.cdt-e4ecf4013653bfc63342a06e6bbd48222c94a3a2.tar.gz
org.eclipse.cdt-e4ecf4013653bfc63342a06e6bbd48222c94a3a2.tar.xz
org.eclipse.cdt-e4ecf4013653bfc63342a06e6bbd48222c94a3a2.zip
Comment handling for refactoring by Emanuel Graf, bug 228009.
Diffstat (limited to 'core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler')
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java45
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/CommentHandler.java8
2 files changed, 43 insertions, 10 deletions
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java
index 03d06574d3..b3f17609b5 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java
@@ -11,18 +11,21 @@
******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
-import java.util.Vector;
+import java.util.ArrayList;
+import java.util.TreeMap;
import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTNode;
+import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
+import org.eclipse.cdt.internal.core.dom.rewrite.util.OffsetHelper;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
/**
- * This is the startpoint of the whole comment handling process. The creation of the
+ * This is the starting point of the entire comment handling process. The creation of the
* NodeCommentMap is based on the IASTTranslationUnit. From this TranslationUnit the comments
* are extracted and skipped if they belong not to the same workspace. An ASTCommenterVisitor
* is initialized with this collection of comments. And the visit process can start.
@@ -45,16 +48,25 @@ public class ASTCommenter {
if(transUnit== null) {
return new NodeCommentMap();
}
- Vector<IASTComment> comments = getCommentsInWorkspace(transUnit);
+ ArrayList<IASTComment> comments = removeNotNeededComments(transUnit);
if(comments == null || comments.size() == 0) {
return new NodeCommentMap();
}
return addCommentsToCommentMap(transUnit, comments);
}
- private static Vector<IASTComment> getCommentsInWorkspace(IASTTranslationUnit tu) {
+ private static ArrayList<IASTComment> removeNotNeededComments(IASTTranslationUnit transUnit) {
+ ArrayList<IASTComment> comments = getCommentsInWorkspace(transUnit);
+ if (comments == null || comments.size() == 0) {
+ return null;
+ }
+ ArrayList<IASTComment> com = removeAllPreprocessorComments(transUnit, comments);
+ return com;
+ }
+
+ private static ArrayList<IASTComment> getCommentsInWorkspace(IASTTranslationUnit tu) {
IASTComment[] comments = tu.getComments();
- Vector<IASTComment> commentsInWorksapce = new Vector<IASTComment>();
+ ArrayList<IASTComment> commentsInWorksapce = new ArrayList<IASTComment>();
if (comments == null || comments.length == 0) {
return null;
@@ -68,6 +80,27 @@ public class ASTCommenter {
return commentsInWorksapce;
}
+ private static ArrayList<IASTComment> removeAllPreprocessorComments(IASTTranslationUnit tu, ArrayList<IASTComment> comments) {
+ IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements();
+ TreeMap<Integer,Object> treeOfPreProcessorLines = new TreeMap<Integer,Object>();
+
+ for (IASTPreprocessorStatement statement : preprocessorStatements) {
+ if (isInWorkspace(statement)) {
+ treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement),null);
+ }
+ }
+
+ ArrayList<IASTComment> commentsInCode = new ArrayList<IASTComment>();
+ for (IASTComment comment : comments) {
+ int comStartLineNumber = OffsetHelper.getStartingLineNumber(comment);
+ if (treeOfPreProcessorLines.containsKey(comStartLineNumber)) {
+ continue;
+ }
+ commentsInCode.add(comment);
+ }
+ return commentsInCode;
+ }
+
private static boolean isInWorkspace(IASTNode node) {
IPath workspacePath = Platform.getLocation();
IPath nodePath = new Path(node.getContainingFilename());
@@ -75,7 +108,7 @@ public class ASTCommenter {
}
- private static NodeCommentMap addCommentsToCommentMap(IASTTranslationUnit rootNode, Vector<IASTComment> comments){
+ private static NodeCommentMap addCommentsToCommentMap(IASTTranslationUnit rootNode, ArrayList<IASTComment> comments){
NodeCommentMap commentMap = new NodeCommentMap();
CommentHandler commHandler = new CommentHandler(comments);
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/CommentHandler.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/CommentHandler.java
index 2203d36a34..9299f42963 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/CommentHandler.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/CommentHandler.java
@@ -11,7 +11,7 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
-import java.util.Vector;
+import java.util.ArrayList;
import org.eclipse.cdt.core.dom.ast.IASTComment;
@@ -24,9 +24,9 @@ import org.eclipse.cdt.core.dom.ast.IASTComment;
*/
public class CommentHandler {
- private final Vector<IASTComment> comments;
+ private final ArrayList<IASTComment> comments;
- public CommentHandler(Vector<IASTComment> comments) {
+ public CommentHandler(ArrayList<IASTComment> comments) {
super();
this.comments = comments;
}
@@ -40,6 +40,6 @@ public class CommentHandler {
}
public IASTComment getFirst() {
- return comments.firstElement();
+ return comments.get(0);
}
}

Back to the top