List of usage examples for org.eclipse.jdt.core.dom CompilationUnit firstLeadingCommentIndex
public int firstLeadingCommentIndex(ASTNode node)
From source file:org.eclipse.modisco.java.discoverer.internal.io.java.CommentsManager.java
License:Open Source License
/** * Linking comments to Nodes using jdt location informations * //from w w w .ja va 2 s .c o m * @param visitor * @param cuJdtNode * @param moDiscoCuNode * @param commentsList * @return */ private List<Comment> jdtLocationSearch(final JDTVisitor visitor, final org.eclipse.jdt.core.dom.CompilationUnit cuJdtNode, final org.eclipse.gmt.modisco.java.CompilationUnit moDiscoCuNode, final List<Comment> commentsList) { List<Comment> localCommentList = new ArrayList<Comment>(commentsList); SortedMap<Integer, org.eclipse.jdt.core.dom.ASTNode> nodesMap = new TreeMap<Integer, org.eclipse.jdt.core.dom.ASTNode>( new Comparator<Integer>() { /** * We must give an order to nodes which own comments : jdt * ast nodes gives only starting and ending indexes. e.g., * for a root class, start index will be 2 and end will be * 11, for a method definition, start index will be 4 and * end will be 6. Ordering will consider first the node * whose starting index is the highest. * * But what if a node has only a trailing comment ? first * leading comment index = -1 ! * * Here is a summary of all cases available: * <table border="1"> * <tr> * <td>case</td> * <td>A</td> * <td>B</td> * <td>C</td> * <td>D</td> * </tr> * <tr> * <td>firstLeadingCommentIndex</td> * <td>-1</td> * <td>n</td> * <td>n</td> * <td>-1</td> * </tr> * <tr> * <td>lastTrailingCommentIndex</td> * <td>-1</td> * <td>-1</td> * <td>n</td> * <td>n</td> * </tr> * </tr> * </table> * * Note: for each case, we cannot gather the information of * having, or not, comments inside the element (specific to * elements which have a block ?) * <ul> * <li>case A: We have no comments before and after the * element * * <li>case B: We have comments before the element * * <li>case C: We have comments before and after the element * * <li>case D: We have comments after the element * </ul> * * Warning: a bug has been potentially found ! The start * position of a node corresponds to the extended start * position (including comments and whitespace)) but only * for elements that have a javadoc. So to bypass this bug, * we have to process comment positions in a different way. * The order of comments locating should be: comments * before, comments after, then comments inside. * <ul> * <li>for comments before, we will start from the first * comment, then shift the start position to the end of * first comment, verify that next comment starts at the new * start position (end of first comment) otherwise we have * to consider it is not a comment before. * <li>For the comments after, we will start from the last * comment which end position should be the same as the end * position of the node, then previous comment should end at * the start position of last comment otherwise it is not a * comment after. * <li>Comments inside are comments in the node that are not * before or after. * </ul> * */ public int compare(final Integer o1, final Integer o2) { return -o1.compareTo(o2); } }); for (org.eclipse.jdt.core.dom.ASTNode node : visitor.getBijectiveMap().getKeys()) { /* * we have to decide if the element has comment * * 1. firstLeadingCommentIndex != -1 * * 2. lastTrailingCommentIndex != -1 * * 3. start position of a comment in the list is included in the * range of start position and end position of the element. This * algorithm may lead to add some elements that have no comments ... */ int position = node.getStartPosition(); if (cuJdtNode.firstLeadingCommentIndex(node) != -1) { nodesMap.put(position, node); } else if (cuJdtNode.lastTrailingCommentIndex(node) != -1) { nodesMap.put(position, node); } } /* * we have now a sorted collection of nodes, using this collection we * will be able to iterate to link current node with its comments. */ for (Integer indexMap : nodesMap.keySet()) { org.eclipse.jdt.core.dom.ASTNode jdtNode = nodesMap.get(indexMap); ASTNode element = visitor.getBijectiveMap().getValue(jdtNode); if (element instanceof PendingElement) { if (((PendingElement) element).getClientNode() != null) { element = ((PendingElement) element).getClientNode(); } else { // should never happen element = visitor.getBijectiveMap().getValue(jdtNode.getParent()); } } if (element instanceof Package) { // replace it by the compilation unit element = moDiscoCuNode; } else { // we have to clear javadoc comments that have been already // linked to the element ListIterator<Comment> listIterator = element.getComments().listIterator(); // element.getComments().clear(); while (listIterator.hasNext()) { Comment comment = listIterator.next(); if (comment instanceof Javadoc) { // Javadoc javadoc = (Javadoc) comment; // XXX put back in the un-located comments list? // localCommentList.add(javadoc); listIterator.remove(); } } } if (CommentsManager.instance.debug) { System.out.println("element commented ? " + element.toString()); //$NON-NLS-1$ } List<Comment> commentsToLink = computeListOfcommentsBefore(jdtNode, cuJdtNode, visitor); addComments(commentsToLink, element, false, true, localCommentList); commentsToLink = computeListOfcommentsAfter(jdtNode, cuJdtNode, visitor); addComments(commentsToLink, element, false, false, localCommentList); } // return unlocated comment return localCommentList; }
From source file:org.eclipse.modisco.java.discoverer.internal.io.java.CommentsManager.java
License:Open Source License
/** * @param jdtNode//from ww w . j av a 2s. com * @param cuJdtNode * @return */ private List<Comment> computeListOfcommentsBefore(final org.eclipse.jdt.core.dom.ASTNode jdtNode, final CompilationUnit cuJdtNode, final JDTVisitor visitor) { List<Comment> result = new ArrayList<Comment>(); int index = cuJdtNode.firstLeadingCommentIndex(jdtNode); if (index != -1) { /* * we have to retrieve all comments which start position is less * than start position of jdt node. shall we use the extended * position and length ? */ int size = cuJdtNode.getCommentList().size(); int startPosition = cuJdtNode.getExtendedStartPosition(jdtNode); for (int i = index; i < size; i++) { org.eclipse.jdt.core.dom.ASTNode jdtComment = (org.eclipse.jdt.core.dom.ASTNode) cuJdtNode .getCommentList().get(i); int commentPosition = cuJdtNode.getExtendedStartPosition(jdtComment); if (this.debug) { System.out.println("start position = " + startPosition); //$NON-NLS-1$ System.out.println("comment position = " + commentPosition); //$NON-NLS-1$ } String whitespaces = null; if (commentPosition > startPosition) { whitespaces = visitor.getJavaContent().substring(startPosition, commentPosition).trim(); } if ((whitespaces == null) || (whitespaces.length() == 0)) { /* * shift the start position to manage further comments, to * manage whitespace (space and tabulation). */ startPosition = commentPosition + cuJdtNode.getExtendedLength(jdtComment); Comment comment = visitor.getCommentsBinding().get(jdtComment); if (comment != null) { result.add(comment); } } else { // stop iteration (we are inside or after the jdt node) i = size; } } } if (this.debug) { System.out.println("number of comments before the node = " + result.size()); //$NON-NLS-1$ } return result; }