If you use fluent builder patterns in Java (or Kotlin), you’ve likely seen IntelliJ IDEA reformat your carefully chained method calls into a mess of indentation, or worse, collapse them into a single unreadable line. This guide shows you how to stop that formatting behavior and keep your builders readable.
What You'll Need
- IntelliJ IDEA (Community or Ultimate edition) — the steps work for recent versions (2020.1 and later).
- An open project that contains fluent builder code.
- Basic familiarity with IntelliJ’s Settings/Preferences dialog.
Step-by-Step
- Open IntelliJ’s formatting settings.
Go to File > Settings (on Windows/Linux) or IntelliJ IDEA > Preferences (on macOS). Then navigate to Editor > Code Style. - Choose your language.
In the left panel, select the language you’re using (e.g., Java or Kotlin). The formatting rules are language‑specific, so you may need to repeat the steps for each language you use. - Go to the “Wrapping and Braces” tab.
In the language’s code style settings, click on the Wrapping and Braces tab. This is where you control how chained method calls are wrapped. - Find the “Chained method calls” option.
Scroll down in the list until you see Chained method calls. It’s usually under the “Method call arguments” or “Method declarations” section, depending on the language. - Set it to “Do not wrap”.
Click the dropdown next to Chained method calls and select Do not wrap. This tells IntelliJ to keep chained calls on the same line if they fit, and not to force a new line at each dot. - Adjust the “Continuation indent” (optional).
If you still want some indentation when calls do wrap (e.g., because the line is too long), set the Continuation indent value. A common choice is 4 or 8 spaces. This won’t force wrapping, but it will control the indent if wrapping happens for other reasons. - Apply and test.
Click Apply and then OK. Now select your builder code and press Ctrl+Alt+L (Windows/Linux) or Cmd+Option+L (macOS) to reformat. The chained calls should stay on one line if they fit within the right margin. - If you still see unwanted wrapping, check the “Right margin” setting.
Sometimes the line is wrapped because it exceeds the right margin (default 120 characters). If you want longer lines, go to Editor > Code Style and increase the Right margin value (e.g., to 140 or 160).
Common Mistakes
- Changing the wrong language settings.
If you work with both Java and Kotlin, you must change the setting in each language’s code style. Changing only Java won’t affect Kotlin builders. - Selecting “Wrap always” instead of “Do not wrap”.
The dropdown has several options: “Do not wrap”, “Wrap if long”, “Wrap always”, and “Chop down if long”. Make sure you pick “Do not wrap” to completely stop forced wrapping. - Not applying the setting to the whole project.
If you have a project‑specific code style (e.g., from a team‑shared .editorconfig), your local changes may be overridden. Check if you have an .editorconfig file that setschained_method_call_wrapping. - Forgetting to click “Apply”.
It sounds trivial, but many users change the setting and then close the dialog without applying. Always click Apply before OK.
Tips
- Use a dedicated “Builder” code style profile.
If you want different formatting for builder classes, you can create a separate code style scheme (e.g., “BuilderStyle”) and switch to it when working on those files. This is more advanced but gives you granular control. - Set the “Continuation indent” to 0 if you want no extra indent.
When a chained call does wrap (for example, because the line is too long), a continuation indent of 0 will align the next call under the first dot, which often looks cleaner for builders. - Use “Reformat Code” with “Optimize imports” unchecked.
Sometimes reformatting also reorders imports, which can be distracting. You can uncheck “Optimize imports” in the reformat dialog (Ctrl+Alt+L) to only change formatting. - Consider using “Save Actions” plugin.
If you want formatting to be consistent across your team, you can configure the “Save Actions” plugin to run a specific code style on save, but make sure everyone uses the same settings.
Troubleshooting
Problem: The setting doesn’t seem to have any effect.
First, check that you changed the correct language. Then, verify that you are not using a project‑specific code style (look for a yellow “Project” indicator in the code style settings). If that’s the case, click “Set from” and choose “Default” or your preferred scheme. Also, check if you have an .editorconfig file in your project root that overrides the setting. You can temporarily rename it to test.
Problem: The chained calls still wrap, but only when the line is long.
That’s actually expected if you have “Do not wrap” selected — IntelliJ will still wrap when the line exceeds the right margin. If you don’t want that, increase the right margin or disable the “Wrap when typing reaches right margin” option in Editor > Code Style.
Problem: My builder is formatted with extra blank lines between calls.
This could be caused by “Blank lines” settings in the code style. Go to Wrapping and Braces and look for “Keep blank lines” or “Blank lines” sections. Set the number of blank lines to 0 or 1 as desired.
Problem: The formatting is fine, but my team members get different results.
Make sure you share your code style settings. You can export them via File > Export Settings or use an .editorconfig file. Also, encourage everyone to use the same IntelliJ version, as formatting behavior can change slightly between versions.
Frequently Asked Questions
Q: Will this affect other chained calls, like String methods?
Yes, the setting applies to all chained method calls in that language. If you only want to affect builders, you’d need to use a more advanced approach like a custom formatting rule or a separate code style profile.
Q: Can I make IntelliJ format each builder call on a new line, but keep other chains on one line?
Not directly. IntelliJ’s built‑in formatting does not distinguish between builders and other chains. You can achieve this with a custom “Code Style” per file, but it’s not trivial. Some users use a “Builder” annotation or a custom inspection to auto‑format, but that’s beyond standard settings.
Q: Does this work for Kotlin data classes with builder patterns?
Yes, the same steps apply for Kotlin. Just select “Kotlin” in the code style settings and adjust the “Chained method calls” option there.
Q: What if I want to keep the current formatting and never have IntelliJ touch it?
You can disable “Reformat Code” for specific files by adding them to the “Do not reformat” list in File > Settings > Editor > Code Style > “Formatter Control”. You can also use // @formatter:off and // @formatter:on comments around your builder code.
Q: Will this setting affect the “Optimize Imports” action?
No, “Optimize Imports” is a separate setting. You can keep it as is.
Q: I use IntelliJ’s “Reformat Code” shortcut, but it also changes my line breaks in other places. Why?
That’s because “Reformat Code” applies all code style rules, not just the chained method call setting. If you only want to adjust the wrapping, you can use “Auto‑indent Lines” (Ctrl+Alt+I) instead, which only fixes indentation.