Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 704
pChip Avatar asked Apr 23 '26 09:04

pChip


2 Answers

I achieved the same result without resorting to downloading Eclipse just to export the desired setting. I read the fantastic guide from vscode-java-formatter.md

I already have this JSON setting in vscode:

"java.format.settings.url": "dir/to/eclipse-java-google-style.xml"

Inside the local eclipse-java-google-style.xml, change the following's value from true to false.

<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/>

Save and restart vscode. Now chained methods won't be squeezed into one line.

like image 195
kohane15 Avatar answered Apr 25 '26 21:04

kohane15


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

like image 22
Gustavo Passini Avatar answered Apr 25 '26 22:04

Gustavo Passini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!