Skip to content

Update Reformatter to honour case wrap settings with switch expressions (GH7326).#9484

Open
neilcsmith-net wants to merge 1 commit into
apache:masterfrom
neilcsmith-net:gh7326
Open

Update Reformatter to honour case wrap settings with switch expressions (GH7326).#9484
neilcsmith-net wants to merge 1 commit into
apache:masterfrom
neilcsmith-net:gh7326

Conversation

@neilcsmith-net

Copy link
Copy Markdown
Member

Update Reformatter to only add a newline to cases in switch expressions when wrapCaseStatements is set to WRAP_ALWAYS (default).

Fixes #7326

Alternative might be to ignore the force wrap setting when we have an ARROW rather than COLON. I'm not sure the default works as well in those cases.

@neilcsmith-net neilcsmith-net requested a review from mbien July 3, 2026 09:36
@neilcsmith-net neilcsmith-net added the Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) label Jul 3, 2026
@neilcsmith-net neilcsmith-net added this to the NB31 milestone Jul 3, 2026

@mbien mbien left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even though this might be incomplete, its a good change since this makes the NEVER option usable (which is nice for arrow switches since it still wraps blocks correctly, see snippet I posted via comment).

We could consider changing the default to NEVER but this would probably require to update many tests.

Comment on lines +3441 to +3445
String choose(String str) {
return switch (str) {
Object str = "pattern matching switch";
IntStream iso = IntStream.of(1, 2);
case null -> "case with null formatting";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if intended but this code is not valid (several compiler errors, matching error and also syntax (e.g (String s) or case default)).

We had issues before where tests using invalid code started failing for the wildest reasons (example: compiler suddenly thought it was a compact-java file and generated an additional wrapping class). Unless this is intended for the purpose of the test it might be better to use valid code.

also would be good to add a case which uses curly braces+yield, e.g

    String choose(String str) {
        IntStream iso = IntStream.of(1, 2);
        return switch (str) {
            case null -> "case with null formatting";
            case String string when (string.length() == iso.filter(i -> i / 2 == 0).count() || string.length() == 0) ->
                "case with pattern matching + condition + lambda expression formatting";
            case String s when s.length() == 1 -> "case with pattern matching + condition formatting";
            case String s when s.length() == 2 -> {
                yield "case with pattern matching + condition formatting";
            }
            default -> "default formatting";
        };
    }

@neilcsmith-net neilcsmith-net Jul 4, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, partly bad copy/paste after converting to multiline. Will fix and add your example. Need to check the existing test code is valid too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add the missing t? Because I once used to think the java format[t]er had no tests since i couldn't find it :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can rename, but if we do should look at doing so in a way that navigate to test works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Formatting Issue - Wrapping for Java 21 Style Switch Cases that have Arrows

2 participants