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

bug: Sniper printer drops whitespace after comment if next element is type member with modified modifier list #3697

Closed
slarse opened this issue Nov 16, 2020 · 7 comments · Fixed by #3701

Comments

@slarse
Copy link
Collaborator

slarse commented Nov 16, 2020

I've encountered a problem where the sniper printer will not insert newlines correctly on modified or new elements. I've encountered this when inserting import statements, as well as when modifying fields. This is a big problem for the fields, because inline comments can cause a modified field to become commented out. For example, if we have this class:

public class PrinterNewlines {
    // if a newline is not inserted on this line, the field below will be commented out
    public static int NON_FINAL_FIELD = 42;
}

And insert a modifier on the field (say final), this is the output:

public class PrinterNewlines {
    // if a newline is not inserted on this line, the field below will be commented outpublic static final int NON_FINAL_FIELD = 42;
}

I'm working on figuring out why this happens.

@monperrus
Copy link
Collaborator

Thanks for the bug report, looking forward to the fix!

@slarse
Copy link
Collaborator Author

slarse commented Nov 16, 2020

Thanks for the bug report, looking forward to the fix!

So am I, this issue is messing with Sorald's patches :)

I've located the problem with the particular case I show in the opening post here, and it's a corner case of corner cases, in particular when there's a comment preceding a modified CollectionSourceFragment (e.g. modifiers). The problem of added import statements not getting newlines is related, but it's not exactly the same.

Overall, the logic for when or when not to print newlines is a bit complicated, and gets really involved when elements are modified, added or deleted. I'm working on a fix, but it's slow going as I still don't fully understand the sniper printer.

@monperrus
Copy link
Collaborator

monperrus commented Nov 16, 2020 via email

@slarse slarse changed the title bug: Sniper printer sometimes does not print newlines correctly in front of modified or new elements bug: Sniper printer drops newline in comment if next element is modified Nov 16, 2020
@slarse slarse changed the title bug: Sniper printer drops newline in comment if next element is modified bug: Sniper printer drops newline after comment if next element is modified Nov 16, 2020
@slarse
Copy link
Collaborator Author

slarse commented Nov 16, 2020

I've determined that the import statement newlines is a separate issue, see #3698

@slarse
Copy link
Collaborator Author

slarse commented Nov 16, 2020

Alright, after a ton of debugging, I've found that the problem here is as follows:

When there is a comment just preceding a type member (method, field, inner type), and the type member's modifiers are altered, things break down. The typical thing that happens is that the whitespace (newline + indentation) between the comment and the type member simply isn't printed. I've however also managed to get the comment to simply disappear.

There are also problems with modifiers on local variables. For example, adding the final keyword to a local variable that was preceded by a comment gave me this funky output:

final
// comment here
int a = 2

The problems seem to be very much related to modifier lists, CollectionSourceFragment, and comments/whitespace (which are treated similarly by the sniper printer). I've found many fixes that have worked, but resulted in redundant whitespace elsewhere. I'll keep working on this, and will open a PR tomorrow with my test cases.

@slarse slarse changed the title bug: Sniper printer drops newline after comment if next element is modified bug: Sniper printer drops newline after comment if next element is type member with modified modifier list Nov 16, 2020
@slarse slarse changed the title bug: Sniper printer drops newline after comment if next element is type member with modified modifier list bug: Sniper printer drops whitespace after comment if next element is type member with modified modifier list Nov 16, 2020
@slarse
Copy link
Collaborator Author

slarse commented Nov 17, 2020

The comment disappears entirely whenever an initially non-empty modifier list is emptied.

@slarse
Copy link
Collaborator Author

slarse commented Dec 17, 2020

The comment disappears entirely whenever an initially non-empty modifier list is emptied.

This is an entirely separate issue.

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 a pull request may close this issue.

2 participants