/** * Add the specified comment string to the list of preceding comments. */ public void addPrecedingComment(String s) { addPrecedingComment(Comment.create(s)); }
public static int commentEnd(CommentSet comments, CommentSet.RelativePosition pos) { List<Comment> list = comments.getComments(pos); if (list.isEmpty()) { return -1; } else { return list.get(list.size() - 1).endPos(); } }
private void addCommentsToContext(DiffContext context) { Map<Integer, Comment> m = new HashMap<>(usedComments.size()); for (Comment c : usedComments) { m.put(c.pos(), c); } context.usedComments = m; }
public void printComment(Comment comment, boolean preceding, boolean printWhitespace, boolean preventClosingWhitespace) { boolean onlyWhitespaces = out.isWhitespaceLine(); if (Comment.Style.WHITESPACE == comment.style()) { if (false && printWhitespace) { char[] data = comment.getText().toCharArray(); String body = comment.getText(); boolean rawBody = body.length() == 0 || body.charAt(0) != '/'; LinkedList<CommentLine> lines = new LinkedList<CommentLine>(); if (comment.pos() > 0 && comment.endPos() < diffContext.origText.length() && diffContext.origText.substring(comment.pos() - 1, comment.endPos()).contentEquals("\n" + comment.getText())) { if (out.lastBlankLines == 0 && !preceding) newline(); out.toLineStart(); } else if (comment.indent() == 0) { if (!preceding && out.lastBlankLines == 0 && comment.style() != Style.LINE) newline(); out.toLineStart(); } else if (comment.indent() > 0 && !preceding) { if (out.lastBlankLines == 0 && comment.style() != Style.LINE) newline(); toLeftMargin(); } else if (comment.indent() < 0 && !preceding) { if (out.lastBlankLines == 0) newline(); switch (comment.style()) { case LINE: print("// ");
private static void moveCommentsBeforeOffset(WorkingCopy wc, Tree from, Tree to, int offset, Document doc) { List<Comment> toMove = new LinkedList<>(); int idx = 0; for (Comment comment : wc.getTreeUtilities().getComments(from, true)) { if (comment.pos() >= offset || comment.endPos() > offset) { break; } DocumentGuards guards = LineDocumentUtils.as(doc, DocumentGuards.class); if (guards != null) { int epAfterBlock = guards.adjustPosition(comment.pos(), true); // comment that ends exactly at the GB boundary cannot be really // reassigned from the previous member. if (epAfterBlock >= comment.endPos()) { // set new offset, after the guarded block break; } } toMove.add(comment); idx++; } if (toMove.size() > 0) { doMoveComments(wc, from, to, offset, toMove, 0, idx); } }
private int getPosAfterCommentEnd(Tree t, int minPos) { CommentSet cs = getCommentsForTree(t, false); List<Comment> cmm = cs.getComments(CommentSet.RelativePosition.TRAILING); if (cmm.isEmpty()) { cmm = cs.getComments(CommentSet.RelativePosition.INLINE); } if (cmm.isEmpty()) { return minPos; } Comment c = cmm.get(cmm.size() - 1); int pos = c.endPos(); assert pos >= 0; if (c.style() == Comment.Style.LINE || c.style() == Comment.Style.WHITESPACE) { // compensate trailing newline to preserve line ends for line comment and whitespace if (pos > 0 && diffContext.origText.charAt(pos - 1) == '\n') { pos--; } } return Math.max(minPos, pos); }
@SuppressWarnings({"MethodWithMultipleLoops"}) public String toString() { StringBuffer sb = new StringBuffer(); sb.append('{'); boolean first = true; for (Map.Entry<RelativePosition, List<Comment>> entry : commentsMap.entrySet()) { if (!first) { sb.append(", "); first = false; } sb.append("[").append(entry.getKey()).append(" -> "); for (Comment comment : entry.getValue()) { sb.append(',').append(comment.getText()); } sb.append("]"); } sb.append('}'); return sb.toString(); }
/** * Define a comment, using source file positions. * @param style the style of comment * @param pos start position within source file * @param endPos end position within source file * @param indent indentation of comment * @param text textual content of comment. With or without proper escaping * @return new comment */ public static Comment create(Style style, int pos, int endPos, int indent, String text) { return new Comment(style, pos, endPos, indent, text); }
private boolean listContains(List<Comment>list, Comment comment) { for (Comment c : list) if (c.equals(comment)) return true; return false; }
Comment javadoc = null; for (Comment comment : oldList) { if(comment.style() == Style.JAVADOC) { javadoc = comment; int nextTarget = Math.max(localPointer, oldC.endPos()); if (commentsMatch(oldC, newC)) { if(preceding && oldC == javadoc && oldDoc != null) { localPointer = diffDocTree((DCDocComment)oldDoc, (DCTree)oldDoc, (DCTree)newDoc, new int[]{localPointer, oldC.endPos()}); copyTo(localPointer, localPointer = oldC.pos()); printer.printComment(newC, !trailing, false, true); oldC = safeNext(oldIter); printer.print(newC.getText()); newC = safeNext(newIter); firstNewCommentPrinted = true; if (oldC.style() == Style.WHITESPACE) { localPointer = cStart; } else if (first && doNotDeleteIfMissing) { } else { first = false; localPointer = Math.max(localPointer, oldC.endPos()); if (Style.WHITESPACE != newC.style()) {
protected int diffInnerComments(JCTree oldT, JCTree newT, int localPointer) { if (innerCommentsProcessed) { return localPointer; } innerCommentsProcessed = true; CommentSet cs = getCommentsForTree(newT, true); CommentSet old = getCommentsForTree(oldT, true); List<Comment> oldPrecedingComments = cs == old ? ((CommentSetImpl)cs).getOrigComments(CommentSet.RelativePosition.INNER) : old.getComments(CommentSet.RelativePosition.INNER); List<Comment> newPrecedingComments = cs.getComments(CommentSet.RelativePosition.INNER); if (sameComments(oldPrecedingComments, newPrecedingComments)) { if (oldPrecedingComments.isEmpty()) { return localPointer; } // WHITESPACE comments have pos == -1, and no real useful data Comment c = oldPrecedingComments.get(oldPrecedingComments.size() - 1); if (c.pos() == -1) { return localPointer; } int newP = c.endPos(); copyTo(localPointer, newP); return newP; } return diffCommentLists(getOldPos(oldT), oldPrecedingComments, newPrecedingComments, null, null, true, false, true, false, localPointer); }
/** * Returns the preceding immediate comment */ static String getPrecedingImmediateCommentText(CompilationInfo cinfo, Tree tree) { List<Comment> comments = cinfo.getTreeUtilities().getComments(tree, true); return comments.size() > 0 ? comments.get(comments.size()-1).getText() : null; }
/** * Define a new block comment from a string. This comment does not * have source file positions. * @param s textual content of comment. With or without proper escaping * @return new comment */ public static Comment create(String s) { return new Comment(Style.BLOCK, NOPOS, NOPOS, NOPOS, s); }
private boolean commentsMatch(Comment oldC, Comment newC) { if (oldC == null && newC == null) return true; if (oldC == null || newC == null) return false; return oldC.equals(newC); }
/** * Add the specified comment string to the list of trailing comments. */ public void addTrailingComment(String s) { addTrailingComment(Comment.create(s)); }
public int pos(RelativePosition position) { List<Comment> list = getComments(position); return list.isEmpty() ? NOPOS : list.get(0).pos(); }
private int adjustLocalPointer(int localPointer, CommentSet cs, CommentSet.RelativePosition position) { if (cs == null) return localPointer; List<Comment> cl = cs.getComments(position); if (!cl.isEmpty()) { for (Comment comment : cl) { localPointer = Math.max(comment.endPos(), localPointer); } } return localPointer; }