kezhuw commented on code in PR #2206:
URL: https://github.com/apache/zookeeper/pull/2206#discussion_r1857741746


##########
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) {
+        if (jField == null || jField.getTypeToken() == null || 
jField.getNextToken() == null) {
+            return "";
+        }
+
+        // get the comment before the line
+        List<String> comments = 
getComments(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(getComments(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("", getComments(getRecordToken().specialToken));
+    }
+
+    private static String formatComments(String space, List<String> 
commentList) {
+        if (commentList == null || commentList.isEmpty()) {
+            return "";
+        }
+
+        String comments = String.join("", commentList);
+
+        StringBuilder formatBuilder = new StringBuilder();
+        for (String s : comments.split("\r?\n")) {
+            formatBuilder.append("\n")
+                    .append(space)
+                    .append(s.replaceAll("^[ \t]+", ""));
+        }
+
+        return formatBuilder.append("\n").toString();
+    }
+
+    private static List<String> getComments(Token token) {

Review Comment:
   > Or how about <MULTI_LINE_COMMENT: "/*" (~["*"])* "*" (~["*","/"] (~["*"])* 
"*" | "*")* "/"> ? 
(https://stackoverflow.com/questions/34550579/multiline-comments-in-javacc)
   
   > I think it is easy to reconstruct original format if multi-line comment is 
captured into one token.
   
   I am not sure the `LOOKAHEAD` could help here. I tried but did not succeed.



-- 
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

Reply via email to