Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZOOKEEPER-4880: Generate comments from zookeeper.jute into code. #2206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

luozongle01
Copy link
Contributor

Generate comments from zookeeper.jute into code: https://issues.apache.org/jira/browse/ZOOKEEPER-4880

Brief Description

Support generating end-of-line comments and class comments in zookeeper.jute into code.

How to test this change

  1. By comparing the zookeeper-jute/target/generated-sources/java/org/apache/zookeeper and zookeeper-client/zookeeper-client-c/generated directories before and after the modification, there are no other differences except the comments.
image image

2. When the test class has multiple lines of comments, the comments can be generated normally. image

3. Testing vector type properties can generate comments normally. image

4. Testing class type properties can generate comments normally image

5. Testing comments that do not meet expectations will not generate comments and will not affect the generation of normal code. image

@luozongle01
Copy link
Contributor Author

@kezhuw Could you help me take a look? Thanks.

Copy link
Member

@kezhuw kezhuw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @luozongle01, thank you for your contribution!

Given the fact that jute and all targeting languages support both // and multi-line comments, I suggest we output whatever comments captured in origin sharp. With help of beginLine, beginColumn, endLine and endColumn of Token, I think we can reproduce comments in generated code with indentation adjusted.

while (tmpTkn != null) {
String image = tmpTkn.image.trim();
tmpTkn = tmpTkn.next;
if (image.startsWith("//")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The capture does not shaped well for directly usage. I tasted the following capture, and it captured a complete line comment. This way we can output whatever it captured without post work.

SPECIAL_TOKEN :
{
  <SINGLE_LINE_COMMENT: "//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>
}

See also https://javacc.github.io/javacc/tutorials/token-manager.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for your comment. I will make some further modifications.😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The capture does not shaped well for directly usage. I tasted the following capture, and it captured a complete line comment. This way we can output whatever it captured without post work.

SPECIAL_TOKEN :
{
  <SINGLE_LINE_COMMENT: "//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>
}

See also https://javacc.github.io/javacc/tutorials/token-manager.html

It seems like there's no need to change this now, because I get all the comments,then I use and distinguished them by line numbe. 😂😂

@luozongle01 luozongle01 force-pushed the ZOOKEEPER-4880 branch 4 times, most recently from 52d4194 to 0631b4f Compare October 29, 2024 15:48
@luozongle01
Copy link
Contributor Author

Hi, @kezhuw, I made some modifications, thank you for your review last time. I feel much better now than last time.
I hope you can help me look again.

Currently, multi-line comments and single-line comments are supported.

I put JField or JRecord into a TreeSet, so that I can get the line or column information of the previous or next element. According to the beginLine, beginColumn, endLine, endColumn information, I determine the range of my comment area, and then format the output in line number order.

Here is my testing process.

  1. Verify that the modification only affects the comments and does not affect the normal code.

  2. When verifying that each JField is on its own line, multi-line comments and single-line comments are generated normally.

image image
  1. If the attributes are on the same line, the comment should belong to the previous Field.
image

@kezhuw kezhuw self-requested a review November 6, 2024 09:33
Copy link
Member

@kezhuw kezhuw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@luozongle01 Thank you for your work!

It might be late, I am sorry for this, but I think we probably should align on how comments from jute should be formatted in generated sources ? I see two approaches.

  1. Keep it in origin format.
  2. Transfer comments from one format to another.

I saw this pr is going on the second approach. I am good to unify the generated comment format. But I think it could be challenge, as we should escape between two formats which could make generated comments strange/inaccurate.

    // /**
    //  * Some dead comments.
    //  */
    //
    // Things changed. We are going ..

Currently, the generated comments blindly strip some special strings. I don't think it is a good thing.

I suggest we keep it simple and don't touch comment content, this is also a safe path. If we want javadoc style comments, I think it is ok to convert them manually.

String[] commentArray = commentMulti.split("\n");
for (String comment : commentArray) {

comment = comment.replaceAll("//|/\\*\\*|/\\*|\\*\\*/|\\*/", "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think people will want to maintain this kind of code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/do/don't/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think people will want to maintain this kind of code.
I'll amend that and I won't touch the comments so they're not needed here.

comment = comment.replaceAll("//|/\\*\\*|/\\*|\\*\\*/|\\*/", "")
.replaceAll("^[* ]+", "").trim();

if (!comment.isEmpty()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated comments has no empty line which is not good.

@luozongle01
Copy link
Contributor Author

luozongle01 commented Nov 7, 2024

@luozongle01 Thank you for your work!

It might be late, I am sorry for this, but I think we probably should align on how comments from jute should be formatted in generated sources ? I see two approaches.

  1. Keep it in origin format.
  2. Transfer comments from one format to another.

I saw this pr is going on the second approach. I am good to unify the generated comment format. But I think it could be challenge, as we should escape between two formats which could make generated comments strange/inaccurate.

    // /**
    //  * Some dead comments.
    //  */
    //
    // Things changed. We are going ..

Currently, the generated comments blindly strip some special strings. I don't think it is a good thing.

I suggest we keep it simple and don't touch comment content, this is also a safe path. If we want javadoc style comments, I think it is ok to convert them manually.

@kezhuw Thank you very much for your review. I really didn't consider that 😂😂. I will keep the original format output.

However, for the end-of-line comments, should I continue to keep them at the end of the line or is it better to move them above the code? 🤔

@kezhuw
Copy link
Member

kezhuw commented Nov 7, 2024

for the end-of-line comments, should I continue to keep them at the end of the line or is it better to move them above the code?

I prefer to move them above the code.

@luozongle01
Copy link
Contributor Author

for the end-of-line comments, should I continue to keep them at the end of the line or is it better to move them above the code?

I prefer to move them above the code.

Thanks, I thought so too, I'll modify the code.

@luozongle01
Copy link
Contributor Author

Hi @kezhuw , I made some changes, could you take a look at it for me?

I won't touch the annotation content now.

But I still formatted it a little bit here, because for example, field needs a few spaces, class does not need spaces, and the number of spaces in java and c is also different.

  private static String getComments(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();
  }

Now the comment is like this
image
image

@kezhuw kezhuw self-requested a review November 9, 2024 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants