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

[formatter] IndexOutOfBoundsException in applying formatter to code having errors #1437

Open
sagarrohat opened this issue Sep 26, 2023 · 1 comment

Comments

@sagarrohat
Copy link

Relates to:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=471825
vi-eclipse/Eclipse-JDT#10

How to reproduce the bug:

The code given in this comment: https://bugs.eclipse.org/bugs/show_bug.cgi?id=471825#c11

or

package testproject;

public class Application extends B {
protected int f = 11;

As far I can understand, it fails because we ignore errors if the kind is K_COMPILATION_UNIT: https://github.com/eclipse-jdt/eclipse.jdt.core/blob/master/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java#L317
and it proceeds to find the closing bracket.

For me, if a code has errors then we should not format it at all but I can't find any comment on why ignoreErrors was set to true for K_COMPILATION_UNIT in the original code which was introduced in this commit: b0e7530

One possible fix could be to not ignore errors: #1434

But it fails tests and before fixing those tests I would like to confirm this is the expected behavior.

@mateusz-matela
Copy link
Member

mateusz-matela commented Dec 30, 2023

The idea was that the formatter would only skip AST nodes marked as malformed (see preVisit2() in all of the formatter's ASTVisitors). This in theory should be enough to avoid this kind of problems, while letting us reformat at least fragments that are syntactically correct.
So maybe a good solution with this example would be if the parser/compiler marked the whole TypeDeclaration as malformed? I've already suggested that in https://bugs.eclipse.org/bugs/show_bug.cgi?id=471825#c38

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

No branches or pull requests

2 participants