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

reformat of record wrongly indents closing brace #7043

Open
homberghp opened this issue Feb 8, 2024 · 1 comment · May be fixed by #8101 or #8106
Open

reformat of record wrongly indents closing brace #7043

homberghp opened this issue Feb 8, 2024 · 1 comment · May be fixed by #8101 or #8106
Labels
Editor Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) kind:bug Bug report or fix

Comments

@homberghp
Copy link

homberghp commented Feb 8, 2024

Apache NetBeans version

Apache NetBeans > 12 (all that support java 14)

What happened

when you have a record, say

record Student( String id, String lastname, String firstname, LocalDate dob ) implements Serializable, Wither<Student, StudentBuilder> {

    @Override
    public StudentBuilder with() {
        return new StudentBuilder(this);
    }

}

and you press reformat, then the resulting code looks like this:

record Student( String id, String lastname, String firstname, LocalDate dob) implements Serializable, Wither<Student, StudentBuilder> {

    @Override
    public StudentBuilder with() {
        return new StudentBuilder( this );
    }

    } // indented wrongly

with the last brace at the wrong place.
This happens if I set spaces within method declaration in the
Options > editor> formatting> spaces > checkbox "method declarations", see below.
I like my code spacious so both in method declaration and calls, because I consider readability paramount.

image

Language / Project Type / NetBeans Component

java editor

How to reproduce

turn on within parenthesis> Method Declaration and reformat with the well know shortcut or menu.
Shortcut is on my machine.

image

Did this work correctly in an earlier version?

No / Don't know

Operating System

Linux Ubuntu 22.04 LTS
Windows 11 with Java 21, Netbeans 20

JDK

all since 14

Apache NetBeans packaging

Apache NetBeans binary zip

Anything else

The problem is always there and repeatably reproducible.

If I had a clue how this reformating is implemented I might be able to write a pull request.
For me the reformat tries to be too smart, and considers the code in the record before the first parenthesis as a method declaration too (which it is more or less) and becomes confused when it finds a method declaration before the opening brace of a class, in this case a record.
I would be fine to put spaces inside all pairs of <>, (), [], {}, but that might not be to everyone's taste.

Are you willing to submit a pull request?

No, missing doc on where to find the problem.

@homberghp homberghp added kind:bug Bug report or fix needs:triage Requires attention from one of the committers labels Feb 8, 2024
@mbien mbien added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) Editor and removed needs:triage Requires attention from one of the committers labels Feb 8, 2024
@homberghp
Copy link
Author

New findings after adding tests.

I also noted that the formatting of records also drops a space before the closing parenthesis in the record header.

I expected this

record Student( String id,String lastname,String firstname ) implements Serializable {
} // should stay flush to left margin

but got

record Student( String id,String lastname,String firstname) implements Serializable {
    } // should stay flush to left margin

that is, the space after firstname is missing.

homberghp added a commit to homberghp/netbeans that referenced this issue Dec 19, 2024
to show the effect of propery spaceswithinMethodDeclaration on record.
See issue apache#7043.

moved Sample spaces from bundle to text block.

To show effect of apache#7043.

added test RecordFormattingTest to show problem  \apache#7043

There are two tests in the test class, one that passws with the spaceWithinMethodDeclParens
set to false, and one that fails when this is set to true.
homberghp added a commit to homberghp/netbeans that referenced this issue Dec 27, 2024
solves missing space before closing RPAREN in record.
homberghp added a commit to homberghp/netbeans that referenced this issue Dec 27, 2024
Indetantion of the closing brace of a record was off and dependent of
preference spaceWithinMethodDeclParens.
fixed in
Java Source Base/org.netbeans.modules.java.source.save.Reformatter.
Tests added in file
org.netbeans.modules.java.source.save.RecordFormattingTest
@mbien mbien linked a pull request Dec 27, 2024 that will close this issue
homberghp added a commit to homberghp/netbeans that referenced this issue Dec 30, 2024
…ition

adds tests to show the error
adds inner and outer record in ui template text for setting java format
spaces
solves the error by moving spaces(cs.spaceWithinMethodDeclParens() ? 1 : 0, true);  to the correct place.

solves apache#7043 formatter to handle record closing braces correctly

squashed commits of test and solution.
mbien pushed a commit to mbien/netbeans that referenced this issue Jan 2, 2025
…ition

adds tests to show the error
adds inner and outer record in ui template text for setting java format
spaces
solves the error by moving spaces(cs.spaceWithinMethodDeclParens() ? 1 : 0, true);  to the correct place.

solves apache#7043 formatter to handle record closing braces correctly

squashed commits of test and solution.
mbien added a commit to mbien/netbeans that referenced this issue Jan 2, 2025
…ition

 - adds tests to show the error
 - adds inner and outer record in ui template text for setting java
   format spaces
 - solves the error by moving spaces(cs.spaceWithinMethodDeclParens() ? 1 : 0, true);  to the correct place.

Co-authored-by: Michael Bien <mbien42@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Editor Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) kind:bug Bug report or fix
Projects
None yet
2 participants