diff options
Diffstat (limited to 'core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler')
5 files changed, 100 insertions, 96 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 25620836f9d..d6defc940b4 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 @@ -13,6 +13,7 @@ package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler; import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.TreeMap; import org.eclipse.cdt.core.dom.ast.ASTVisitor; @@ -171,26 +172,25 @@ public class ASTCommenter { * @return NodeCommentMap */ public static NodeCommentMap getCommentedNodeMap(IASTTranslationUnit transUnit){ - if (transUnit== null) { + if (transUnit == null) { return new NodeCommentMap(); } - ArrayList<IASTComment> comments = removeNotNeededComments(transUnit); - if (comments == null || comments.size() == 0) { + List<IASTComment> comments = removeNotNeededComments(transUnit); + if (comments == null || comments.isEmpty()) { return new NodeCommentMap(); } return addCommentsToCommentMap(transUnit, comments); } - private static ArrayList<IASTComment> removeNotNeededComments(IASTTranslationUnit transUnit) { - ArrayList<IASTComment> comments = getCommentsInWorkspace(transUnit); - if (comments == null || comments.size() == 0) { + private static List<IASTComment> removeNotNeededComments(IASTTranslationUnit transUnit) { + List<IASTComment> comments = getCommentsInWorkspace(transUnit); + if (comments == null || comments.isEmpty()) { return null; } - ArrayList<IASTComment> com = removeAllPreprocessorComments(transUnit, comments); - return com; + return removeAllPreprocessorComments(transUnit, comments); } - private static ArrayList<IASTComment> getCommentsInWorkspace(IASTTranslationUnit tu) { + private static List<IASTComment> getCommentsInWorkspace(IASTTranslationUnit tu) { IASTComment[] comments = tu.getComments(); ArrayList<IASTComment> commentsInWorkspace = new ArrayList<IASTComment>(); @@ -206,8 +206,8 @@ public class ASTCommenter { return commentsInWorkspace; } - private static ArrayList<IASTComment> removeAllPreprocessorComments(IASTTranslationUnit tu, - ArrayList<IASTComment> comments) { + private static List<IASTComment> removeAllPreprocessorComments(IASTTranslationUnit tu, + List<IASTComment> comments) { IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements(); TreeMap<Integer, String> treeOfPreProcessorLines = new TreeMap<Integer,String>(); TreeMap<String, ArrayList<Integer>> ppOffsetForFiles = new TreeMap<String, ArrayList<Integer>>(); @@ -286,7 +286,7 @@ public class ASTCommenter { } private static NodeCommentMap addCommentsToCommentMap(IASTTranslationUnit rootNode, - ArrayList<IASTComment> comments){ + List<IASTComment> comments){ NodeCommentMap commentMap = new NodeCommentMap(); CommentHandler commHandler = new CommentHandler(comments); @@ -296,8 +296,8 @@ public class ASTCommenter { ASTCommenterVisitor commenter = new ASTCommenterVisitor(commHandler, commentMap); declarations[i].accept(commenter); - //add remaining comments to the last declaration => Comments won't get lost - if (i + 1 == declarations.length) { + // Add the remaining comments to the last declaration to prevent comment loss. + if (i == declarations.length - 1) { commenter.addRemainingComments(declarations[i]); } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenterVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenterVisitor.java index 6d7b1e26776..9fbd6bbf19f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenterVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenterVisitor.java @@ -45,17 +45,18 @@ public class ASTCommenterVisitor extends ASTVisitor { private NodeCommenter nodeCommenter; { - shouldVisitExpressions = true; - shouldVisitStatements = true; - shouldVisitNames = true; + shouldVisitBaseSpecifiers = true; shouldVisitDeclarations = true; - shouldVisitDeclSpecifiers = true; shouldVisitDeclarators = true; + shouldVisitDeclSpecifiers = true; + shouldVisitExpressions = true; shouldVisitInitializers = true; - shouldVisitBaseSpecifiers = true; + shouldVisitNames = true; shouldVisitNamespaces = true; - shouldVisitTemplateParameters = true; shouldVisitParameterDeclarations = true; + shouldVisitStatements = true; + shouldVisitTemplateParameters = true; + shouldVisitTypeIds = true; } public ASTCommenterVisitor(CommentHandler commHandler, NodeCommentMap commentMap) { @@ -93,6 +94,11 @@ public class ASTCommenterVisitor extends ASTVisitor { } @Override + public int visit(IASTTypeId typeId) { + return nodeCommenter.appendComments((ASTNode) typeId); + } + + @Override public int visit(IASTDeclaration declaration) { return nodeCommenter.appendComments((ASTNode) declaration); } 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 9299f42963b..9ef98352418 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 @@ -7,11 +7,11 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Institute for Software - initial API and implementation + * Institute for Software - initial API and implementation *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler; -import java.util.ArrayList; +import java.util.List; import org.eclipse.cdt.core.dom.ast.IASTComment; @@ -23,10 +23,9 @@ import org.eclipse.cdt.core.dom.ast.IASTComment; * @author Guido Zgraggen IFS */ public class CommentHandler { - - private final ArrayList<IASTComment> comments; + private final List<IASTComment> comments; - public CommentHandler(ArrayList<IASTComment> comments) { + public CommentHandler(List<IASTComment> comments) { super(); this.comments = comments; } @@ -36,7 +35,7 @@ public class CommentHandler { } public boolean hasMore() { - return comments.size()>0; + return !comments.isEmpty(); } public IASTComment getFirst() { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommentMap.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommentMap.java index 4f65d336710..829c2ddd4e2 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommentMap.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommentMap.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -7,12 +7,14 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Institute for Software - initial API and implementation + * Institute for Software - initial API and implementation ******************************************************************************/ package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.eclipse.cdt.core.dom.ast.IASTComment; import org.eclipse.cdt.core.dom.ast.IASTNode; @@ -25,18 +27,18 @@ import org.eclipse.cdt.core.dom.ast.IASTNode; * @author Guido Zgraggen IFS */ public class NodeCommentMap { - protected final HashMap<IASTNode, ArrayList<IASTComment>> leadingMap = new HashMap<IASTNode, ArrayList<IASTComment>>(); - protected final HashMap<IASTNode, ArrayList<IASTComment>> trailingMap = new HashMap<IASTNode, ArrayList<IASTComment>>(); - protected final HashMap<IASTNode, ArrayList<IASTComment>> freestandingMap = new HashMap<IASTNode, ArrayList<IASTComment>>(); + protected final Map<IASTNode, List<IASTComment>> leadingMap = new HashMap<IASTNode, List<IASTComment>>(); + protected final Map<IASTNode, List<IASTComment>> trailingMap = new HashMap<IASTNode, List<IASTComment>>(); + protected final Map<IASTNode, List<IASTComment>> freestandingMap = new HashMap<IASTNode, List<IASTComment>>(); /** * Add a comment to the map with the trailing comments. * @param node The node is the key. * @param comment The comment is the value */ - public void addTrailingCommentToNode(IASTNode node, IASTComment comment){ - ArrayList<IASTComment> comments = trailingMap.get(node); - if(comments == null){ + public void addTrailingCommentToNode(IASTNode node, IASTComment comment) { + List<IASTComment> comments = trailingMap.get(node); + if (comments == null) { comments = new ArrayList<IASTComment>(); } comments.add(comment); @@ -44,17 +46,15 @@ public class NodeCommentMap { } /** - * Returns an ArrayList for the given node. This ArrayList contains all the comments + * Returns a List for the given node. This List contains all the comments * which are assigned to this specific node. If no comments are available an empty - * ArrayList is returned. + * List is returned. * @param node The key to fetch the associated comments. - * @return ArrayList + * @return List */ - public ArrayList<IASTComment> getTrailingCommentsForNode(IASTNode node){ - if(trailingMap.get(node) == null) { - return new ArrayList<IASTComment>(); - } - return trailingMap.get(node); + public List<IASTComment> getTrailingCommentsForNode(IASTNode node) { + List<IASTComment> list = trailingMap.get(node); + return list != null ? list : new ArrayList<IASTComment>(); } /** @@ -62,9 +62,9 @@ public class NodeCommentMap { * @param node The node is the key. * @param comment The comment is the value */ - public void addLeadingCommentToNode(IASTNode node, IASTComment comment){ - ArrayList<IASTComment> comments = leadingMap.get(node); - if(comments == null){ + public void addLeadingCommentToNode(IASTNode node, IASTComment comment) { + List<IASTComment> comments = leadingMap.get(node); + if (comments == null) { comments = new ArrayList<IASTComment>(); } comments.add(comment); @@ -72,17 +72,15 @@ public class NodeCommentMap { } /** - * Returns an ArrayList for the given node. This ArrayList contains all the comments + * Returns a List for the given node. This List contains all the comments * which are assigned to this specific node. If no comments are available an empty - * ArrayList is returned. + * List is returned. * @param node The key to fetch the associated comments. - * @return ArrayList + * @return List */ - public ArrayList<IASTComment> getLeadingCommentsForNode(IASTNode node){ - if(leadingMap.get(node) == null) { - return new ArrayList<IASTComment>(); - } - return leadingMap.get(node); + public List<IASTComment> getLeadingCommentsForNode(IASTNode node) { + List<IASTComment> list = leadingMap.get(node); + return list != null ? list : new ArrayList<IASTComment>(); } /** @@ -90,9 +88,9 @@ public class NodeCommentMap { * @param node The node is the key. * @param comment The comment is the value */ - public void addFreestandingCommentToNode(IASTNode node, IASTComment comment){ - ArrayList<IASTComment> comments = freestandingMap.get(node); - if(comments == null){ + public void addFreestandingCommentToNode(IASTNode node, IASTComment comment) { + List<IASTComment> comments = freestandingMap.get(node); + if (comments == null) { comments = new ArrayList<IASTComment>(); } comments.add(comment); @@ -100,51 +98,50 @@ public class NodeCommentMap { } /** - * Returns an ArrayList for the given node. This ArrayList contains all the comments + * Returns a List for the given node. This List contains all the comments * which are assigned to this specific node. If no comments are available an empty - * ArrayList is returned. + * List is returned. * @param node The key to fetch the associated comments. - * @return ArrayList + * @return List */ - public ArrayList<IASTComment> getFreestandingCommentsForNode(IASTNode node){ - if(freestandingMap.get(node) == null) { - return new ArrayList<IASTComment>(); - } - return freestandingMap.get(node); + public List<IASTComment> getFreestandingCommentsForNode(IASTNode node) { + List<IASTComment> list = freestandingMap.get(node); + return list != null ? list : new ArrayList<IASTComment>(); } - - + /** - * Returns the HashMap with all leading maps. Used only for test purpose - * @return HashMap of all leading comments + * Returns the Map with all leading maps. Used only for test purpose + * @return Map of all leading comments */ - public HashMap<IASTNode, ArrayList<IASTComment>> getLeadingMap() { + public Map<IASTNode, List<IASTComment>> getLeadingMap() { return leadingMap; } + /** - * Returns the HashMap with all trailing maps. Used only for test purpose - * @return HashMap of all trailing comments + * Returns the Map with all trailing maps. Used only for test purpose + * @return Map of all trailing comments */ - public HashMap<IASTNode, ArrayList<IASTComment>> getTrailingMap() { + public Map<IASTNode, List<IASTComment>> getTrailingMap() { return trailingMap; } + /** - * Returns the HashMap with all freestanding maps. Used only for test purpose - * @return HashMap of all freestanding comments + * Returns the Map with all freestanding maps. Used only for test purpose + * @return Map of all freestanding comments */ - public HashMap<IASTNode, ArrayList<IASTComment>> getFreestandingMap() { + public Map<IASTNode, List<IASTComment>> getFreestandingMap() { return freestandingMap; } /** - * Returns an ArrayList for the given node. This ArrayList contains all the comments + * Returns an List for the given node. This List contains all the comments * which are assigned to this specific node. If no comments are available an empty - * ArrayList is returned. + * List is returned. * @param node The key to fetch the associated comments. - * @return ArrayList + * @return List */ - public ArrayList<IASTComment> getAllCommentsForNode(IASTNode node) { - ArrayList<IASTComment> comment = new ArrayList<IASTComment>(); + public List<IASTComment> getAllCommentsForNode(IASTNode node) { + List<IASTComment> comment = new ArrayList<IASTComment>(); comment.addAll(getFreestandingCommentsForNode(node)); comment.addAll(getLeadingCommentsForNode(node)); comment.addAll(getTrailingCommentsForNode(node)); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommenter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommenter.java index 5ae49e8f7dc..75430528969 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommenter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommenter.java @@ -7,7 +7,7 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Institute for Software - initial API and implementation + * Institute for Software - initial API and implementation *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler; @@ -39,25 +39,27 @@ import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; /** - * The NodeCommenter contains all the logic that is needed for the ASTCommentVisitor to assign the comments - * to the suitable node. Together with the ASTCommenterVisitor it fills all the comments with the correspondent - * node into the NodeCommentMap. + * The NodeCommenter contains all the logic that is needed for the ASTCommentVisitor to assign + * the comments to the suitable node. Together with the ASTCommenterVisitor it fills all + * the comments with the correspondent node into the NodeCommentMap. * - * Following, a little explanation of the assignment logic. It is only a loose illustration a detailed description - * would include a combined explanation of ASTCommenterVisitor and NodeCommenter.<br> + * Following, a little explanation of the assignment logic. It is only a loose illustration + * a detailed description would include a combined explanation of ASTCommenterVisitor and + * NodeCommenter.<br> * To understand the logic we define the three types of comments:<br> * leading comments - Comments before a statement, declaration, or definition.<br> * trailing comments - Comments right after the AST node on the same line.<br> * freestanding comments - Comments before a closing brace such as they occur in * namespace-, class- and method-definitions or at the end of a file.<br> * - * The first comment is fetched and the position of it is compared to the position of the actual node. If - * the position of the comment is smaller than the comment is added to the node as leading. If it is behind the node - * but on the same line it is added as trailing. If one of these possibilities match the next comment is fetched for - * the same check. If it doesn't match the same procedure is done for all the child nodes. After checking the sub nodes - * the actual node is checked again if the comment is trailing. Then there is also the possibility that this comment is - * freestanding. This is the case when the comment is not added to any child node but the position is smaller den - * the end position of the node. + * The first comment is fetched and the position of it is compared to the position of the actual + * node. If the position of the comment is smaller than the comment is added to the node as leading. + * If it is behind the node but on the same line it is added as trailing. If one of these + * possibilities match the next comment is fetched for the same check. If it doesn't match the same + * procedure is done for all the child nodes. After checking the sub nodes the actual node is + * checked again if the comment is trailing. Then there is also the possibility that this comment is + * freestanding. This is the case when the comment is not added to any child node but the position + * is smaller than the end position of the node. * * @author Guido Zgraggen IFS */ @@ -90,7 +92,7 @@ public class NodeCommenter { ASTNode com = (ASTNode) comment; if (node.getFileLocation() == null) { - //MacroExpansions have no FileLocation + // MacroExpansions have no FileLocation return false; } @@ -111,7 +113,7 @@ public class NodeCommenter { ASTNode com = (ASTNode) comment; if (node.getFileLocation() == null) { - //MacroExpansions have no Filelocation + // MacroExpansions have no FileLocation return false; } if (OffsetHelper.getNodeEndPoint(com) <= OffsetHelper.getNodeEndPoint(node)) { @@ -151,7 +153,7 @@ public class NodeCommenter { try { InputStream is = file.getContents(); - int length = OffsetHelper.getNodeOffset(com)-OffsetHelper.getNodeEndPoint(node); + int length = OffsetHelper.getNodeOffset(com) - OffsetHelper.getNodeEndPoint(node); byte[] b = new byte[length]; long count = is.skip(OffsetHelper.getEndOffsetWithoutComments(node)); |