kezhuw commented on code in PR #2206: URL: https://github.com/apache/zookeeper/pull/2206#discussion_r1860576159
########## 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`. ########## zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/rcc.jj: ########## @@ -274,22 +261,37 @@ JRecord Record() : ArrayList<JField> flist = new ArrayList<JField>(); Token t; JField f; + // Get the comments on the class token + Token recordTkn; + Token rbraceTkn; } { - <RECORD_TKN> + recordTkn = <RECORD_TKN> t = <IDENT_TKN> { rname = t.image; } <LBRACE_TKN> ( f = Field() Review Comment: I think [`getToken(1)`](https://www.cs.purdue.edu/homes/hosking/javacc/doc/apiroutines.html#getToken) could help you eliminating most intrusive code. ```jj { typeToken = getToken(1) } ``` Then most changes should resides in `Record()`. ########## zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java: ########## @@ -767,4 +793,88 @@ 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) { Review Comment: Sorry for the confusion! I means it should be better to rename variable `space` to `indent`. -- 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