Skip to content

Don't break line when switch expression used in declaration assignment - #1789

Merged
bulldozer-bot[bot] merged 1 commit into
developfrom
pkoenig/switchAssignment
Sep 23, 2026
Merged

bulldozer-bot[bot] merged 1 commit into
developfrom
pkoenig/switchAssignment

Conversation

@pkoenig10

Copy link
Copy Markdown
Member

Fixes #1258

Before this PR

The formatter will change

int x = switch (y) {
  case 1 -> 1;
  case 2 -> 2;
  default -> 10;
};

into

int x =
        switch (y) {
          case 1 -> 1;
          case 2 -> 2;
          default -> 10;
        };

This creates unnecessary indentation that makes the code harder to read.

After this PR

The formatter will format this code like

int x = switch (y) {
  case 1 -> 1;
  case 2 -> 2;
  default -> 10;
};

@changelog-app

changelog-app Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Generate changelog in changelog/@unreleased

Type (Select exactly one)

  • Feature (Adding new functionality)
  • Improvement (Improving existing functionality)
  • Fix (Fixing an issue with existing functionality)
  • Break (Creating a new major version by breaking public APIs)
  • Deprecation (Removing functionality in a non-breaking way)
  • Migration (Automatically moving data/functionality to a new system)

Description

Don't break line when switch expression used in assignment.

Check the box to generate changelog(s)

  • Generate changelog entry

Comment on lines +3597 to +3600
builder.open(
Indent.If.make(typeBreak, plusFour, ZERO),
BreakBehaviours.breakOnlyIfInnerLevelsThenFitOnOneLine(true),
LastLevelBreakability.ABORT);

@pkoenig10 pkoenig10 Sep 23, 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.

This effectively reverses the change from #365. It's not clear to me why we introduced this case in the first place. Removing it addresses the TODO that was added here.

builder.close();
}
builder.guessToken(";");
builder.forcedBreak(minusTwo);

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.

This was added in #1069, but the test cases now pass without it, so this must have been fixed elsewhere.

@changelog-app

changelog-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

βœ… Successfully generated changelog entry!

Need to regenerate?

Simply interact with the changelog bot comment again to regenerate these entries.


πŸ“‹Changelog Preview

πŸ’‘ Improvements

  • Don't break line when switch expression used in assignment. (#1789)

@bulldozer-bot
bulldozer-bot Bot merged commit 478d4c5 into develop Sep 23, 2026
7 checks passed
@bulldozer-bot
bulldozer-bot Bot deleted the pkoenig/switchAssignment branch September 23, 2026 18:14
@crogoz

crogoz commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

thanks @pkoenig10 !

@autorelease3

autorelease3 Bot commented Sep 23, 2026

Copy link
Copy Markdown

Released 2.99.0

This creates a tag and GitHub release. Additional build or deployment steps may still be in progress.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected new line in switch assignements

2 participants