luozongle01 commented on code in PR #2206: URL: https://github.com/apache/zookeeper/pull/2206#discussion_r1860645336
########## zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java: ########## @@ -767,4 +795,122 @@ public static String getCsharpFQName(String name) { } return fQName.toString(); } + + public String getJavaFieldComments(JField jField) { + return getFieldComments(jField, " "); + } + + public String getCFieldComments(JField jField) { + return getFieldComments(jField, " "); + } + + private String getFieldComments(JField jField, String space) { + if (jField == null || jField.getTypeToken() == null || jField.getNextToken() == null) { + return ""; + } + + // get the comment before the line + List<String> comments = extractLeadingComments(jField.getTypeToken().specialToken); + + // get the end-of-line comments of fields + // If the current field and the next field are on the same line, + // the leading field comment of the next field should be discarded + Token tmpToken = jField.getNextToken(); + while (tmpToken.specialToken != null) { + + if (jField.getTypeToken().beginLine == tmpToken.specialToken.beginLine) { + Token endLineComments = tmpToken.specialToken; + tmpToken.specialToken = null; + comments.addAll(extractLeadingComments(endLineComments)); + break; + } + + tmpToken = tmpToken.specialToken; + } + + return formatComments(space, comments); + } + + public String getRecordComments() { + if (getRecordToken() == null || getRecordToken().specialToken == null) { + return ""; + } + + // get the comments before the class + return formatComments("", extractLeadingComments(getRecordToken().specialToken)); + } + + private static String formatComments(String indent, List<String> commentLines) { + if (commentLines == null || commentLines.isEmpty()) { + return ""; + } + + StringBuilder builder = new StringBuilder(); + for (String line : commentLines) { + if (!line.isEmpty()) { + builder.append(indent).append(line); + } + builder.append(System.lineSeparator()); + } + + return builder.toString(); + } + + /** + * Extracts leading comments with indentation and line separator trimmed. + * + * <p>Empty line is represented as empty string. + */ + private static List<String> extractLeadingComments(Token token) { + List<String> comments = new ArrayList<>(); + + if (token == null) { + return comments; + } + + Token tmpToken = token; + while (tmpToken.specialToken != null) { + tmpToken = tmpToken.specialToken; + } Review Comment: > Add a method `getCommentToken` ? There is a similar loop in `getFieldComments`. Okay, I'll make some changes here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@zookeeper.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org