redhat-developer / vscode-java

Java Language Support for Visual Studio Code
Eclipse Public License 2.0
2.07k stars 433 forks source link

Inconsistency in line break "{}" formatting in Java with in VS Code #3694

Open drakgoku opened 2 months ago

drakgoku commented 2 months ago

In my VSC I have the extension called "Language support for java(TM) by red hat". The typical one that all of us who do Java have and this is self-evident. The formatter of said extension does not format correctly. Surely the formatting is taken from LLVM so there is some error or even some incorrect syntax when formatting

When I format in VSC this example code with java or java spring boot:

❌Incorrect - If I format like this, it leaves it like this This happens on both the opening "{" and the ending "}". It seems that the extra space has no influence since the format accepts both together and extra spacing. When the correct one is the second one with the extra spacing. Incorrect Preview
image image image
Also say the formatting of the extension also leaves it like this if the code is like this.image

3 incorrect formats accepted by the formatter have been shown. Therefore, if there is any code "in that format", the formatter will leave it that way. Only the correct one is the last one or the one made by Eclipse. This doesn't make much sense since the code is stuck above or below or even between the curly braces "{}".

This way of formatting is not expected for those of us who work with Java. Eclipse does well in this regard.

✅ correct - This is how Eclipse does it. It doesn't matter how messy the code is. It will leave it like this: Correct Preview
image

I have tried countless things, none of them worked.

This has worked since 2007 in eclipse. We are in 2024. As an example of a good format:

image

How do you solve this problem and get the correct form, like correct or equal to eclipse?

drakgoku commented 2 months ago

It's been a week...any ideas?

snjeza commented 2 months ago

@drakgoku You can try to add

org.eclipse.jdt.core.formatter.blank_lines_after_last_class_body_declaration=1
org.eclipse.jdt.core.formatter.blank_lines_before_first_class_body_declaration=1

to /.settings/org.eclipse.jdt.core.prefs See Formatter settings

drakgoku commented 2 months ago

Very good!🤔 but I am in VSC (visual studio code) not in Eclipse..

snjeza commented 2 months ago

@drakgoku You can set it in VS Code, too

drakgoku commented 2 months ago

I still don't understand what you're trying to tell me. I have a project created with VSC, not with eclipse or similarly with spring boot init 1 - I don't have: /.settings/org.eclipse.jdt.core.prefs 2 - It should be configured in settings.json

Are you sure what you're talking about?

fbricon commented 2 months ago

vscode-java is using the Eclipse JDT.LS language server, so yes, it's definitely based on Eclipse. Please read https://github.com/redhat-developer/vscode-java/wiki/Formatter-settings

I can guarantee that, as a core Eclipse JDT.LS and vscode-java developer, @snjeza knows what she's talking about.