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

Disappearing comments with LexicalPreservingPrinter #4376

Open
andrecsilva opened this issue Apr 18, 2024 · 1 comment
Open

Disappearing comments with LexicalPreservingPrinter #4376

andrecsilva opened this issue Apr 18, 2024 · 1 comment

Comments

@andrecsilva
Copy link

andrecsilva commented Apr 18, 2024

Moving statements may make their associated comments disappear from LexicalPreservingPrinter output.
An example:

    String code =
        """
    class A{
	    void f(){
		    // first comment
		    int a = 1;
		    // second comment
		    int b = 2;
		    // third comment
		    int c = 3;
	    }
    }
    """;
    CompilationUnit cu = StaticJavaParser.parse(code);
    LexicalPreservingPrinter.setup(cu);

    System.out.println(cu);
    System.out.println("----------------------------------");
    System.out.println(LexicalPreservingPrinter.print(cu));
    var block = cu.findAll(BlockStmt.class).get(0);
    var lastStmt = block.getStatements().removeLast();
    block.addStatement(lastStmt);

    System.out.println("AFTER-----------------------------");
    System.out.println(cu);
    System.out.println("----------------------------------");
    System.out.println(LexicalPreservingPrinter.print(cu));

Output:

    class A {

        void f() {
            // first comment
            int a = 1;
            // second comment
            int b = 2;
            // third comment
            int c = 3;
        }
    }

    ----------------------------------
    class A{
     void f(){
      // first comment
      int a = 1;
      // second comment
      int b = 2;
      // third comment
      int c = 3;
     }
    }

    AFTER-----------------------------
    class A {

        void f() {
            // first comment
            int a = 1;
            // second comment
            int b = 2;
            // third comment
            int c = 3;
        }
    }

    ----------------------------------
    class A{
     void f(){
      // first comment
      int a = 1;
      // second comment
      int b = 2;
      int c = 3;

     }
    }

Notice the last comment is present with the default printer, but won't be printed with the LexicalPreservingPrinter. There is also an extra empty line at the end of the block.

@andrecsilva andrecsilva changed the title Dissapearing comments with LexicalPreservingPrinter Disappearing comments with LexicalPreservingPrinter Apr 19, 2024
@jlerbsc
Copy link
Collaborator

jlerbsc commented Apr 19, 2024

It's a complicated case and I'm surprised that you're the first person to spot this issue. Unfortunately I'm not going to be able to solve it immediately. No matter how much I think about it, I can't see how to display the comment with the LexicalPreservingPrinter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants