Tinker
Tinker

Reputation: 4535

Java formatting: Keep lines but fix indentation

I have the following code:

DSPOTTGuaranteedUserReachForecastModel model = new DSPOTTGuaranteedUserReachForecastModel(
    mockCache,
    demand,
    mockTargetingDatabaseHelper,
    programType,
    region);

The correct indentation should be two indents for each incomplete line. Ideally I would want to my text editor to format my code as such:

DSPOTTGuaranteedUserReachForecastModel model = new DSPOTTGuaranteedUserReachForecastModel(
        mockCache,
        demand,
        mockTargetingDatabaseHelper,
        programType,
        region);

However, when I try to format it using VSCode, I am getting all the new lines collapsing into one long line instead.

DSPOTTGuaranteedUserReachForecastModel model = new DSPOTTGuaranteedUserReachForecastModel(mockCache, demand,
        mockTargetingDatabaseHelper, programType, region);

Does anyone know what is the name of the rule for preserving line numbers?

Upvotes: 0

Views: 2121

Answers (2)

Molly Wang-MSFT
Molly Wang-MSFT

Reputation: 9471

About java formatting, VSCode-Java provides an Eclipse formatter file like Google Style.

The setting you wanted should be something like java_method_parameters_wrap = split_into_lines, but unfortunately it's not included in GoogleStyle.xml.

If you're still interested in java formatting, you may figure it out in DefaultCodeFormatterOptions.

[EDIT]

Download googlestyle.xml and edit the following settings:

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

Then in vscode settings.json, set

"java.format.settings.url": "<local path to java-google-style.xml>",

You can get the formatting style that you want:

enter image description here

Upvotes: 7

Ben Neighbour
Ben Neighbour

Reputation: 75

I've heard of an extension called Rewrap which may be what you want to try?

For Rewrap to work automatically, go to the command palette and type: Rewrap: Toggle Auto-Wrap

I hope this helps, and if not - hopefully, somebody else can contribute!

Upvotes: 0

Related Questions