Skip to content
Advertisement

Visual Studio Code: Formatting Java method chaining

I’m trying to change the VSCode settings which will allow me to do something like this with method chaining :

Flux
  .just("test", "string", "flux")
  .log()
  .map(String::toUpperCase);

The problem is whenever the formatter is applied, it changes it back to just one line:

Flux.just("test", "string", "flux").log().map(String::toUpperCase);

I have the Java Extensions Pack installed, which includes the ‘Language Support for Java by Red Hat’. That extension has a few format options added, and I’ve been trying to use them, but no such luck.

Here are the format options I have changed:

"editor.formatOnSave": true,
"java.format.settings.url": "https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml",
"java.format.enabled": true,
"java.format.settings.profile": "GoogleStyle"

Anyone have any ideas how I can have the formatter leave multi-lined method chaining alone?

Advertisement

Answer

From the Redhat Developer VSCode-Java plugin wiki:

You can also define the formatting preferences in your project’s .settings/org.eclipse.jdt.core.prefs. It will override global formatting settings. Since this is rather tedious, the best way to edit those preferences is to open your project in Eclipse and set the formatting preferences for your project there.

The option you will be looking for is:

Line Wrapping > Wrapping settings > Function Calls > Qualified invocations

Set it to Wrap all elements, every element on a new line, like in the picture below:

enter image description here

Advertisement