From e4ecf4013653bfc63342a06e6bbd48222c94a3a2 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Mon, 21 Apr 2008 14:44:12 +0000 Subject: [PATCH] Comment handling for refactoring by Emanuel Graf, bug 228009. --- .../rewrite/commenthandler/ASTCommenter.java | 45 ++++++++++++++++--- .../commenthandler/CommentHandler.java | 8 ++-- core/org.eclipse.cdt.ui/plugin.properties | 2 +- .../refactoring/actions/messages.properties | 2 +- 4 files changed, 45 insertions(+), 12 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 03d06574d37..b3f17609b50 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 comments = getCommentsInWorkspace(transUnit); + ArrayList comments = removeNotNeededComments(transUnit); if(comments == null || comments.size() == 0) { return new NodeCommentMap(); } return addCommentsToCommentMap(transUnit, comments); } - private static Vector getCommentsInWorkspace(IASTTranslationUnit tu) { + private static ArrayList removeNotNeededComments(IASTTranslationUnit transUnit) { + ArrayList comments = getCommentsInWorkspace(transUnit); + if (comments == null || comments.size() == 0) { + return null; + } + ArrayList com = removeAllPreprocessorComments(transUnit, comments); + return com; + } + + private static ArrayList getCommentsInWorkspace(IASTTranslationUnit tu) { IASTComment[] comments = tu.getComments(); - Vector commentsInWorksapce = new Vector(); + ArrayList commentsInWorksapce = new ArrayList(); if (comments == null || comments.length == 0) { return null; @@ -68,6 +80,27 @@ public class ASTCommenter { return commentsInWorksapce; } + private static ArrayList removeAllPreprocessorComments(IASTTranslationUnit tu, ArrayList comments) { + IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements(); + TreeMap treeOfPreProcessorLines = new TreeMap(); + + for (IASTPreprocessorStatement statement : preprocessorStatements) { + if (isInWorkspace(statement)) { + treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement),null); + } + } + + ArrayList commentsInCode = new ArrayList(); + 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 comments){ + private static NodeCommentMap addCommentsToCommentMap(IASTTranslationUnit rootNode, ArrayList 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 2203d36a34a..9299f42963b 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 comments; + private final ArrayList comments; - public CommentHandler(Vector comments) { + public CommentHandler(ArrayList comments) { super(); this.comments = comments; } @@ -40,6 +40,6 @@ public class CommentHandler { } public IASTComment getFirst() { - return comments.firstElement(); + return comments.get(0); } } diff --git a/core/org.eclipse.cdt.ui/plugin.properties b/core/org.eclipse.cdt.ui/plugin.properties index 17b686626ed..26d35f88f4e 100644 --- a/core/org.eclipse.cdt.ui/plugin.properties +++ b/core/org.eclipse.cdt.ui/plugin.properties @@ -159,7 +159,7 @@ Refactoring.menu.label= Refac&tor Refactoring.renameAction.label=Re&name... Refactoring.extractConstant.label=Extr&act Constant... Refactoring.extractFunction.label=Extract &Function... (work in progress) -Refactoring.hideMethod.label=Hide Member Function... (work in progress) +Refactoring.hideMethod.label=Hide Method... Refactoring.implementMethod.label=Impl&ement Method... (work in progress) Refactoring.gettersAndSetters.label=Generate Getters and Setters... diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/messages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/messages.properties index 52d03bcbb77..5257a3fcb90 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/messages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/messages.properties @@ -13,5 +13,5 @@ CRenameAction_label=Rename... ExtractConstantAction_label=Extract Constant... GettersAndSetters_label=Generate Getters and Setters... ImplementMethodAction_label=Implement Method... (work in progress) -HideMethodAction_label=Hide Member Function... (work in progress) +HideMethodAction_label=Hide Method... ExtractFunctionAction_label=Extract Function... (work in progress)