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

java format on type not working #3396

Closed
AvinashChowdary opened this issue Nov 21, 2023 · 2 comments · Fixed by eclipse-jdtls/eclipse.jdt.ls#3142
Closed

java format on type not working #3396

AvinashChowdary opened this issue Nov 21, 2023 · 2 comments · Fixed by eclipse-jdtls/eclipse.jdt.ls#3142
Assignees
Milestone

Comments

@AvinashChowdary
Copy link

AvinashChowdary commented Nov 21, 2023

[formatOnType setting is not working, formatOnSave works]

Environment
  • Operating System: Mac os Ventura 13.6
  • JDK version: 17
  • Visual Studio Code version: 1.84.2
  • Java extension version: 1.24.0
Steps To Reproduce
  1. Enable format on type "editor.formatOnType": true,
  2. Create a line break in any java statement, cursor beings at the first index of line instead of 2 tabs indentation.

[Please attach a sample project reproducing the error]
Please attach logs

Current Result

Line break returns to beginning of the line

public String print() {
    return String.format("Value of a: {} and b:{}", 
    a, b);
}
Expected Result

Line break should start after 2 tabs

public String print() {
    return String.format("Value of a: {} and b:{}", 
            a, b);
}
Additional Informations

Attached a video where the formatting triggers when the file is saved but it should also work when creating a line break while typing.

vscode_cannot_format_java.mov
@rgrunber
Copy link
Member

rgrunber commented Dec 4, 2023

I don't think we currently do anything with the setting editor.formatOnType. It does come from VS Code so I agree that we should probably have our functionality check that setting. We currently have java.f\\ormat.onType.enabled (should be enabled by default) but according to it's description, it's mainly meant for the formatting of semicolon and curly braces. It does mention carriage return as well, but I'm not really finding a case where that does anything interesting.

You're right that if you call the formatter with default settings, after creating a line break in a statement, the formatting gets corrected, so it definitely looks like something is missing.

Update: I think it's related to how we establish the region to be formatted when particular trigger characters are typed. Maybe we're just not handling the next line when a newline is inserted. I'll have a closer look soon.

@rgrunber
Copy link
Member

rgrunber commented May 8, 2024

For better support, I think we need to look at adopting https://github.com/eclipse-jdt/eclipse.jdt.ui/blob/fe10342ac9a664fb8438fb203eb76cce6ab97e99/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/JavaAutoIndentStrategy.java . Would be worth looking at how eclipse-jdtls/eclipse.jdt.ls#2710 was done (which is based on a different class but maybe similar API)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

3 participants